4

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.com/ajax/libs/vue/2.5.17/vue.js"></script>

This renders only "Hi Name". Why isn't age showing up?

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. 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 include html, head, or body there. I've updated the snippet to fix that. More about creating snippets: meta.stackoverflow.com/questions/358992 Commented Mar 11, 2019 at 11:23

3 Answers 3

3

Vue 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>`
Sign up to request clarification or add additional context in comments.

Comments

1

After search a lot ways to implement component 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 compiler 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, }

Comments

0

A couple of issues:

  1. As Sergeon said, there must be one root node, not two.

  2. 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.com/ajax/libs/vue/2.5.17/vue.js"></script>

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.