Loading...

Nuxt 與 Vuex

Nuxt3 取消 Vuex classic mode

更新於 2025-03-13 08:37:45
Written by   Jacky Yang@Jacky Yang

如果使用nuxt 2.15.7 以上版本,並且vuex的語法是比較舊的(直接export default、namespaced: true 等等),終端機就會出現警告:

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


這時侯就要改成簡化後的版本:

// store/index.js

export const state = () => ({
  // states
})

export const mutations = {
  // mutations
}

export const actions = {
  nuxtServerInit(){}
}

export const getters = {
  //  getters
}


非常的簡化,並且每個檔名就是一個module,不用再把module merge 到 index.js,Nuxt已經幫忙merge了。

例如: dispatch store/user.js getUserData action

// store/user.jsexport const action = {
  getUserData(){
    // code
  }
}

// componentmethods: {
  getUserData(){
     this.$store.dispatch('user/getUserData')
  }
}