vuex

作者: 李小白呀 | 来源:发表于2021-04-28 17:07 被阅读0次
    1. 新建文件
    const state = {
      zp: [],
      postName: ''
    }
    
    // getters
    const getters = {}
    
    // actions
    const actions = {}
    
    // mutations
    const mutations = {
      setZp(state, zp) { // 设置参数
        state.zp = zp
      },
      setPostName(state, postName) {
        state.postName = postName
      }
    }
    
    export default {
      namespaced: true,
      state,
      getters,
      actions,
      mutations
    }
    
    1. 挂载
    import Vue from 'vue'
    import Vuex from 'vuex'
    import getters from './getters'
    // import zp from './modules/zpList'
    Vue.use(Vuex)
    
    ...
    
    const store = new Vuex.Store({
      modules,
      getters
    })
    
    export default store
    
    1. 存入
    async search() {
        if (this.zpList.postName) {
          this.zpList = this.zpList.postName
        }
        const { data: res } = await zpList(JSON.stringify(this.zpList))
        console.log(res)
        this.$store.commit('zpList/setZp', { // zpList表示vuex的文件名
          zp: res.data
        })
        if (res.code == '0') {
          this.$message.success(res.msg)
        }
      },
    

    4.使用

    import { mapState } from 'vuex'
    
    // 计算属性
      computed: {
        ...mapState({
          // 'zp'
          zpList: state => state.zpList.zp
        })
      },
    

    相关文章

      网友评论

          本文标题:vuex

          本文链接:https://www.haomeiwen.com/subject/ncnvrltx.html