The study of Nuxt

Nuxt Vue is based on an application framework, using server-side rendering, let your SPA application (Vue) may also have SEO

Nuxt in the life cycle of some server (Node), a client, or even both sides are in:

1. Window object before the other does not exist

 2. You can edit layouts / error.vue file to customize error pages.

 <template>

  <div class="container"> <h1 v-if="error.statusCode === 404">页面不存在</h1> <h1 v-else>应用发生错误异常</h1> <nuxt-link to="/">首 页</nuxt-link> </div> </template> <script> export default { props: ['error'], layout: 'blog' // 你可以为错误页面指定自定义的布局 } </script>

 3. Customize Loading page

 

module.exports = {
  loading: '~components/loading.vue'
}

 loading.vue

 

<template lang="html">
  <div class="loading-page" v-if="loading"> <p>Loading...</p> </div> </template> <script> export default { data: () => ({ loading: false }), methods: { start () { this.loading = true }, finish () { this.loading = false } } } </script>

 4. Verify parameters

 

<script>
export default { validate({ params, query }) { return /^d+$/.test(params.id) // must be number } } </script>

 5. Header, Footer and other public assembly put what?

 

As we all know, VUE-cli import documents are app.vue , in nuxt in development it is ./layout/default.vue

<template>
  <div id="app"> <!-- 公共头部组件 --> <xxx-header></xxx-header> <!-- 路由视图,相当于router-view --> <nuxt/> <!-- 公共底部组件 --> <xxx-footer></xxx-footer> </div> </template>
6.

 No keep-alive

Because it is server-side rendering, so I do not support the keep-alive component, it is naturally activated, deactivated the two did the life cycle

 

7. Configure Plug

All plug-ins are written in the / plugins directory under here to vue-lazyload A Case Study

plugins/lazy-load.js

import Vue from 'vue'
import VueLazyLoad from 'vue-lazyload' Vue.use(VueLazyLoad, { loading: require('~/assets/images/loading.jpg'), error: require('~/assets/images/error.jpg') })
nuxt.config.js
module.expors = {
  plugins = [
    {
      src: "~/plugins/lazy-load",
      ssr: false
    }
  ]
}

 

8. Use Vuex

nuxt own integrated vuex , so no need to install , the new index.js in the / store directory can be used

import Vuex from 'vuex'

let store = () => new Vuex.Store({ state: { token: '' }, mutations: { setToken (state, token) { state.token = token } } }) export default store
9. logged in?

vue-cli项目中,我们可以用vuex-persistedstate,它可以使vuex的状态持久化,页面刷新都不会丢失,原理当然是localStorage啦!当然我更喜欢用vue-cookies进行保存token,问题来了,nuxt项目怎么保存登录状态呢?当然上面这两种方法我们都可以使用,但是有个问题,由于在created钩子中不存在window对象(获取cookie、localStorage都需要window对象),当你需要判断是否存在token的时候,你必须要在mounted进行操作,这说明页面进来的一瞬间你无法得知是否已经登录了,这会导致显示用户名、组件显示于隐藏都慢半拍


nuxt非常友好,它提供了fetch钩子,还有nuxtServerInit,这两个钩子都运行在服务端并且我们能很快速地操作store

10.

14. fetch的使用

如果页面组件设置了fetch方法,它会在组件每次加载前被调用(在服务端或切换至目标路由之前),此方法需要跟服务端的人员配合

<script>
export default { async fetch ({ app, store, params }) { let { data } = app.$axios.get('/token'); store.commit('setToken', data.token); } } </script>


 

Guess you like

Origin www.cnblogs.com/zhouyideboke/p/10931683.html