美文网首页
Vuex总结

Vuex总结

作者: xbmchina | 来源:发表于2018-12-18 23:03 被阅读0次

简介

简单来讲:共享的状态管理器,比如你想修改某些背景色,设备状态、cookie等信息的时候你可以考虑用它。其他的当跨页面共享数据的时候也可以考虑用它。其他的自己去看官网、所有的API参考

State单一状态树

用来存储数据的,相当于一个变量(支持json对象、数组、字符串等),相当于数据源。

获取state中的状态值可以通过直接找或者通过getter方式

//方式一
this.$store.state.test.phone.name

//方式二
this.$store.getters.phone

Getter

有时候我们需要从 store 中的 state 中派生出一些状态,例如对列表进行过滤并计数。
Vuex 允许我们在 store 中定义“getter”(可以认为是 store 的计算属性)。就像计算属性一样,getter 的返回值会根据它的依赖被缓存起来,且只有当它的依赖值发生了改变才会被重新计算。

Getter可以在组件的computed的时候通过get方法进行获取。以及触发Mutation中的方法进行修改。

  computed: {
    company: {
      get() {
        return this.$store.state.test.company;
      },
      set(value) {
        this.$store.commit("CHANGE_COMPANY", value);
      }
    }

Mutation

更改 Vuex 的 store 中的状态的唯一方法是提交 mutation。它是同步的。

在页面可以直接用commit的方式触发其中的方法。

this.$store.commit("CHANGE_COMPANY", value);

Action

Action 类似于 mutation,不同在于:

  • Action 提交的是 mutation,而不是直接变更状态。
  • Action 可以包含任意异步操作。

Action可以通过dispatch来触发。

   this.$store.dispatch('toggleDevice',this.company)

所有整合

主配置index.js

import Vue from 'vue'
import Vuex from 'vuex'
import app from './modules/app'
import test from './modules/test'
import getters from './getters'

Vue.use(Vuex)

export default new Vuex.Store({
    modules: {
        test
    },
    getters
  })

store中的某个模块 test.js

const test = {
    state: {
        phone: {
            name: 'xiaomi',
            price: 3000
        },
        company: 'xm'
    },
    mutations: {
        CHANGE_PHONE: (state, device) => {
            console.log("CHANGE_PHONE = device:"+device)
            if (device == 'hw') {
                state.phone.price = state.phone.price + 1000
                state.phone.name = 'huawei'
                state.phone.company == 'hw'
            } else {
                state.phone.price = state.phone.price - 1000
                state.phone.name = 'xiaomi'
                state.phone.company == 'xm'
            }
            
        },
        CHANGE_COMPANY: (state, device) => {
            console.log("CHANGE_COMPANY = device:"+device)
            state.phone.company == device
          
            
        },
    },
    actions: {
        toggleDevice({ commit }, device) {
            console.log("device:"+device)
            commit('CHANGE_PHONE', device)
        },
        toggleCompany({ commit }, device) {
            console.log("device:"+device)
            commit('CHANGE_COMPANY', device)
        }

    }
}

export default test

getter.js

const getters = {
    phone: state => state.test.phone
  }
  export default getters
  

页面模板调用index.vue

<template>
  <div>
    这是一个layout{{userame}}
    <input v-model="company">
    <a @click="handleLogin">点击我</a>
  </div>
</template>

<script>
import { getTest } from "@/api/login";

export default {
  name: "Layout",
  data() {
    return {
      userame: this.$store.state.test.phone.name
    };
  },
  computed: {
    company: {
      get() {
        return this.$store.state.test.company;
      },
      set(value) {
        this.$store.commit("CHANGE_COMPANY", value);
      }
    }
  },
  methods: {
    handleLogin() {
      console.log("xxxxxxxxxxxxxxxx=="+this.company);
      console.log(this.$store.getters.phone);
    //   this.$store.commit("CHANGE_PHONE", this.$store.state.test.company);
      this.$store.dispatch('toggleDevice',this.company)
      console.log(this.$store.getters.phone);
      // getTest(this.userame).then(response => {
      //     alert(response.data);
      //      this.$store.dispatch('increment')
      // })
    }
  }
};
</script>

其他知识

相关文章

  • vuex学习

    使用vuex也有一段时间了,今天总结一下vuex的使用 vuex有5个核心概念state getter mutat...

  • vuex记录

    前言 官方文档讲vuex讲的很清楚了,本文用于总结vuex的简单使用 简介 Vuex 是一个专为 Vue.js 应...

  • vuex总结

    vuex的核心思想:把存在于一个或一定范围内的对应关系(单向数据流或数据流)抽离出来,在项目全局中是使用。 最简单...

  • VUEX总结

    集中的状态存储,主要是为了解决组件间共享数据状态时,传值不便问题 1、特点 vuex的核心是store,数据都存储...

  • Vuex总结

    简介 简单来讲:共享的状态管理器,比如你想修改某些背景色,设备状态、cookie等信息的时候你可以考虑用它。其他的...

  • Vuex总结

    【应用场景】 简单来说就是状态管理,存放一些全局参数,供各个组件调用,并且这是响应式的。这里补充下,个人认为Vue...

  • Vuex 总结

    1. vuex简介 vuex是专门用来管理vue.js应用程序中状态的一个vue插件。他的作用是将应用中的所有状态...

  • vuex总结

    什么是父子组件 使用components,引入的组件为子组件,子组件所在的当前组件为父组件。 vue中数据共享方式...

  • Vuex 2.0 学习笔记(二):源码阅读

    上一章总结了 Vuex 的框架原理,这一章我们将从 Vuex 的入口文件开始,分步骤阅读和解析源码。由于 Vuex...

  • vuex学习总结

    它是一种flux实现 什么是flux 具体是怎么设计的 什么是react,如何实现 用到了哪些js2005语法

网友评论

      本文标题:Vuex总结

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