vuex

How should I handle events in Vuex?

梦想的初衷 提交于 2019-12-03 05:43:58
问题 I am used to using a global event bus to handle cross-component methods. For example: var bus = new Vue(); ... //Component A bus.$emit('DoSomethingInComponentB'); ... //Component B bus.$on('DoSomethingInComponentB', function(){ this.doSomething() }) However, I am building a larger project, which requires global state management. Naturally, I want to use Vuex. While this bus pattern works with Vuex, it seems wrong. I have seen Vuex recommended as a replacement for this pattern. Is there a way

Separating vuex stores for dynamically created components

一笑奈何 提交于 2019-12-03 05:40:34
问题 This was the question got me stuck for a little bit. Unfortunately, I coudn't find answer here ( asking also didn't help ). So after doing some research and asking here and there, it seems that I got the solution to this issue. If you have a question that you already know the answer to, and you would like to document that knowledge in public so that others (including yourself) can find it later. Of course, my answer may not be the ideal one, moreover I know it is not, that's the key point why

How to access Vuex module getters and mutations?

孤街浪徒 提交于 2019-12-03 05:30:28
问题 I'm trying to switch to using Vuex instead of my homegrown store object, and I must say I'm not finding the docs as clear as elsewhere in the Vue.js world. Let's say I have a Vuex module called 'products', with its own state, mutations, getters, etc. How do I reference an action in that module called, say, 'clearWorking Data'? The docs give this example of accessing a module's state: store.state.a // -> moduleA's state But nothing I can see about getters, mutations, actions, etc. 回答1: In your

Vuex基本使用的总结

别等时光非礼了梦想. 提交于 2019-12-03 05:29:51
本文转载于: 猿2048 网站➩ https://www.mk2048.com/blog/blog.php?id=hj101jackj Vuex 背后的基本思想: 把组件的共享状态抽取出来,以一个全局单例模式管理,在这种模式下,我们的组件树构成了一个巨大的“视图”,不管在树的哪个位置,任何组件都能获取状态或者触发行为! 另外,通过定义和隔离状态管理中的各种概念并强制遵守一定的规则,我们的代码将会变得更结构化且易维护。 特点: Vuex 的状态存储是响应式的。当 Vue 组件从 store 中读取状态的时候,若 store 中的状态发生变化,那么相应的组件也会相应地得到高效更新。 你不能直接改变 store 中的状态。改变 store 中的状态的唯一途径就是显式地 提交 (commit) mutation 。这样使得我们可以方便地跟踪每一个状态的变化,从而让我们能够实现一些工具帮助我们更好地了解我们的应用 使用 在 Vue 的单页面应用中使用,需要使用 Vue.use(Vuex) 调用插件。 使用非常简单,只需要将其注入到Vue根实例中。 import Vuex from 'vuex' Vue.use(Vuex) const store = new Vuex.Store({ state: { count: 0 }, getter: { doneTodos: (state,

Can I call commit from one of mutations in Vuex store

天大地大妈咪最大 提交于 2019-12-03 05:12:58
I have a vuex store, like following: import spreeApi from '../../gateways/spree-api' // initial state const state = { products: [], categories: [] } // mutations const mutations = { SET_PRODUCTS: (state, response) => { state.products = response.data.products commit('SET_CATEGORIES') }, SET_CATEGORIES: (state) => { state.categories = state.products.map(function(product) { return product.category}) } } const actions = { FETCH_PRODUCTS: (state, filters) => { return spreeApi.get('products').then(response => state.commit('SET_PRODUCTS', response)) } } export default { state, mutations, actions } I

Vuex: Access State From Another Module

旧巷老猫 提交于 2019-12-03 04:41:17
问题 I want to access state.session in instance.js from records_view.js . How is this accomplished? store/modules/instance.js const state = { // This is what I want to access in records_view.js session: {} }; const getters = { sessionGetter: state => state.session }; store/modules/records_view.js const actions = { getSettingsAction (context, props) { // This is how I'm trying to access session, which doesn't work let session = context.state.instance.session; Api( context, { noun: props.noun, verb:

How to structure api calls in Vue.js?

旧巷老猫 提交于 2019-12-03 02:54:23
问题 I'm currently working on a new Vue.js application. It depends heavily on api calls to my backend database. For a lot of things I use Vuex stores because it manages shared data between my components. When looking at other Vue projects on github I see a special vuex directory with files that handles all the actions, states and so on. So when a component has to call the API, it includes the actions file from the vuex directory. But, for messages for example, I don't want to use Vuex because

Call an action from within another action

廉价感情. 提交于 2019-12-03 02:30:53
问题 I have the following setup for my actions: get1: ({commit}) => { //things this.get2(); //this is my question! }, get2: ({commit}) => { //things }, I want to be able to call one action from within another, so in this example I want to be able to call get2() from within get1() . Is this possible, and if so, how can I do it? 回答1: You have access to the dispatch method in the object passed in the first parameter: get1: ({ commit, dispatch }) => { dispatch('get2'); }, This is covered in the

How to access async store data in vue-router for usage in beforeEnter hook?

核能气质少年 提交于 2019-12-03 02:23:49
How is it possible to access store data in beforeEnter which is retrieved asynchronously via the store action? import store from './vuex/store'; store.dispatch('initApp'); // in here, async data will be fetched and assigned to the store's state // following is an excerpt of the routes object: { path: '/example', component: Example, beforeEnter: (to, from, next) => { if (store.state.asyncData) { // the above state is not available here, since it // it is resolved asynchronously in the store action } } } This is especially important on the first page load or after a page reload, when the init

Difference between Asyncdata vs Fetch

只谈情不闲聊 提交于 2019-12-03 01:32:07
What is the exact difference between fetch and async data. The official documentation says the following: asyncData You may want to fetch data and render it on the server-side. Nuxt.js adds an asyncData method that lets you handle async operations before setting the component data. asyncData is called every time before loading the component (only for page components). It can be called from the server-side or before navigating to the corresponding route. This method receives the context object as the first argument, you can use it to fetch some data and return the component data. Fetch The