vuex

  • vue 插件 npm install vuex --save
  • 将多个组件的共享状态进行 集中式管理 - 极易破坏单向数据流
  • 多个视图依赖于同一状态 ----- 就 props 而言:嵌套组件传参繁琐,兄弟组件传参无能为力
  • 来自不同视图的行为需要变更同一状态

将组件的共享状态抽取出来: 组件共享的数据、后台请求数据

  • 核心对象 store

向外暴露 store 对象

状态自管理应用 分为

state -------- 驱动应用的 数据源

view -------- 以声明方式将 state 映射到视图 - template

actions -------- 响应有用户在 view 上的用户输入导致的状态变化 - methods: {}

action 中的 commit() 来触发 mutation 的调用,间接更新 state

一个 action 中可能有多个 commit("mutation的名字", 传参给mutation)

开发工具 会监视 mutations

---------------------------------------------------------------------------------------

src/store.js -------- vuex 最核心的管理模块

import Vue from "vue";

import Vuex from "vuex";

Vue.use(Vuex);

const state = {        // 包含所有状态数据的对象,相当于 data

count: 1        // 指定初始状态数据

};

const actions = {        // 包含 n 个用于间接更改共享状态数据方法的对象 - 分析对状态数据的几个间接操作

increment ({commit}) {commit("INCREMENT")}

decrement ({commit}) {commit("DECREMENT")}

incrementIfOdd ({commit, state}) {if(state.count%2===1)commit("INCREMENT")}

// 不像 react-thunk 才能实现 redux 的异步,vuex 本身就可以异步

incrementAsync ({commit}) {setTimeout(()=>commit("INCREMENT"), 1000)}    

};

const mutations = {        // 包含 n 个用于直接更改共享状态数据方法的对象 - 分析对状态数据的几个直接操作

INCREMENT (state) {state.count++}

DECREMENT (state) {state.count--}

};

const getters = {        // 包含了 计算属性 的定义

evenOrOdd(state){return state.count % 2 === 0 ? '偶数' : '奇数'}

};

export default new Vuex.Store({

state,

actions,

mutations,

getters

});

---------------------------------------------------------------------------------------

src/main.js

  • import Vue from 'vue'
    import App from './App.vue'
    import store from './store/index' /* eslint-disable no-new */
    new Vue({
    el: '#app',
    components: {
    App
    },
    template: '<App/>',
    store
    })


src/App.vue -------- 组件中使用 vuex 管理的状态数据 - ViexModel 即 组件对象的 this 会多一个 $store 对象

...

methods: {

increment () {this.$store.dispatch("increment")}

decrement () {this.$store.dispatch("decrement")}

incrementIfOdd () {this.$store.dispatch("incrementIfOdd")}

incrementAsync () {this.$store.dispatch("incrementAsync")}

}

...

------------------------------------ 简化写法 ------------------------------------

import {mapState, mapGetters, mapActions} from "vuex"

...

computed: {

...mapState(["count"]),        // 函数返回有一个对象,拆包解构以后再放入另一个对象

....mapGetters(["evenOrOdd"])

}        // 相当于:

methods: {...mapActions(["increment", "decrement", "incrementIfOdd", "incrementAsync"])}    // 用的少?因为只适合只有 this.$store 的函数

...


Vuex.Store({ modules: {...} }) --------- 大型项目,按 功能模块 合理拆分 store

Vuex 允许我们将 store 分割成模块(module)。

每个模块拥有自己的 state、mutation、action、getter、

甚至是嵌套子模块——从上至下进行同样方式的分割

  • 对于模块内部的 mutation 和 getter,接收的第一个参数是模块的局部状态对象

  • 同样,对于模块内部的 action,局部状态通过 context.state 暴露出来,根节点状态则为 context.rootState

  • 对于模块内部的 getter,根节点状态会作为第三个参数暴露出来

5

5

vue_vuex的更多相关文章

随机推荐

  1. jQuery使用(十):jQuery实例方法之位置、坐标、图形(BOM)

    offset() position() scrollTop().scrollLeft width().height() innerWidth().outerWidth().innerHeight(). ...

  2. MySQL初步

    一 写在开头1.1 本节内容本节的主要内容是MySQL的基本操作(来自MySQL 5.7官方文档). 1.2 工具准备一台装好了mysql的ubuntu 16.04 LTS机器. 二 MySQL的连接 ...

  3. fetch 的控制器和观察者

    因为受 cancelable promise 的拖延,fetch 一直没有传统的 XHR 所拥有的 abort() 和 onprogress 功能,去年年底 cancelable promise 草案 ...

  4. 关于PHP创建接口及调用接口的简短例子(本地)

    ********************************************************************************************** /*这是P ...

  5. Django logging配置

    1,在项目下建个文件夹    log 2,在django的setting的配置下添加路径     BASE_LOG_DIR = os.path.join(BASE_DIR, "log&quo ...

  6. git切换到新的远程地址

    查看仓库链接 git remote -v 修改url链接 git remote set-url origin URL

  7. 智联python 技能摘取

  8. 关于模拟I2C的一些问题???

    1.在调试BH1750时发现stm32f103rb单片机用模拟I2C通讯时引脚使用开漏模式能正常读出来数据,使用推挽模式则完全无法通讯,发送地址后从机没有应答? https://blog.csdn.n ...

  9. [转] fastText

    mark- from : https://www.jiqizhixin.com/articles/2018-06-05-3 fastText的起源 fastText是FAIR(Facebook AIR ...

  10. ASP.NET Core学习之五 EntityFrameworkCore

    目的:运用EntityFrameworkCore ,使用codefirst开发 一.创建web项目 创建一个不进行身份验证的   ASP.NET Core Web Application (.NET ...