Open dailynodejs opened 2 years ago
文件地址.nuxt/axios.js当前文件
import Axios from 'axios'
由于页面初次加载在服务端完成,window['$nuxt']并未生成。请求被再次调用时生成window['$nuxt']
export default (context) => {
context.app.router.beforeEach((to, from, next) => {
console.log('$nuxt in beforeEach', window['$nuxt'])
window['$nuxt'] && window['$nuxt'].$loading.start() && console.log('start in beforeEach');
next()
})
context.app.router.afterEach((to, from) => {
console.log('$nuxt in afterEach', window['$nuxt'])
window['$nuxt'] && window['$nuxt'].$loading.start() && console.log('start in afterEach');
})
}
async function createApp(ssrContext, config = {}) {
}
export { createApp, NuxtError }
.nuxt/client.js文件中引入并创建并挂载
// Create and mount App
createApp(null, NUXT.config).then(mountApp).catch(errorHandler)
async function mountApp (__app) {}
mountApp中执行路由钩子调用loadAsyncComponents和render
//Add beforeEach router hooks
router.beforeEach(loadAsyncComponents.bind(_app))
router.beforeEach(render.bind(_app))
文件地址.nuxt/client.js
// Check disabled page loading
this.$loading.manual = Component.options.loading === false
文件地址.nuxt/client.js
if (this._routeChanged === false && this._paramChanged === false && this._queryChanged === false) {
return next()
}
2.定义 nextCalled判断是否路由重定向,其默认值为false
// nextCalled is true when redirected
let nextCalled = false
const _next = (path) => {
if (from.path === path.path && this.$loading.finish) {
this.$loading.finish()
}
.....
}
上述条件成立则执行this.$loading.finish(),再执行next(),渲染结束
// If not redirected
if (!nextCalled) {
if (this.$loading.finish && !this.$loading.manual) {
this.$loading.finish()
}
next()
}
.nuxt/router.js
import Router from 'vue-router'
export function createRouter (ssrContext, config) {
}
.nuxt/index.js
async function createApp(ssrContext, config = {}) {
}
.nuxt/index.js
import { createStore } from './store.js'
.nuxt/store.js
import Vue from 'vue'
import Vuex from 'vuex'
Vue.use(Vuex)
export const createStore = store instanceof Function ? store : () => {
}
nuxt 编译之后会有一个目录
看一下 nuxt-loading.vue 文件
loading 配置
https://www.nuxtjs.cn/api/pages-loading