nuxt中的fetch和asyncData是两个比较重要的概念,今天看看这两个方法使用以及他们的区别。
fetch
假设我们现在的代码是这样:
export default {
async fetch() {
this.name = await new Promise((resolve, reject) => {
console.log('fetch start');
setTimeout(() => {
console.log('fetch end');
resolve(Math.random().toString(36).substr(2));
}, 1000)
})
},
beforeCreate() {
console.log('beforeCreate');
},
created() {
console.log('created');
},
beforeMount() {
console.log('beforeMount')
},
mounted() {
console.log('mounted');
}
}
执行时机问题
在服务端运行的时候,fetch是发生在created之后: