- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我已经在 Vue 中构建了一个应用程序。它由许多独立的模块组成,每个模块对应一个路由,并有一个顶级组件(以及许多子组件/子组件)。每个模块都有自己的存储、操作、突变和 getter ,以及在组件的 created() Hook 中调度的 API 调用以获取必要的数据。
这是我的应用程序的结构:
Candidates.vue
created() {
this.$store.dispatch('$_candidates/getAllCandidates');
},
/modules/candidates/_store/actions.js
import api from '../_api';
const getAllCandidates = (context) => {
api.fetchAllCandidates
.then((response) => {
context.commit('ALL_CANDIDATES_FETCHED', response.data.candidate_list);
})
.catch((error) => {
// eslint-disable-next-line
console.error(error);
});
};
/modules/candidates/_api/index.js
import { fetchData } from '@/helpers';
const allCandidatesEndpoint =
'https://myapiendpoint.io/candidates/list/all';
const fetchAllCandidates = fetchData(allCandidatesEndpoint, 'get');
export default {
fetchAllCandidates,
};
在 App.vue 的 beforeCreate() 钩子(Hook)中,我有一个辅助函数来一次性注册所有应用程序模块。我这样做是将模块存储导入帮助程序文件,然后注册它们。这是我的帮助文件:
helpers.js
import axios from 'axios';
import { store } from '@/store/store';
import candidatesStore from './modules/candidates/_store';
import dashboardStore from './modules/dashboard/_store';
import eventsStore from './modules/events/_store';
import loginStore from './modules/login/_store';
function fetchData(endpoint, requestType, requestBody) {
const apiToken = store.state.apiToken;
delete axios.defaults.auth;
return axios.request({
method: requestType,
data: requestBody,
url: endpoint,
headers: {
'server-token-id': apiToken,
},
})
.then(response => response)
.catch(error => error);
}
/* Register all of the Vuex modules we'll need to manage application state */
function registerVuexModules() {
store.registerModule('$_candidates', candidatesStore);
store.registerModule('$_dashboard', dashboardStore);
store.registerModule('$_events', eventsStore);
store.registerModule('$_login', loginStore);
}
function unregisterVuexModules() {
store.unregisterModule('$_candidates', candidatesStore);
store.unregisterModule('$_dashboard', dashboardStore);
store.unregisterModule('$_events', eventsStore);
store.unregisterModule('$_login', loginStore);
}
export {
fetchData,
registerVuexModules,
unregisterVuexModules,
};
...然后我像这样导入到 App.vue 中:
beforeCreate() {
registerVuexModules();
},
但是,每个模块的导入都会以某种方式触发 API 调用(使用 fetchData 函数),它会返回 401。我已经通过注释掉 helpers.js 的各个部分来确认这一点——这绝对是导入而不是自己发挥作用。
当我删除将模块存储导入到 helpers.js 中时,不会尝试对该模块的顶级组件进行 API 调用。对我来说奇怪的部分是:
即使应该触发这些 API 调用的操作仅在每个模块的顶级组件中调度,但每次我重新加载登录页面时都会尝试 API 调用,甚至在创建这些组件之前;
Vue-dev-tools 没有为正在调度的 Action 注册相应的事件;
如果我从帮助程序文件中删除所有商店导入,则不会发生任何 API 调用。
我已经尝试更改我在 vue-router 中导入的格式,以便延迟加载组件,因为我认为这可能是问题所在。 bundle 的大小减小了,但它没有修复幻影 API 调用。这就是我导入它们的方式...
/router/index.js
import Vue from 'vue';
import Router from 'vue-router';
import axios from 'axios';
import { store } from '../store/store';
/* Lazy load all of the components required for the routes */
const Login = () => import(/* webpackChunkName: "login" */
'@/modules/login/Login');
const Dashboard = () => import(/* webpackChunkName: "dashboard" */
'@/modules/dashboard/Dashboard');
...
const router = new Router({
routes: [
{
path: '/',
name: 'root',
component: Login,
},
{
path: '/login',
name: 'login',
component: Login,
},
{
path: '/dashboard',
name: 'dashboard',
component: Dashboard,
beforeEnter: (to, from, next) => {
guard(to, from, next);
},
},
...
任何人都可以解释这种行为或我错过了什么吗?
最佳答案
据我所知,你有这条线
const fetchAllCandidates = fetchData(allCandidatesEndpoint, 'get');
这意味着每次您 import
时,它都会运行 fetchData
函数并返回结果。
也许你打算改为这样做。
const fetchAllCandidates = function ()
{
return fetchData(allCandidatesEndpoint, 'get');
}
关于javascript - Vue.js-Vuex : why is an action dispatched when I import my module stores to a helper file?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52442473/
我刚刚开始使用 Vuex,但遇到了问题(这可能是我的一些无能为力的语法错误)。我有一个用户喜欢 like_items 并且有一个网络调用,它与一个项目不同。 mutations: { SET_TOGG
我刚开始在这里学习 Vuex。到目前为止,我一直将共享数据存储在 store.js 中。文件和导入store在每个模块中,但这越来越烦人,我担心会发生变异。 我正在努力解决的是如何使用 Vuex 从
我正在创建一个使用 Fabric.js 来管理 Canvas 的 Vue 应用程序。 我想使用 Vuex 管理 Canvas 状态。为此,我将 fabric canvas 实例设置为 vuex 状态变
我正在构建一个 Nuxt 应用程序,我正在从我在本地主机上运行的节点后端获取一些数据。 我有一个插件 getApps.js export default ({ store }) => { st
我想安装 Vuex 但出现错误。 // error npm ERR! code ERESOLVE npm ERR! ERESOLVE unable to resolve dependency tree
我使用 vuex-persistedstate 包( https://github.com/robinvdvleuten/vuex-persistedstate )在浏览器上保持数据状态。 我使用 A
假设我有一个简单的 Vuex 状态来保存食谱: const state = { recipe: { id: '', name: "", ingr
目前,我有一个具有 2 个状态属性的商店 - 版本和冠军 + 2 个发出 GET 请求然后提交到状态的操作。第二个 GET 请求 URL 需要包含我从第一个 GET 请求获取的版本,如下所示: axi
我有一个基于 Vue 和 Vuex 的小应用程序。它只是一个包含类似项目的表格 {{ item.name }} Edit Items 数组是使用 getter 从
我有一个 Vuex getter,我从应用程序中的各个组件调用它。但是,我发现在调用 getter 之前需要稍微复杂一些的逻辑,所以我使用了 Vuex Action。如何使用操作中的参数调用 gett
为什么会出现此错误: Error [vuex] Do not mutate vuex store state outside mutation handlers. 这是什么意思? 当我尝试输入编辑输入
在 Nuxt 文档(here)中,它说“您可以选择将模块文件分解为单独的文件:state.js、actions.js、mutations.js 和 getters.js。” 我似乎找不到任何示例来说明
我正在使用 TypeScript 构建一个简单的 vue 项目。我在我的 vue 项目中编写了 vuex 商店,该商店有一个名为“计算”的子模块。但是一旦创建了包含子模块的主存储,就会发生奇怪的错误(
我完全熟悉 Vuex,但我对 Vue 组合 API 一无所知。这到底是怎么回事,它和Vuex有什么区别?它涵盖了 Vuex 中的哪些差距,以及何时何地使用它们更好? 感谢您的帮助。 最佳答案 首先,
简短的问题:是否可以通过 Nuxt Auth 模块手动更新 Vuex 中的用户数据? 为什么我有这个问题:我的问题是这个。我在用户文档中的 MongoDB 中保存了一些 Likes/Follows。我
尝试将带有 vuex-module-decorators 的存储模块加载到初始化程序中时出现此错误: vuex.esm.js?2f62:261 Uncaught TypeError: Cannot r
我有一个 VueX 商店,有两个模块,user.js 和merchant.js,顶层是index.js。 user.js 中的 getter 是: 重构 const getters = { s
我在我的代码库中偶然发现了这个错误,并试图看看是否有人可以修复它。 以下是我的 listings/actions.js export const fetchFeaturedListings = ({
我正在尝试将 Vuex 与 Vue 3 一起使用。这是我在 main.js 中的代码: import { createApp } from 'vue'; import App from './App.
我在我的代码库中偶然发现了这个错误,并试图看看是否有人可以修复它。 以下是我的 listings/actions.js export const fetchFeaturedListings = ({
我是一名优秀的程序员,十分优秀!