28. Vuex State

Vuex使用单一状态树,用一个对象就包含了全部的应用层级状态。这也意味着,每个应用将只包含一个 store 实例

import Vue from 'vue';
import Vuex from 'vuex';

Vue.use(Vuex)

const app = new Vue({
  el: '#app',
  // 把 store 对象提供给 “store” 选项,这可以把 store 的实例注入所有的子组件
  store,
  components: { Counter },
  template: `
    <div class="app">
      <counter></counter>
    </div>
  `
})
// component
const Counter = {
  template: `<div>{{ count }}</div>`,
  computed: {
    count () {
      return this.$store.state.count
    }
  }
}

mapState 辅助函数
当一个组件需要获取多个状态的时候,将这些状态都声明为计算属性会有些重复和冗余。为了解决这个问题,我们可以使用mapState辅助函数帮助我们生成computed属性

import { mapState } from 'vuex'

export default {
computed: mapState([
  // 映射 this.count 为 store.state.count
  'count'
])
}

<<:  DAY28: 运算是什麽?

>>:  【30天Lua重拾笔记31】进阶议题: 记忆体回收&弱表

Re: 新手让网页 act 起来: Day01 - 一起来认识 React 吧!

前言 哈喽!大家好,开赛前先来简单的自我介绍。前阵子因为公司专案的关系,开始接触 React,想要藉...

【第二二天 - Flutter GitHub Search 范例+RxDart+搜寻快取】

前言 今日的程序码 => GTIHBU 今天来讲讲搜寻的介绍。那当然,肯定要以 github ...

Day-21 Child Process

Child Process tags: IT铁人 Context Switching 前面提到电脑会...

【29】遇到不平衡资料(Imbalanced Data) 时 使用 SMOTE 解决实验

Colab连结 今天要介绍处理不平衡资料的方法叫 SMOTE (Synthetic Minority...

[Day9] THM Pickle Rick

今天来解的题目比较不是寻找公开 Exploit 的方式,而是要靠自己在网页上寻宝(?),来取得进一步...