• vuex


    vuex 概念

        每一个 Vuex 应用的核心就是 store(仓库)。“store”基本上就是一个容器,
        1、Vuex 的状态存储是响应式的。
        2、你不能直接改变 store 中的状态。改变 store 中的状态的唯一途径就是显式地提交 (commit) mutation。
    
    

    State

        // store/index.js
        let store = new Vuex.Store({
            state:{
                count:100,
                dataList:[],
                title:"aaa",
            },
    
        })
    
        // *.vue  组件中获取
        1、计算属性获取
        computed: {
            count () {
                return this.$store.state.count
            }
        }
        2、mapState 辅助函数
         computed: mapState({
            // 箭头函数可使代码更简练
            count: state => state.count,
    
            // 传字符串参数 'count' 等同于 `state => state.count`
            countAlias: 'count',
    
            // 为了能够使用 `this` 获取局部状态,必须使用常规函数
            countPlusLocalState (state) {
            return state.count + this.localCount
            }
        })
        3、对象展开运算符
        mapState 函数返回的是一个对象。我们将它与局部计算属性混合使用
        computed:{
             // 使用对象展开运算符将此对象混入到外部对象中
            ...mapState({
                ...
            })
        }
    
    

    Getter

    从 store 中的 state 中派生出一些状态,例如对列表进行过滤并计数
    类似于全局函数的一个计算属性;
    
    const store = new Vuex.Store({
      state: {
        todos: [
          { id: 1, text: '...', done: true },
          { id: 2, text: '...', done: false }
        ]
      },
      getters: {
        doneTodos: state => (id)=>{
          return state.todos.filter(todo => todo.done)
        }
      }
    })
    
    // 访问
    this.$store.getters.doneTodos(1)
    
    
    2、mapGetters 辅助函数
     computed: {
      // 使用对象展开运算符将 getter 混入 computed 对象中
        ...mapGetters([
          'doneTodosCount',
          'anotherGetter',
          // ...
        ])
      }
      ...mapGetters({
        // 把 `this.doneCount` 映射为 `this.$store.getters.doneTodosCount`
            doneCount: 'doneTodosCount'
        })
    
    

    Mutation

    Mutation 必须是同步函数
    mutations:{
            // 设置数据
            setdata(state,payload){
                state.dataList = payload
            }
        },
    
    // 在组件中提交 Mutation
    你可以在组件中使用 this.$store.commit('xxx') 提交 mutation,
    或者使用 mapMutations 辅助函数将组件中的 methods 映射为 store.commit 调用
    
      methods: {
        ...mapMutations([
          'increment', // 将 `this.increment()` 映射为 `this.$store.commit('increment')`
    
          // `mapMutations` 也支持载荷:
          'incrementBy' // 将 `this.incrementBy(amount)` 映射为 `this.$store.commit('incrementBy', amount)`
        ]),
        ...mapMutations({
          add: 'increment' // 将 `this.add()` 映射为 `this.$store.commit('increment')`
        })
      }
    
    

    Action

    Action 提交的是 mutation,而不是直接变更状态。
    Action 可以包含任意异步操作。
    
    const store = new Vuex.Store({
      state: {
        count: 0
      },
      mutations: {
        increment (state) {
          state.count++
        }
      },
      actions: {
        increment (context) {
          context.commit('increment')
        }
      }
    })
    
    // 调用
    你在组件中使用 this.$store.dispatch('xxx') 分发 action,
    或者使用 mapActions 辅助函数将组件的 methods 映射为 store.dispatch 调用
    
    // 以载荷形式分发
    this.$store.dispatch('incrementAsync', {
      amount: 10
    })
    
    // 以对象形式分发
    this.$store.dispatch({
      type: 'incrementAsync',
      amount: 10
    })
    
     methods: {
        ...mapActions([
          'increment', // 将 `this.increment()` 映射为 `this.$store.dispatch('increment')`
    
          // `mapActions` 也支持载荷:
          'incrementBy' // 将 `this.incrementBy(amount)` 映射为 `this.$store.dispatch('incrementBy', amount)`
        ]),
        ...mapActions({
          add: 'increment' // 将 `this.add()` 映射为 `this.$store.dispatch('increment')`
        })
      }
    
    
    
  • 相关阅读:
    Linux下so动态库查看与运行时搜索路径的设置掌握这个就够了
    使用 VSCode 远程 图形化 GDB 调试 嵌入式linux
    gdb加载so库符号失败的解决方法
    falco hips规则引擎——仅仅支持单一事件匹配,算子比较少,亮点是事件上报里包含取证。
    从上层检测逻辑看ossec hips规则引擎——支持单事件正则,统计类规则,支持前后关联,例如暴力破解,爆破成功检测
    wazuh hips规则引擎和ossec的差异分析——本质上语法层面和ossec没有变化,但是公共字段提取出来了,同时正则匹配数据提取灵活性更强
    crash命令 —— vtop
    delphi ipubrefit
    炉石传说 佣兵战纪 英文
    Tinyfool的英语学习之路
  • 原文地址:https://www.cnblogs.com/kgwei520blog/p/14225234.html
Copyright © 2020-2023  润新知