vuex

Vue状态管理:vuex

怎甘沉沦 提交于 2019-12-05 04:06:50
作用 因为vue是单向数据流,父子组件数据传递为单向流动(父->子) ,子组件向父组件传递需要用$emit方法,兄弟组件之间数据传递通过他们的父元素进行数据交换,如果是页面组件,则通过路由传参,vuex解决的就是所有组件之间的数据交换,事实动态更新。 为什么不用全局变量而要用vuex 因为全局变量只有引用类型(对象、数组)才可以实现实时更新 ,普通类型数据无法实时更新 页面刷新后store中的数据就恢复初始化了怎么办 如刷新页面还要保留的数据就放在浏览器缓存中 如何使用vuex // store.js import Vue from 'vue' import Vuex from 'vuex' Vue.use(Vuex) const portalMod={ //状态数据源,类似于data对象 state:{ pageNum: 2, myNum:2, }, //状态数据操作方法,类似于methods mutations:{ incrementPage (state,n) { state.pageNum+=n }, incrementmyNum(state,n){ state.myNum+=n } }, //状态计算属性,类似于computed,只支持同步 getters:{ compPagenum:function (state,getters) { return state

[Vue] Storage 与 Vuex 的关系

柔情痞子 提交于 2019-12-05 03:24:38
Storage 是 数据的落地 ,如果你想在 页面刷新后,能重新载入 一些必要信息,那么这些信息必然需要存储于 Storage。 Vuex 是单页应用的 全局数据集 ,组件之间可以共享这些数据。 不仅如此,还包括了计算属性特性,这样同时也做到了 全局的数据绑定 。数据并不落地。 两者场景完全不同,但是前端调用关系一般是 Local variable -> Vuex Computed attrubute -> WebStorage. Link: https://www.cnblogs.com/farwish/p/11902299.html 来源: https://www.cnblogs.com/farwish/p/11902299.html

Vue如何在父组件页面的方法事件中控制子组件中的值

旧城冷巷雨未停 提交于 2019-12-05 02:46:13
在Vue中,通常会自定义组件来实现页面上的渲染。页面需求中可能会有需要通过页面点击事件来控制各个板块的值,那么如何在父组件的事件中来控制自定义子组件的值呢?我这里总结了我用到的两种方法。 1、通过vuex存储数据来控制值; 2、通过dom页面调用子组件中的事件。 第一种:通过vuex存储数据来控制值 (1)现在假设一个场景:通过自定义组件写一个年月日或者省市区的控件,然后在父组件中,需要通过 保存 按钮获取子组件的值。还要通过 清空按钮 改变子组件中的值,使其值达到初始值或空值的状态。 代码直接上: 父组件: <time-picker :year="year" :month="month" :day="day"></time-picker> <div class="te_center m_top_20"> <van-button type="default" @click="clearTime">清空</van-button> <van-button type="primary" @click="searchTime">提交</van-button> </div> 子组件: 父组件页面中,外部获取到数据之后,通过设置数据的时间,将值存入vuex中。 通过点击清空按钮,通过清空数据的事件,改变vuex中相应的值。 然后定义store中的js文件: 这样定义了清空和设置的事件之后

Computed property not updating from Vuex store object

安稳与你 提交于 2019-12-05 02:10:20
问题 I have a bunch of class binding statements: :class="{active: isActive('status', status.id)}" Here's the method referred to above: isActive: function (param, value) { if (!this.activeFilters.hasOwnProperty(param) && value === 'all' && param !== 'type') { return true; } ...etc } ...and the computed property the method is looking at: activeFilters() { return this.$store.state.activeFilters; }, Which is in the Vuex state. The problem is, these properties aren't updating when one of the dropdowns

Vue: How to use component prop inside mapFields

删除回忆录丶 提交于 2019-12-05 02:07:59
问题 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

Vue躬行记(9)——Vuex

别来无恙 提交于 2019-12-05 01:49:13
  Vuex是一个专为Vue.js设计的状态管理库,适用于多组件共享状态的场景。Vuex能集中式的存储和维护所有组件的状态,并提供相关规则保证状态的独立性、正确性和可预测性,这不仅让调试变得可追踪,还让代码变得更结构化且易维护。本文所使用的Vuex,其版本是3.1.1。 一、基本用法   首先需要引入Vue和Vuex两个库,如果像下面这样在Vue之后引入Vuex,那么Vuex会自动调用Vue.use()方法注册其自身;但如果以模块的方式引用,那么就得显式地调用Vue.use()。注意,因为Vuex依赖Promise,所以对于那些不支持Promise的浏览器,要使用Vuex的话,得引入相关的polyfill库,例如es6-promise。 <script src="js/vue.js"></script> <script src="js/vuex.js"></script>   然后创建Vuex应用的核心:Store(仓库)。它是一个容器,保存着大量的响应式状态(State),并且这些状态不能直接修改,需要显式地将修改请求提交到Mutation(变更)中才能实现更新,因为这样便于追踪每个状态的变化。在下面的示例中,初始化了一个digit状态,并在mutations选项中添加了两个可将其修改的方法。 const store = new Vuex.Store({ state: {

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

限于喜欢 提交于 2019-12-05 01:40:29
问题 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

Should I store all the data in vuex state

核能气质少年 提交于 2019-12-05 00:48:52
If I use Vuex, is that means it's better to store every data in the vuex/state? I have a little confused, some data I get from API (for example UserDetail), I don't need it be shared in components or I counld easily pass it as props. Should I keep that kind of data in Vuex/State? Why not get the data by Promise or only use Vuex/Action/Promise? Data should be kept as local as possible. That's a general principle that helps reduce complexity . Vuex should handle data that needs to be shared among not-closely-related components. 来源: https://stackoverflow.com/questions/43801873/should-i-store-all

cannot read property 'dispatch' of undefined in Vuex

别等时光非礼了梦想. 提交于 2019-12-04 23:18:34
问题 I'm trying to perform a dispatch on 'logOutUser' in vuex store, and i'm getting the following error message in respone: TypeError: Cannot read property 'dispatch' of undefined deleteUser.vue (the component from which the dispatch action is not working): <template> <v-dialog v-model="openDelete" persistent max-width="500px"> <v-card> <v-card-title> <h4>Delete User</h4> </v-card-title> <v-card-text> <h2>Are You Sure?</h2> <p>Deleting your user is a permanent action.</p> <br> <br> <v-btn color=

How to store non-reactive data in Vuex?

让人想犯罪 __ 提交于 2019-12-04 20:12:51
问题 I have data, that loaded on page once before VueJS-application init, and this data will not change for all time while html-page will not reload (classic CGI application, not SPA). Data example: const nonReactiveObjectWithSomeNestedData = { a: 'a', b: { bb: 'bb', cc: { ccc: 'ccc' }, dd: ['dd1', 'dd2'] } } I am using this data in a few vue-components. It would be handy to store this data in Vuex namespaced module and to use Vuex-getters for wrapping same functionality for different vue