Nuxt.js报错:Classic mode for store/ is deprecated and will be removed in Nuxt 3

将Nuxt.js升级到最新版的2.4.0时,终端提示警告:

Classic mode for store/ is deprecated and will be removed in Nuxt3

在Nuxt.js的源码中,其中有一段代码如下:

// If store is an exported method = classic mode (deprecated)
if (typeof store === 'function') {
    const log = (process.server ? require('consola') : console)
    return log.warn('Classic mode for store/ is deprecated and will be removed in Nuxt 3.')
}

从代码中我们可以看出,它对store做了一个是否方法的判断,如果我们export的是一个方法就会出现本文中的警告信息。 如下是出现警告信息的写法(文件路径:store/index.js):

import Vue from 'vue'
import Vuex from 'vuex'
Vue.use(Vuex)
const state = {
    token:null,
}
const getters = {
    getToken(state){
        return state.token
    },
}
const mutations = {
    setToken(state, token) {
        state.token = token
    },
}
export const actions = {
    //TODO ajax here
}
const store = () => {
    return new Vuex.Store({
        state,
        getters,
        mutations,
        actions
    })
}
export default store

改成如下的写法就可以了(文件路径:store/index.js):

export const state = () => ({
    token:null,
})
export const getters = {
    getToken(state){
        return state.token
    },
}
export const mutations = {
    setToken(state, token) {
        state.token = token
    },
}
export const actions = {
    //TODO ajax here
}

Classic mode for store/ is deprecated and will be removed in Nuxt 3.解决方案

你可能感兴趣的:(Nuxt.js)