[Vuejs]-Can I define a variable in beforeCreate()?

0👍

beforeCreate is called immediately after the instance is initialized, after the resolution of the props, and before processing any further options like data() or calculated(). The difference between them is the beforeCreate hook is before an instance has been fully initialized, whereas the created hook is after an instance is created.

data() {
    return {
        country: 'USA'
    }
},
beforeCreate() {
    this.country = 'Japan' // at this point country is undefined but 
                                "this" is avalible  
},
created() {
    console.log(this.country) // 'USA'
}

Leave a comment