I have the function getSession
in the methods
Vuejs element and I need to execute it on load, either the page or the Vue instance
This is the Javascript code
new Vue ({
el:'#homeApp',
data:{
session:{}
},
methods:{
getSession: function () {
var vm = this;
axios.post('php/session/session.php',this.createdFormData(this.session))
.then(function (response) {
console.log(response);
var vm = this;
if (response.data.state == true ){
} else {
}
})
.catch(function (error) {
console.log(error);
});
},
createdFormData: function (postData){
var formDa = new FormData();
for(var key in postData){
formDa.append(key, postData[key]);
}
return formDa;
}
}
});
How can I do it? Can I do it from the Javascript document? or should I do it from the html document with some directive?
Ideally, you would do this inside mounted() which will be called as its name implies when mounting the instance to the DOM with the HTML elements loaded. It can be very useful for us to initialize external libraries in some cases.
Although if your method does not depend on any element of the DOM and will not render data on it, it could be done inside the method
created()
without problems.Basically with
created
no you will have access to the DOM elements, withmounted
yes,Ejm , $el returns el
div
with the id #appyou can do that with
created()
Reference source: https://v2.vuejs.org/v2/guide/instance.html