Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

like image 533
Michael Gee Avatar asked Aug 05 '17 21:08

Michael Gee


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.

like image 148
dimitris tseggenes Avatar answered Sep 28 '22 03:09

dimitris tseggenes


As VueJS can be implemented into existing systems, you should be able to do so yes.

like image 25
Pedro The Magnificent Avatar answered Sep 28 '22 02:09

Pedro The Magnificent