Vue.js 是一个基于组件化的前端框架,它提供了一些钩子函数,用于控制组件在不同阶段的行为和处理。以下是 Vue.js 常用的钩子函数以及它们的作用和示例:
Vue.component('my-component', {
beforeCreate: function () {
console.log('beforeCreate')
},
data: function () {
return {
message: 'Hello, Vue!'
}
}
})
Vue.component('my-component', {
created: function () {
console.log('created')
},
data: function () {
return {
message: 'Hello, Vue!'
}
}
})
Vue.component('my-component', {
beforeMount: function () {
console.log('beforeMount')
},
data: function () {
return {
message: 'Hello, Vue!'
}
}
})
Vue.component('my-component', {
mounted: function () {
console.log('mounted')
},
data: function () {
return {
message: 'Hello, Vue!'
}
}
})
Vue.component('my-component', {
beforeUpdate: function () {
console.log('beforeUpdate')
},
data: function () {
return {
message: 'Hello, Vue!'
}
}
})
Vue.component('my-component', {
updated: function () {
console.log('updated')
},
data: function () {
return {
message: 'Hello, Vue!'
}
}
})
Vue.component('my-component', {
beforeDestroy: function () {
console.log('beforeDestroy')
},
data: function () {
return {
message: 'Hello, Vue!'
}
}
})
Vue.component('my-component', {
destroyed: function () {
console.log('destroyed')
},
data: function () {
return {
message: 'Hello, Vue!'
}
}
})