admin管理员组文章数量:1293672
I am in the process of learning Vue. As far as I know I should be able to use a simple template string as a template in Vue. But for some reason, Vue renders only the first html tag.
Vue.config.devtools = true;
let template = `<h2>Hi {{name}}</h2><h1>{{age}}</h1>`
const vm = new Vue({
el: '#app',
data() {
return {
name: 'Name',
age: 23,
message: 'hello'
}
},
template,
methods: {
greet(e) {
return this.message = e.target.value
},
esc(e) {
e.target.value = ''
}
}
})
<div id="app"></div>
<script src=".5.17/vue.js"></script>
I am in the process of learning Vue. As far as I know I should be able to use a simple template string as a template in Vue. But for some reason, Vue renders only the first html tag.
Vue.config.devtools = true;
let template = `<h2>Hi {{name}}</h2><h1>{{age}}</h1>`
const vm = new Vue({
el: '#app',
data() {
return {
name: 'Name',
age: 23,
message: 'hello'
}
},
template,
methods: {
greet(e) {
return this.message = e.target.value
},
esc(e) {
e.target.value = ''
}
}
})
<div id="app"></div>
<script src="https://cdnjs.cloudflare./ajax/libs/vue/2.5.17/vue.js"></script>
This renders only "Hi Name". Why isn't age showing up?
Share Improve this question edited Mar 11, 2019 at 11:21 T.J. Crowder 1.1m199 gold badges2k silver badges1.9k bronze badges asked Mar 11, 2019 at 11:11 navinavi 531 gold badge1 silver badge3 bronze badges 2- Side note: It's a "template" (or "template literal"), not a "template string". If it's untagged, it evaluates to a string, but if it's used with a tag function, it doesn't necessarily. – T.J. Crowder Commented Mar 11, 2019 at 11:20
-
Nice one including the snippet!! Just FYI, the HTML pane in Stack Snippets is automatically put inside a
body
element, so you don't includehtml
,head
, orbody
there. I've updated the snippet to fix that. More about creating snippets: meta.stackoverflow./questions/358992 – T.J. Crowder Commented Mar 11, 2019 at 11:23
3 Answers
Reset to default 3Vue templates should have only one root node. I think you should have a message in your console warning about that.
Try:
let template = `<div><h2>Hi {{name}}</h2><h1>{{age}}</h1></div>`
After search a lot ways to implement ponent template even this one - all searchers need to know it must chance this flag to true - and add 10Kb to final result hahaha - this little one will add the piler to properly do work - by default its false
// if you are using VUE -> vue.config.js module.exports = { runtimeCompiler: true };
// if you are using VUE+QUASAR -> quasar.config.js add at -> build: { vueCompiler: true, }
A couple of issues:
As Sergeon said, there must be one root node, not two.
Vue's templates are different from JavaScript's template literals. You've used Vue's syntax (
{{name}}
), but in a JavaScript template literal. You probably wanted a simple string. (Although you could certainly use an untagged template literal instead if you wanted, since they evaluate to strings. Just not that{{name}}
and such placeholders are handled by Vue, and${name}
and such will be handled by the JavaScript template itself.)
Fixing those, it seems to work:
Vue.config.devtools = true;
let template = "<div><h2>Hi {{name}}</h2><h1>{{age}}</h1></div>";
const vm = new Vue({
el: '#app',
data() {
return {
name: 'Name',
age: 23,
message: 'hello'
}
},
template,
methods: {
greet(e) {
return this.message = e.target.value
},
esc(e) {
e.target.value = ''
}
}
})
<div id="app"></div>
<script src="https://cdnjs.cloudflare./ajax/libs/vue/2.5.17/vue.js"></script>
本文标签: javascriptUsing template string as Vue templateStack Overflow
版权声明:本文标题:javascript - Using template string as Vue template - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741582602a2386657.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论