nuxt的使用和一些问题

/**
 cookie 安装
 yarn: yarn add cookie-universal-nuxt
 npm: npm i --save cookie-universal-nuxt
*/
  {
   modules: [
            ['cookie-universal-nuxt', { parseJSON: false }]
       ]
   }
   //store里使用
    this.app.$cookies.set('token', res.login_name)
    nuxtServerInit({ commit }, { app }) {
        console.log('token', app.$cookies.get('token'))
        const token = app.$cookies.get('token')
        commit('SET_TOKEN', token)
      }
/**
  eleUI 安装
  npm i element-ui -S
  plugins
*/
    import Vue from 'vue'
    import Element from 'element-ui'
    import locale from 'element-ui/lib/locale/lang/en'
    Vue.use(Element, { locale, zIndex: 3000 })
/**
  weditor 安装
  npm i wangeditor --save
  plugins
*/
    import Wangeditor from 'wangeditor'
    export default ({ app }) => {
      app.router.afterEach(() => {
        // 在Window原型中设置wangEditor
        Window.prototype.Wangeditor = Wangeditor
      })
    }
/**
  $axios 
  plugins
*/
    import { Message } from 'element-ui'
    export default function ({ $axios, redirect, app, store }) {
      $axios.interceptors.request.use(
        (config) => {
          // do something before request is sent
          return config
        },
        (error) => {
          // do something with request error
          return Promise.reject(error)
        }
      )
      $axios.onRequest((config) => {
        if (app.$cookies.get('token')) {
          config.headers['x-auth-token'] = store.getters.GET_TOKEN
        }
      })
      // response interceptor
      $axios.interceptors.response.use(
        /**
         * Determine the request status by custom code
         * Here is just an example
         * You can also judge the status by HTTP Status Code
         */
        (response) => {
          const res = response.data
          if (res.code * 1 === 200) {
            return res
          } else if (res.code * 1 === 203) {
            store
              .dispatch('loginOut')
              .then(() => {
                return res
              })
            Message.warning(res.msg || '系统错误')
          } else {
            Message.warning(res.msg || '系统错误')
          }
          return Promise.reject(new Error(res.msg))
        },
        (error) => {
          console.log('err' + error) // for debug
          return Promise.reject(error)
        }
      )
      $axios.onError(() => {
        redirect({ path: '/' })
      })
    }
/**授权中间件 middleware*/
    export default function ({ store, redirect, route }) {
      const whiteList = ['index', 'login'] // 没有重定向白名单
      // 如果用户未经过身份验证
      if (!store.getters.GET_TOKEN && !whiteList.includes(route.name)) {
        return redirect({ path: '/login' })
      }
    }

/**部署pm2守护进程*/
    打包:
         npm run build
    运行文件
         .nuxt static nuxt.config.js package.json
    依赖安装
         npm install
    pm2运行命令:
        pm2 start ./node_modules/nuxt/bin/nuxt.js -- start
        pm2会于运行nuxt.config.js里的代理
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

卤鸽子

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值