0

In Vue2, I have a string along the lines of the following in my component.

<template>
    <div><h1>Hello World</h1>
    <div v-html="testString"></div>
    </div>
</template>
<script>
    export default {
        name: "test-component",
        props: ['incoming'],        
        data: function() {
            return {
                testString: "";
            }
        },
        created() {
            this.testString = this.incoming;
        }
    }
</script>

And then when I do the following (with all the imports done correctly)

<template>
    <text-component :incoming="mycontent"></text-component>
</template>
<script>
import 'test-component' from './path/to/test-component.vue'
export default { // etc, just presume this bit is right, it's only psudo code
components: ['test-component'],
data() { return { mycontent: '' }},
created() {
    this.mycontent="I just want to <router-link to='/home' v-html='Go Home'></router-link>"
</script>

So now I want the first template with testString to render the <router-link> as if I had put it in myself directly.

I've tried v-html and {{ }} , in my test-component but I think i'm missing something. I'm pretty sure in Vue1 I would use {{{ }}}.

Can anyone help out? Thanks.

2
  • <text-component> should be <test-component>.... it's the passing on the router-link from a string that I'm struggling with. Commented Sep 1, 2017 at 16:34
  • In that case, please use the edit link at the bottom left corner of the post to update the question Commented Sep 1, 2017 at 16:48

1 Answer 1

1

From the documentation on v-html:

Note that the contents are inserted as plain HTML - they will not be compiled as Vue templates.


You should be using a slot instead.

Add the slot to your testComponent:

<template>
  <div><h1>Hello World</h1>
  <slot></slot>
</template>

And then just put the content right in the test-component tag:

<template>
  <test-component>
    I just want to <router-link to='/home' v-html='Go Home'></router-link>
  </test-component>
</template>
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

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.