vuex

关于vuex中的状态变量的思考???

一个人想着一个人 提交于 2019-12-01 07:53:26
store中存取的为整个项目的公共变量,通过设置mutation来改变他们 假设现有如下代码: const store = new Vuex.Store({ state: { userInfo:{ name:'' } }, mutations: { setuserInfo:(state,value) => { const obj = state; obj.userInfo = value } }, getters:{ } }) 我们定义了一个userInfo变量,通过mutation方法, 1,在页面A中 我们开启keepAlive,并在第一次mounted的时候将该页面的userInfo(记录为vara)赋值为this.$store.state.userInfo(记录为varb); 2,然后我们在B页面中改变store中的这一变量:this.$store.commit('setuserInfo',res); 3,当我们回到A页面时,发现userInfo还是改变了,那么问题来了 ,我只在mounted的时候 将varb的一份引用复制给vara,之后在B页面改变的时候 使得 vara的指向另外一个res,按理来说vara还是指向之前的varb,可是结果仍旧是vara改变了 可能的原因解释: 在3步骤中我们声明了个一个赋值操作: const obj = state; obj

localStorage的使用问题

六眼飞鱼酱① 提交于 2019-12-01 07:43:43
问题:在隐身模式、或者用户未启用的情况下,使用localStorage可能会导致浏览器直接报错,怎么办? 方法:使用try-catch包裹 代码示例: import Vue from 'vue' import Vuex from 'vuex' Vue.use(Vuex) let defaultCity = '汉中' try { if (localStorage.city) { defaultCity = localStorage.city } } catch (e) {} export default new Vuex.Store({ state: { city: defaultCity }, mutations: { changeCity (state, city) { state.city = city try { localStorage.city = city } catch (e) {} } }, actions: { changeCity (ctx, city) { // ctx为上下文,city是传来的参数 ctx.commit('changeCity', city) } } }) 来源: https://www.cnblogs.com/VCplus/p/11669655.html

Vuex

穿精又带淫゛_ 提交于 2019-12-01 07:27:35
  说起vuex呢,对这个流程只是有个大概的认识,因为项目相对都不是很大,所以都使用了替代方案来共享数据,比如 EventBus或者 通过vue.observe来简易的共享数据。 observe用起来呢确实很方便,没有vuex那么多的流程,比较简单,但是不能通过vue-devtool来跟踪数据,这是比较蛋疼的。   还是说说vuex吧,对一个东西的认知呢总是遵循一个螺旋上升的曲线,最开始,觉得,又是actions,又是mutations,完全懵逼,但是后来想想,这就是人家的流程规矩,包含了设计者自己的考量了思考,也是有道理的,   贴一张自己画的图,这里面就画的比较清楚了,淡然,其实和官方的图也没啥差别,具体里面的那些用法,还是得遵照官网来    其实想法是比较简单的,数据沿着一个固定的环路来获取和更新,然后引出了vuex中的很多概念。比如store store就是一个对象,里面包含四个对象,对吧,基本就是这么个结构 1 var store={ 2 const state={} 3 const mutation={} 4 const actions={} 5 const getters={} 6 } 来源: https://www.cnblogs.com/ysla/p/11668926.html

vuex

烂漫一生 提交于 2019-12-01 07:09:25
一、vuex 1、简介 (1)参考文档   https://vuex.vuejs.org/zh/ (2)vuex指的是一种状态管理模式,集中式管理所有组件的状态。可以将其理解为一个数据仓库,用来集中式存储管理数据。 (3)单向数据流   State:指的就是状态   View:以声明的方式将 state 映射到视图(比如 {{msg}})。   Actions:指的就是响应动作。   简单的理解就是, view 映射 state 到 视图上, 状态发生变化时,触发 Actions,Actions执行完后,更新 state。 2、为什么出现vuex   没使用vuex前,组件间通信处理比较麻烦。   如下图,五个组件,D是B的子组件,E是C的子组件,B、C是A的子组件。现需要D和E组件间传递数据,则需要通过A、B、C三个组件来进行中转,如果出现更多的组件嵌套,那么逻辑可能会很复杂。   如果可以将这个数据放在某个特定的地方,需要用的时候直接去拿,不用通过组件的嵌套传值获得,那么会很方便,可以理解vuex就是为了解决这个问题而诞生的。 3、什么时候使用vuex (1)小型应用不建议使用,使用的话可能会比较繁琐。 (2)中型、大型单页应用,推荐使用,可以更好地管理组件状态。 二、vuex相关概念 1、State (1)指的是vuex管理的状态对象。 (2)应该是唯一的。 【形如:】

How can I call method in other component on vue.js 2?

和自甴很熟 提交于 2019-12-01 06:46:53
My first component like this : <template> ... </template> <script> export default { ... methods: { addPhoto() { const data = { id_product: this.idProduct} const item = this.idImage this.$store.dispatch('addImage', data) .then((response) => { this.createImage(item, response) }); }, } } </script> If the method addPhoto called, it will call ajax and then it will get response ajax I want to send response ajax and another parameter to method createImage. Method createImage is located in other component (second component) My second component like this : <template> <div> <ul class="list-inline list

Is it bad to commit mutations without using actions in Vuex?

懵懂的女人 提交于 2019-12-01 06:38:48
I have been using Vuex for awhile now, and I have always been following the pattern: Components use Actions to commit Mutations to mutate the Store. I thought this was the proper way to do things considering this diagram from the docs: I came across code where people were committing mutations directly in components, and not even creating simple actions which have no purpose other than to trigger mutations. I even found several examples of this in the Vuex docs. I figured since it's used in the docs this must be an acceptable pattern, and I was wondering if skipping Actions and directly

Property or method not defined on the instance but referenced during render I Vuex

冷暖自知 提交于 2019-12-01 06:05:33
I'm getting the following error. [Vue warn]: Property or method "updateData" is not defined on the instance but referenced during render. Make sure to declare reactive data properties in the data option. As far I can tell by the code, the method is there, so I'm stuck on something that I miss due to my ignorance of Vuex. I've googled the matter and got quite a few answers but none of them made me any wiser what to do. It seems to be something with scope, I'm sensing. I also get the error below but I suspect that it's the same root cause for both so solving the one will resolve the other. [Vue

Update data using vuex

醉酒当歌 提交于 2019-12-01 06:02:21
问题 As Vuex, I'm trying to update an object using form. My code like this. In store: const state = { categories: [] }; //mutations: [mutationType.UPDATE_CATEGORY] (state, id, category) { const record = state.categories.find(element => element.id === id); state.categories[record] = category; } //actions: updateCategory({commit}, id, category) { categoriesApi.updateCategory(id, category).then((response) => { commit(mutationType.UPDATE_CATEGORY, id, response); router.push({name: 'categories'}); }) }

Vuex Mutation running, but component not updating until manual commit in vue dev tools

不想你离开。 提交于 2019-12-01 05:11:16
I have a vue component that I can't get to update from a computed property that is populated from a service call. Feed.vue <template> <div class="animated fadeIn"> <h1 v-if="!loading">Stats for {{ feed.name}}</h1> <h2 v-if="loading">loading {{ feedID }}</h2> </div> </template> <script> export default { data: () => { return { feedID: false } }, computed: { feed(){ return this.$store.state.feed.currentFeed }, loading(){ return this.$store.state.feed.status.loading; } }, created: function(){ this.feedID = this.$route.params.id; var fid = this.$route.params.id; const { dispatch } = this.$store;

How can I call method in other component on vue.js 2?

雨燕双飞 提交于 2019-12-01 04:44:29
问题 My first component like this : <template> ... </template> <script> export default { ... methods: { addPhoto() { const data = { id_product: this.idProduct} const item = this.idImage this.$store.dispatch('addImage', data) .then((response) => { this.createImage(item, response) }); }, } } </script> If the method addPhoto called, it will call ajax and then it will get response ajax I want to send response ajax and another parameter to method createImage. Method createImage is located in other