Are you able to use vue.js inside an ejs file?

Viewed 8130

Sorry guys I am new to node.js and was wondering whether we are allowed to use vue.js within an ejs file.

2 Answers

Yes, we can. Here is a scenario. Let's say you want to render a data Object to ejs file, and you want this data Object be accessible from VueJS.
First, in your controller, you have to render it as a JSON string
res.render("index", { data: JSON.stringify(data) }); in order to access it in your javascript code.
Then, in your VueJS code inside ejs file, you simply access it this way:

var app = new Vue({
    el: '#app',
    data: {
        myData: JSON.parse('<%- data %>')
    }
}) 

Note about <%- tag in your VueJS code, it is necessary in order to output your data properly.

Related