ThinkChat2.0新版上线,更智能更精彩,支持会话、画图、阅读、搜索等,送10W Token,即刻开启你的AI之旅 广告
[TOC] ### nuxt.js vuex 状态树 ![](https://box.kancloud.cn/5e0960d0b3c04752ed4a420a4efcbc35_223x108.png) * [ ] city.js ~~~ const state = () => ({ list: ['北京','上海'] }) const mutations = { add(state, text) { state.list.push(text) } } const actions = { add: ({commit}, text) => { commit('add', text) } } export default { namespaced: true, state, mutations, actions } ~~~ ***** index.js ~~~ import Vue from 'vue' import Vuex from 'vuex' import city from './module/city' import navbar from './module/navbar' Vue.use(Vuex); const store = () => new Vuex.Store({ modules: { city, navbar }, actions: {} }) export default store ~~~ ***** 页面组件中使用 ~~~ <template> <div> page is search <ul> <li v-for="(item, index) in list" :key="index"> {{item}} </li> </ul> </div> </template> ~~~ ~~~ <script type="text/ecmascript-6"> import { mapMutations } from 'vuex' export default { data() { return { items: [] } }, computed: { list() { return this.$store.state.city.list } }, } </script> ~~~