【问题标题】:Import Constants in Vuex Store在 Vuex Store 中导入常量
【发布时间】:2019-02-10 10:51:42
【问题描述】:

我的 Vue.js 应用程序的 Vuex 存储正在增长,并且由于其中有很多常量而变得有点混乱。我想将这些常量拆分为单独的文件并将它们导入到我的 Vuex 商店store.js。我是 JavaScript 新手,所以我想知道:

  • 如何将这些常量存储在单独的文件中?这些文件中的语法是什么?
  • 如何在store.js 中导入这些常量?这样做的确切语法是什么?

这里是我store.js的当前内容

import Vue from 'vue';
import Vuex from 'vuex';

Vue.use(Vuex);

export const store = new Vuex.Store({
    state: {
        graphqlUrl: 'https://localhost/listof/api/v1/graphql',
        errorObject: {
            flag: false,
            message: ''
        },

        // Data types queries
        queryGetAllDataTypes: `query getAllDataTypes {
            allSysDataTypes(orderBy: NAME_ASC) {
                nodes {
                    id
                    name
                }
            }
        }`,

        // Data for linked list & attributes drodpdown in attribute form
        // Response labels must be formatted according to Treeselect requirements
        queryGetLinkedLists: `query getAllLists {
            allSysLists(orderBy: NAME_ASC) {
                nodes {
                    id:nodeId
                    label:name
                    attributes:sysAttributesByListId {
                        children:nodes {
                            id
                            label:name
                        }
                    }
                }
            }
        }`,

        // Data for linked list & attributes drodpdown in value form
        // Response labels must be formatted according to Treeselect requirements
        queryGetLinkedListValues: `query getAllValues {
            all<GraphQlListName> {
                nodes {
                    id
                    label:<graphQlAttributeName>
                }
            }
        }`,

        // Lists queries and mutations
        queryGetAllLists: `query getAllLists{
            allSysLists(orderBy: NAME_ASC) {
                nodes {
                    id
                    name
                    description
                }
            }
        }`,

        queryGetList: `query getList($id: Int!) {
            sysListById(id: $id) {
                id
                name
                description
                tableName
                sysAttributesByListId {
                    nodes {
                        id
                        name
                        description
                        flagMandatory
                        flagUnique
                        dataTypeId
                        sysDataTypeByDataTypeId { name }
                        linkedAttributeId
                        sysAttributeByLinkedAttributeId {
                            name
                            columnName
                            listId
                            sysListByListId {
                                name
                                tableName
                            }
                        }
                        columnName
                    }
                }
            }
        }`,

        mutationCreateList: `mutation createList($sysList: SysListInput!) {
            createSysList(input: {sysList: $sysList}) {
                sysList {
                    id
                }
            }
        }`,

        mutationUpdateList: `mutation updateList($id: Int!, $sysListPatch: SysListPatch!) {
            updateSysListById(input: {id: $id, sysListPatch: $sysListPatch }) {
                sysList {
                    id
                }
            }
        }`,

        mutationDeleteList: `mutation deleteList($id: Int!) {
            deleteSysListById(input: {id: $id}){
                sysList {
                    id
                }
            }
        }`,

        // Attributes queries and mutations
        queryGetAttribute: `query getAttribute($id: Int!) {
            sysAttributeById(id: $id) {
                id
                name
                description
                flagMandatory
                flagUnique
                dataTypeId
                sysDataTypeByDataTypeId { name }
                linkedAttributeId
                sysAttributeByLinkedAttributeId {
                    name
                    listId
                    sysListByListId { name }
                }
                defaultValue
            }
        }`,

        mutationCreateAttribute: `mutation createAttribute($sysAttribute: SysAttributeInput!) {
            createSysAttribute(input: {sysAttribute: $sysAttribute}) {
                sysAttribute {
                    id
                }
            }
        }`,

        mutationUpdateAttribute: `mutation updateAttribute($id: Int!, $sysAttributePatch: SysAttributePatch!) {
            updateSysAttributeById(input: {id: $id, sysAttributePatch: $sysAttributePatch }) {
                sysAttribute {
                    id
                }
            }
        }`,

        mutationDeleteAttribute: `mutation deleteAttribute($id: Int!) {
            deleteSysAttributeById(input: {id: $id}){
                sysAttribute {
                    id
                }
            }
        }`,

        // Generic query used as template to fetch all values from a list
        queryGetAllValues: `query getAllValues {
            all<GraphQlListName> {
                nodes {
                    id
                    createdDate
                    updatedDate
                    <graphQlAttributeName>
                }
            }
        }`,

        // Generic query used as template to fetch one value from a list
        queryGetValue: `query getValue($id: Int!) {
            <graphQlListName>ById(id: $id) {
                id
                createdDate
                updatedDate
                <graphQlAttributeName>
            }
        }`,

        // Generic mutation used as template to create a new value in a list
        mutationCreateValue: `mutation createValue($<graphQlListName>: <GraphQlListName>Input!) {
            create<GraphQlListName>(input: {<graphQlListName>: $<graphQlListName>}) {
                <graphQlListName> {
                    id
                }
            }
        }`,

        // Generic mutation used as template to update a value in a list
        mutationUpdateValue: `mutation updateValue($id: Int!, $<graphQlListName>Patch: <GraphQlListName>Patch!) {
            update<GraphQlListName>ById(input: {id: $id, <graphQlListName>Patch: $<graphQlListName>Patch }) {
                <graphQlListName> {
                    id
                }
            }
        }`,

        // Generic mutation used as template to delete a value in a list
        mutationDeleteValue: `mutation deleteValue($id: Int!) {
            delete<GraphQlListName>ById(input: {id: $id}){
                <graphQlListName> {
                    id
                }
            }
        }`,
    }
});

【问题讨论】:

    标签: javascript vue.js import vuex


    【解决方案1】:

    最简单的选择是为常量创建一个新文件 (constants.js) 并在那里定义和导出它们,例如:

    export const cat = 'black'
    export const dog = 'brown'
    export const mouse = 'grey'
    

    然后将它们全部导入store.js 中的当前命名空间:

    import * as constants from './constants'
    

    或在需要时选择性地导入它们:

    import { cat, dog } from './constants'
    

    【讨论】:

      【解决方案2】:

      VueX 存储默认是不可变的,只能通过 mutations 进行变异。 Vuex 为此创建了 modules。我只是创建了一个模块,然后将我所有的常量都放在那里而不发生变化。

      来自 vux 文档: https://vuex.vuejs.org/guide/modules.html下方:

      const moduleA = {
        state: { ... },
        mutations: { ... },
        actions: { ... },
        getters: { ... }
      }
      
      const moduleB = {
        state: { ... },
        mutations: { ... },
        actions: { ... }
      }
      
      const store = new Vuex.Store({
        modules: {
          a: moduleA,
          b: moduleB
        }
      })
      
      store.state.a // -> `moduleA`'s state
      store.state.b // -> `moduleB`'s state
      

      使用 nuxt.js 你可以简单地将 constants.js 添加到 store 文件夹中:

      export const state = () => ({
          example: {}
      });
      

      【讨论】:

        猜你喜欢
        • 2021-03-04
        • 2020-03-14
        • 2021-03-18
        • 2020-01-19
        • 2019-12-05
        • 2023-03-21
        • 1970-01-01
        • 2018-11-11
        • 2022-10-06
        相关资源
        最近更新 更多