状态store的写入和读取

1 vue中store的写入和读取

1.1 创建store文件夹
1.2 写js文件

const state = {  // this.$store.state.
    username: null,
    token: null,
    brandArr: [], // 记录访问路径
    shopStatus: '0'
  }
  // 获取数据 对外暴露
  // mautation 内部调用,外部不允许调用
  const mutations = {
    setUsername (state, username) {
      state.username = username
    },
    setToken (state, token) {
      state.token = token
      localStorage.setItem('token',state.token)
    },
    setShopStatus (state, shopStatus) {
      state.shopStatus = shopStatus
    },
  }
  // 修改数据 对外暴露
  // 调用方式: store.dispatch(方法名, 参数)
  const actions = {  // this.$store.dispatch('setToken', res.data.data.token)
    setUsername ({commit}, username) {
      commit('setUsername', username)
    },
    setToken ({commit}, token) {
      commit('setToken', token)
    },
    setShopStatus ({commit}, shopStatus) {
      commit('setShopStatus', shopStatus)
    },
  }
  export default {
    state,
    actions,
    mutations
  }

1.3 实例化store
文件结构
在store的index.js 中引入相应的文件

import Vue from 'vue'
import Vuex from 'vuex'
import user from './user/user'
Vue.use(Vuex)
export default new Vuex.Store({
  modules: {
    user
  },
  plugins: []
})

1.4 在页面中(.vue文件)将数据存入store中

this.$store.dispatch('setUsername', res.username)

1.5 在页面中(.vue文件)将数据从store中读取出来

 computed: {
      // 个人账户
      userName() {
        return this.$store.state.user.username
      }
  }

2 nuxt框架中store的写入和读取

2.1 创建store文件夹
2.2 写js文件

export const state = ()=>({  // this.$store.state.
    username: null,
    token: null,
    brandArr: [], // 记录访问路径
    shopStatus: '0'
  })
  // 获取数据 对外暴露
  // mautation 内部调用,外部不允许调用
  export const mutations = {
    setUsername (state, username) {
      state.username = username
    },
    setToken (state, token) {
      state.token = token
      localStorage.setItem('token',state.token)
    },
    setShopStatus (state, shopStatus) {
      state.shopStatus = shopStatus
    },
  }
  // 修改数据 对外暴露
  // 调用方式: store.dispatch(方法名, 参数)
 export  const actions = {  // this.$store.dispatch('setToken', res.data.data.token)
    setUsername ({commit}, username) {
      commit('setUsername', username)
    },
    setToken ({commit}, token) {
      commit('setToken', token)
    },
    setShopStatus ({commit}, shopStatus) {
      commit('setShopStatus', shopStatus)
    },
  }

2.3 实例化store
文件结构
2.4 在页面中(.vue文件)将数据存入store中

 this.$store.dispatch('user/setUsername', res.username)
 
 this.$store.dispatch('cart/setAccountItem', accountItem)

2.5 在页面中(.vue文件)将数据从store中读取出来

 computed: {
      // 个人账户
      userName() {
        return this.$store.state.user.username
      }
  }

总结

nuxt和vue从store存取有三个地方不一样
1 vue框架中的state是一个对象,nuxt框架中的state是一个函数
2 vue框架中store的文件目录和nuxt框架中的store目录结构不一样
    vue框架中是由store文件夹下的index.js引入相关的.js文件,再实例化Vuex.Store
    nuxt框架中.js文件直接就在store目录下。不需要自己实例化
3 vue框架中往store存数据和nuxt框架中往store存数据的方式不一样
    vue框架中直接写函数名称就可以了
    nuxt框架中需要写明是哪个.js文件中的方法

发布了17 篇原创文章 · 获赞 0 · 访问量 416

猜你喜欢

转载自blog.csdn.net/weixin_44805161/article/details/104770993