• vue项目中的js文件使用vuex


    1. 使用场景:假设有一个接口,需要在很多页面获取一遍并且将接口的返回值保存起来,这样就能使用vuex,将值保存在vuex
    2. 实现:vuex中新建firmModule.js文件,编写存储值的代码,utils/getFirmData.js用来调接口获取值并将值存储在vuex中,xxx.vue中调取getFirmData.js中的方法,并且在watch中监听vuex的值实现给xxx.vueform赋值

    store/modules/firmModule.js

    1. const state = {
    2. firmData: {
    3. firmId: undefined,
    4. firmName: undefined,
    5. },
    6. };
    7. const mutations = {
    8. SET_FIRM_DATA(state, firmData) {
    9. state.firmData = firmData;
    10. },
    11. };
    12. const actions = {
    13. setFirmData({ commit }, firmData) {
    14. commit("SET_FIRM_DATA", firmData);
    15. },
    16. };
    17. export default {
    18. namespace: true,
    19. state,
    20. mutations,
    21. actions,
    22. };

    store/index.js

    1. import Vue from "vue";
    2. import Vuex from "vuex";
    3. import firmModule from "@/store/modules/firmModule";
    4. Vue.use(Vuex);
    5. const store = new Vuex.Store({
    6. modules: {
    7. firmModule,
    8. },
    9. });
    10. export default store;

    utils/getFirmData.js

    1. /**
    2. * @Event 获取企业信息
    3. * @description: 最终数据存储到vuex中 store.state.firmModule.firmData
    4. * @author: mhf
    5. * @time: 2023-11-16 21:41:05
    6. **/
    7. import { queryUserId } from "@/api/enterpriseManage/riskControl.js"; // 接口
    8. import { isEmptyArray } from "@/utils/publicFun"; // 判断是否是空数组
    9. import store from "@/store";
    10. export async function getFirmData() {
    11. try {
    12. const user = localStorage.getItem('user');
    13. if (!user) {
    14. throw new Error('无法获取用户信息');
    15. }
    16. const userId = JSON.parse(user).user.userId;
    17. const res = await queryUserId({ userId })
    18. if (isEmptyArray(res.data)) {
    19. throw new Error('查询结果为空')
    20. }
    21. await store.dispatch('setFirmData', res.data[0])
    22. } catch (error) {
    23. throw new Error('失败:' + error)
    24. }
    25. }
    26. // isEmptyArray方法如下:
    27. /**
    28. * @Event 判断是否是空数组
    29. * @description:
    30. * @author: mhf
    31. * @time: 2023-11-16 17:26:31
    32. **/
    33. export function isEmptyArray(arr) {
    34. if (Object.prototype.toString.call(arr) !== "[object Array]") return;
    35. return arr.length === 0;
    36. }

    xxx.vue

    1. import { getFirmData } from "@/utils/getFirmData";
    2. // 监听vuex中的数据给formData赋值
    3. watch: {
    4. "$store.state.firmModule.firmData"(obj) {
    5. this.$set(this.formData, "firmName", obj.firmName);
    6. this.$set(this.formData, "firmId", obj.id);
    7. }
    8. },
    9. created() {
    10. getFirmData();
    11. },

    注意 eslint检测async await配置如下

    .eslintrc.js
    1. // ESlint 检查配置
    2. module.exports = {
    3. root: true,
    4. parserOptions: {
    5. parser: 'babel-eslint',
    6. sourceType: 'module',
    7. "ecmaVersion": 2020, // 需要此项
    8. },
    9. env: {
    10. browser: true,
    11. node: true,
    12. es6: true
    13. },
    14. extends: [],
    15. // add your custom rules here
    16. // it is base on https://github.com/vuejs/eslint-config-vue
    17. rules: {}
    18. }

  • 相关阅读:
    java基础运算符 之 关系运算符
    5. Makefile项目管理
    sql数据库入门(1)
    MYSQL锁机制 - 锁的简述 | 索引对行级锁的影响
    C#使用 MQTTnet 快速实现 MQTT 通信
    小米root以及面具的使用
    遥感影像智能处理比赛收集
    【AI应用】NVIDIA Tesla V100-PCIE-32GB的详情参数
    多线程的三种创建方式&守护线程
    网红探店一般怎么收费?找网红探店该如何筛选博主
  • 原文地址:https://blog.csdn.net/m0_74149462/article/details/134452372