Node.js/nuxt/生命周期:修订间差异
来自康健生活
无编辑摘要 |
|||
| (未显示同一用户的6个中间版本) | |||
| 第1行: | 第1行: | ||
== 生命周期 == | == 生命周期 == | ||
{| class="wikitable" | |||
|- | |||
! 生命周期 !! 是否获取dom节点 !! 是否可以获取data !! 是否获取methods || 是否获取session || 是否获取windows | |||
|- | |||
| beforeCreate || 否 || 否 || 否 || sessionStorage is not defined || window is not defined | |||
|- | |||
| created || 否 || 是 || 是 || sessionStorage is not defined || window is not defined | |||
|- | |||
| beforeMount || 否 || 是 || 是 || 是 || 是 | |||
|- | |||
| mounted || 是 || 是 || 是 || 是 || 是 | |||
|} | |||
=== 概述 === | === 概述 === | ||
<syntaxhighlight lang="javascript"> | <syntaxhighlight lang="javascript"> | ||
| 第33行: | 第45行: | ||
} | } | ||
} | } | ||
}</syntaxhighlight> | |||
== 常见对象 == | |||
<syntaxhighlight lang="javascript"> | |||
export default { | |||
asyncData() { | |||
console.log(window) // 服务端报错 | |||
}, | |||
fetch() { | |||
console.log(window) // 服务端报错 | |||
}, | |||
created () { | |||
console.log(window) // undefined | |||
}, | |||
mounted () { | |||
console.log(window) // Window {postMessage: ƒ, blur: ƒ, focus: ƒ, close: ƒ, frames: Window, …} | |||
} | |||
}</syntaxhighlight> | }</syntaxhighlight> | ||
| 第45行: | 第73行: | ||
*: matching layout | *: matching layout | ||
*: matching page & children | *: matching page & children | ||
* validate | * validate (校验参数) | ||
*: pages/* | *: pages/* | ||
*: components/* | *: components/* | ||
* asyncData | * asyncData | ||
*: pages/* | *: pages/* | ||
* fetch | * fetch (fetch 方法用于在渲染页面前填充应用的状态树(store)数据, 与 asyncData 方法类似,不同的是它不会设置组件的数据。) | ||
*: pages/* | *: pages/* | ||
*: components/* | *: components/* | ||
| 第57行: | 第85行: | ||
* vue 周期 created (组件的data和methods可用,页面DOM未渲染) | * vue 周期 created (组件的data和methods可用,页面DOM未渲染) | ||
* head | * head | ||
* render | * render (服务端向模板中填充动态数据) | ||
== 客户端 == | == 客户端 == | ||
* store(!nuxtServerInit) | * store(!nuxtServerInit) | ||
* vue 周期 | * vue 周期 | ||
* beforeMounted | * beforeMounted (模板结构已经在内存中编译完成,并没有渲染到页面,页面只有模板不存在动态数据) | ||
* mounted | * mounted (组件创建阶段的最后一个生命周期。页面已经渲染,数据已经填充。运行中的生命周期函数,会根据data数据的变化有选择性的触发0次或n次) | ||
* beforeUpdate | * beforeUpdate (此时数据是最新的,但页面上呈现的数据还是旧的) | ||
* updated | * updated (页面已经完成了更新,此时data数据是最新的,同时页面上呈现的数据也是最新的) | ||
* beforeDestory | * beforeDestory (执行该函数的时候,组件即将被销毁但还没有真正销毁,此时组件(data、methods等数据或方法)还可以正常用) | ||
* destoryed | * destoryed (组件完成销毁) | ||
* head | * head | ||
* render | * render | ||
== question == | |||
=== store === | |||
在实际项目中,由于在 nuxt 的一些存储在 store 中的数据,比如验证登陆状态的 token 等,只能在 mounted 中操作(直到 mounted 周期才挂载 window 对象),这样又会引发一个问题,在进入页面的瞬间无法立即获得 token(包括一系列的开销),视觉上会有明显的延迟感。 | |||
如果使用 nuxt,可以借助 fetch 和 nuxtServerInit 规避这个问题,在页面渲染(组件加载 )前快速操作 store。 | |||
==== fetch ==== | |||
<syntaxhighlight lang="javascript"> | |||
export default { | |||
async fetch({ app, store, params}) { | |||
let { data } = app.$axios.get(/token) | |||
store.commit('user/setToken', data.token) | |||
} | |||
}</syntaxhighlight> | |||
==== nuxtServerInit ==== | |||
<syntaxhighlight lang="javascript"> | |||
let store = () => new Vuex.store({ | |||
state: { | |||
token: '' | |||
}, | |||
mutations: { | |||
setToken (state, token) { | |||
state.token = token | |||
} | |||
}, | |||
actions: { | |||
nuxtServerInit({ commit}, { req }) { | |||
let cookie = req.headers.cookie | |||
commit('setToken ', cookie.token) | |||
} | |||
} | |||
})</syntaxhighlight> | |||
2020年9月17日 (四) 11:03的最新版本
生命周期
| 生命周期 | 是否获取dom节点 | 是否可以获取data | 是否获取methods | 是否获取session | 是否获取windows |
|---|---|---|---|---|---|
| beforeCreate | 否 | 否 | 否 | sessionStorage is not defined | window is not defined |
| created | 否 | 是 | 是 | sessionStorage is not defined | window is not defined |
| beforeMount | 否 | 是 | 是 | 是 | 是 |
| mounted | 是 | 是 | 是 | 是 | 是 |
概述
export default {
middleware () {}, //服务端
validate () {}, // 服务端
asyncData () {}, //服务端
fetch () {}, // store数据加载
beforeCreate () { // 服务端和客户端都会执行},
created () { // 服务端和客户端都会执行 },
beforeMount () {},
mounted () {} // 客户端
}
asyncData
export default {
asyncData (ctx) {
ctx.app // 根实例
ctx.route // 路由实例
ctx.params //路由参数
ctx.query // 路由问号后面的参数
ctx.error // 错误处理方法
}
}
export default {
async asyncData (ctx) {
// 尽量使用try catch的写法,将所有异常都捕捉到
try {
throw new Error()
} catch {
ctx.error({statusCode: 500, message: '服务器开小差了~' })
}
}
}
常见对象
export default {
asyncData() {
console.log(window) // 服务端报错
},
fetch() {
console.log(window) // 服务端报错
},
created () {
console.log(window) // undefined
},
mounted () {
console.log(window) // Window {postMessage: ƒ, blur: ƒ, focus: ƒ, close: ƒ, frames: Window, …}
}
}
服务端
- nuxtServerInit
- store
- action
服务端、客户端
- middleware
- nuxt.config.js
- matching layout
- matching page & children
- validate (校验参数)
- pages/*
- components/*
- asyncData
- pages/*
- fetch (fetch 方法用于在渲染页面前填充应用的状态树(store)数据, 与 asyncData 方法类似,不同的是它不会设置组件的数据。)
- pages/*
- components/*
- vue 周期 data
- vue 周期 beforeCreate (组件的data和methods以及页面DOM结构没有初始化)
- vue 周期 created (组件的data和methods可用,页面DOM未渲染)
- head
- render (服务端向模板中填充动态数据)
客户端
- store(!nuxtServerInit)
- vue 周期
- beforeMounted (模板结构已经在内存中编译完成,并没有渲染到页面,页面只有模板不存在动态数据)
- mounted (组件创建阶段的最后一个生命周期。页面已经渲染,数据已经填充。运行中的生命周期函数,会根据data数据的变化有选择性的触发0次或n次)
- beforeUpdate (此时数据是最新的,但页面上呈现的数据还是旧的)
- updated (页面已经完成了更新,此时data数据是最新的,同时页面上呈现的数据也是最新的)
- beforeDestory (执行该函数的时候,组件即将被销毁但还没有真正销毁,此时组件(data、methods等数据或方法)还可以正常用)
- destoryed (组件完成销毁)
- head
- render
question
store
在实际项目中,由于在 nuxt 的一些存储在 store 中的数据,比如验证登陆状态的 token 等,只能在 mounted 中操作(直到 mounted 周期才挂载 window 对象),这样又会引发一个问题,在进入页面的瞬间无法立即获得 token(包括一系列的开销),视觉上会有明显的延迟感。
如果使用 nuxt,可以借助 fetch 和 nuxtServerInit 规避这个问题,在页面渲染(组件加载 )前快速操作 store。
fetch
export default {
async fetch({ app, store, params}) {
let { data } = app.$axios.get(/token)
store.commit('user/setToken', data.token)
}
}
nuxtServerInit
let store = () => new Vuex.store({
state: {
token: ''
},
mutations: {
setToken (state, token) {
state.token = token
}
},
actions: {
nuxtServerInit({ commit}, { req }) {
let cookie = req.headers.cookie
commit('setToken ', cookie.token)
}
}
})
No categories