admin管理员组文章数量:1314577
I have vue template data as string. For example,
String s = "<div>{{myData}}</div>"
And now I want to render in my already defined vue ponent.
<template>
<div>
HERE I NEED TO PLACE THE STRING
</div>
</template>
<script>
export default {
name: 'HelloWorld',
data: {
myData: "IRONMAN"
},
}
</script>
Now I want the output as IRONMAN
How can i achieve this? Pleas help. Thanks
I have vue template data as string. For example,
String s = "<div>{{myData}}</div>"
And now I want to render in my already defined vue ponent.
<template>
<div>
HERE I NEED TO PLACE THE STRING
</div>
</template>
<script>
export default {
name: 'HelloWorld',
data: {
myData: "IRONMAN"
},
}
</script>
Now I want the output as IRONMAN
How can i achieve this? Pleas help. Thanks
Share Improve this question asked Sep 12, 2019 at 7:20 bCliksbCliks 2,9787 gold badges31 silver badges52 bronze badges 3-
2
this.template = Vue.pile('<div>{{myData}}</div>').render
looks promising. source – Roland Starke Commented Sep 12, 2019 at 7:24 - 1 Is there any reason to make the div not an child pontent itself? – Sebastian Commented Sep 12, 2019 at 7:25
-
1
From where is this
String s
ing? – void Commented Sep 12, 2019 at 7:26
1 Answer
Reset to default 8You can have a single file ponent and do this - I have one called Dynamic.vue
which accepts a HTML string - I use this to allow the users to generate their own templates and the bindings all match up properly, something like:
<script>
export default {
data () {
return {
someVar: 'Test'
}
},
props: {
templateHtml: {
templateHtml: true,
type: String
}
},
created () {
this.$options.template = this.templateHtml
}
}
</script>
If you were to call it like:
this.htmlData = '<div>Hello - {{{someVar}}</div>'
....
<my-dynamic-ponent :template-html="htmlData" />`
You would see the output
Hello Test
You would then omit the <template>
part in the SFC.
Note: In order for this to work, you must also have the Vue Compiler included in your project (as this handles piling the SFC into render functions which Vue uses to display data).
This link: https://v2.vuejs/v2/guide/installation.html#CLI can give more information about including the Vue Compiler.
本文标签: javascriptdynamically render vue templateStack Overflow
版权声明:本文标题:javascript - dynamically render vue template - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741966125a2407557.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论