Skip to main content
  1. About
  2. For Teams
Asked
Viewed 3k times
1

I've got a string like this:

"Hello World! It's me, Chelsea."

And a list of strings.

["Hello", "World", "Chelsea"]

I'd like to dynamically wrap the matching strings in Vue components. Individually, it looks like this:

<MyComponent :text="Hello"/> <MyComponent :text="World"/>! It's me, <MyComponent :text="Chelsea"/>.

The solution for this could be something like the following (thanks Ulysse BN):

<template v-for="s in string.split(/\b/)">
  <MyComponent v-if="list.includes(s)" :string="s"/>
  <span v-else>{{ s }}</span>
</template>

But we run into problems where we have multi-word strings in our list (e.g. "It's me") and more specifically, overlapping words. If we added to the list of strings "Hello World", here's what the ideal result looks like:

<MyComponent :text="Hello World"> 
  <MyComponent :text="Hello"/> 
  <MyComponent :text="World"/>!
</MyComponent>
It's me, <MyComponent :text="Chelsea"/>.

How can I achieve this functionality? I have a hunch it involves v-for and some kind of recursive function, but how I can't say.

3
  • I don't think that split is doing what you think
    Dan
    –  Dan
    2020-01-08 06:01:23 +00:00
    Commented Jan 8, 2020 at 6:01
  • You're right. I'll make it Regex instead of a string.
    Jasper R.
    –  Jasper R.
    2020-01-08 06:10:35 +00:00
    Commented Jan 8, 2020 at 6:10
  • Seems this needs to be thought through more. Some issues: 1) If your loop traverses the split string pieces, how will you check multi-word phrases? 2) Should "me, Chelsea" match ["me Chelsea"] (note the comma in the string but not in the list) 3) What will you display if your list is as follows: ["Hello", "World", "Chelsea", "Hello World", "World It's"]? In that case, "World" belongs to 2 separate parent phrases
    Dan
    –  Dan
    2020-01-08 18:41:29 +00:00
    Commented Jan 8, 2020 at 18:41

1 Answer 1

1

I'm not sure of your use case, but maybe something like this would be suitable:

<template v-for="item in myContent">
    <span v-if="item.type === 'string'">{{ item.content }}</span>
    <my-component v-else-if="item.type === 'component'" :text="item.content" /> 
</template>

myContent: [
    {'content': 'hello', type: 'string'},
    {'content': 'hello world', type: 'component'},
    {'content: 'world', type: 'component'},
    {'content': 'oh hello world', type: 'string'}
]

You use computed to take the string and return it in this structure, breaking out any of the matched words.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

Post as a guest

Required, but never shown

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.

Morty Proxy This is a proxified and sanitized view of the page, visit original site.