vuex

从头开始学习Vuex

旧时模样 提交于 2019-12-04 01:52:49
一、前言 当我们的应用遇到多个组件共享状态时,会需要多个组件依赖于同一状态抑或是来自不同视图的行为需要变更同一状态。以前的解决办法: a.将数据以及操作数据的行为都定义在父组件; b.将数据以及操作数据的行为传递给需要的各个子组件(有可能需要多级传递) 传参的方法对于多层嵌套的组件将会非常繁琐,并且对于兄弟组件间的状态传递无能为力。在搭建下面页面时,你可能会对 vue 组件之间的通信感到崩溃 ,特别是非父子组件之间通信。此时就应该使用vuex,轻松可以搞定组件间通信问题。 二、什么是Vuex Vuex 是一个专为 Vue.js 应用程序开发的状态管理模式。它采用集中式存储管理应用的所有组件的状态,并以相应的规则保证状态以一种可预测的方式发生变化。这里的关键在于集中式存储管理。 简单来说,对 vue 应用中多个组件的共享状态进行集中式的管理(读/写) 。 三、Vuex的原理是什么 1.简要介绍Vuex原理 Vuex实现了一个单向数据流,在全局拥有一个State存放数据,当组件要更改State中的数据时,必须通过Mutation进行,Mutation同时提供了订阅者模式供外部插件调用获取State数据的更新。而当所有异步操作(常见于调用后端接口异步获取更新数据)或批量的同步操作需要走Action,但Action也是无法直接修改State的

[vuex]

梦想与她 提交于 2019-12-03 23:43:01
1. 需求: 数据 多组件共享 2. 涉及: 数据定义 - /vuex/store/ index.js /** * @file /vuex/store/index.js * @author v_wangyuan01 */ import Vue from 'vue'; import Vuex from 'vuex'; import mutations from './mutations'; import actions from './actions'; import getters from './getters'; Vue.use(Vuex); export default new Vuex.Store({ state: { // 状态机 - 数据多组件共享 userInfo: { userName: '', isLogin: false } }, actions, // 行为『检查用户是否登录 isUserLogin』 mutation, // 执行 如果用户登录, 写入 true; 否则写入 false getters }); mutation-type.js // actions.js 和 mutations.js 的通信约定 /** * @file 封装 * @author v_wangyuan01 */ export const CHANGE_CUR_USER_NAME

Vuex getter not updating

青春壹個敷衍的年華 提交于 2019-12-03 23:27:32
I have the below getter: withEarmarks: state => { var count = 0; for (let l of state.laptops) { if (l.earmarks.length > 0) { count++; } } return count; } And in a component, this computed property derived from that getter: withEarmarks() { return this.$store.getters.withEarmarks; }, The value returned is correct, until I change an element within the laptops array, and then the getter doesn't update. In your case state.laptops.earmarks is an array, and you are manipulating it by its array index state.laptops[index] . Vue is unable to react to mutations on state arrays (by index). The

vuexjs getter with argument

放肆的年华 提交于 2019-12-03 23:19:26
问题 Is there a way to pass parameter into getter of vuex store ? Something like: new Vuex.Store({ getters: { someMethod(arg){ // return data from store with query on args } } }) So that in component I could use <template> <div> <p>{{someMethod(this.id)}}</p> </div> </template> <script lang="ts"> import { mapGetters } from "vuex" export default { props: ['id'], computed: mapGetters(['someMethod']) } } </script> but in vuex first argument is state and second is other getters . Is it possible? 回答1:

Accessing rootState in Vuex getter

为君一笑 提交于 2019-12-03 22:40:24
How do you access rootState in getters? const getters = { getParams: rootState => { return rootState.route.params }, } The above doesn't work. How is this done? If this getter is in a module rootState is the third arguments. const getters = { getParams: (state, getters, rootState) => { return rootState.route.params } } 来源: https://stackoverflow.com/questions/42171276/accessing-rootstate-in-vuex-getter

Why the value from input is not passed to VUEX

ⅰ亾dé卋堺 提交于 2019-12-03 18:15:58
问题 I can't transfer the value from input to the store . When I click on the add item button, I need to create a block with its delete button and the text entered in the input . And then save it all in localstorage. But now I am creating only a new block without text. Please help me fix my code to make it work. Here's how it should work But how it works now What I'm doing wrong? How do I transfer the value from Input to Vuex? Here is my code <template> <f7-block-title>Some items</f7-block-title>

Vue: How to use component prop inside mapFields

两盒软妹~` 提交于 2019-12-03 17:22:24
I have general component and vuex store. For easy two-way binding I use vuex-map-fields . On component side it has mapFields method which creates get&set with mutations. I want to pass namespace from vuex module with props but it seems to be impossible. <my-component namespace="ns1" /> // my-component code export default { props: ["namespace"], computed: { ...mapFields(??this.namespace??, ["attr1", "attr2"]) } } Of course, there is no way to use this in such way so we don't have access to props. How can I specify namespace as prop in such case? The problem (as you probably gathered) is that

How to deep clone the state and roll back in Vuex?

為{幸葍}努か 提交于 2019-12-03 16:50:35
In Vuex I would like to take a snapshot / clone of an object property in the tree, modify it, and later possibly roll back to the former snapshot. Background: In an application the user can try out certain changes before applying them. When applying the changes, they should effect the main vuex tree. The user can also click «cancel» to discard the changes and go back to the former state. Example: state: { tryout: {}, animals: [ dogs: [ { breed: 'poodle' }, { breed: 'dachshund' }, ] ] } User enters »Try out« mode and changes one breed from poodle to chihuahua . She then decides either to

data variable not being updated from watcher on computed property in Vue.js with Vuex

二次信任 提交于 2019-12-03 15:11:56
Fiddle: https://jsfiddle.net/mjvu6bn7/ I have a watcher on a computed property, which has a dependency on Vuex store variable, which is being set asynchronously. I am trying to set data variable of Vue component, when this computed property is changing, but this is not happening. Here is Vue component: new Vue({ el: '#app', store, data : { myVar : "" }, beforeMount() { this.$store.dispatch('FETCH_PETS', { }).then(() => { console.log("fetched pets") }) }, computed:{ pets(){ return this.$store.state.pets } }, watch:{ pets: (pets) => { console.log("Inside watcher") this.myVar = "Hey" } } }); Here

手摸手教你在vue-cli里面使用vuex,以及vuex简介

十年热恋 提交于 2019-12-03 14:59:54
写在前面: 这篇文章是在vue-cli里面使用vuex的一个极简demo,附带一些vuex的简单介绍。有需要的朋友可以做一下参考,喜欢的可以点波赞,或者关注一下,希望可以帮到大家。 本文首发于我的个人blog: obkoro1.com 引入步骤 我创建了一个新的vue-cli里面什么东西都没有,只引用了vuex,这里是 码云地址 ,可以下载下来,然后 npm install 、 npm run dev 试试看,里面vuex的使用地方也全都注释了一遍。 安装 npm install vuex --save 在src目录下创建文件夹vuex 该文件夹包含以上文件,创建好了之后,我们一个一个文件来说里面都有什么东西。 vuex/index.js import Vue from 'vue'; import Vuex from 'vuex'; import status from './modules/status/index';//引进模块 Vue.use(Vuex); export default new Vuex.Store({ modules: { //Vuex 允许我们将 store 分割成模块(module)。每个模块拥有自己的 state、mutation、action dataStatus:status,//访问这里面数据的时候要使用'dataStatus' }, });