반응형
Vuex 스토어에서 상수 가져오기
Vue.js 어플리케이션의 Vuex 스토어가 증가하고 있으며, 상수가 많아 약간 혼란스러워지고 있습니다.이러한 상수를 다른 파일로 분할하여 Vuex 스토어로 Import합니다.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
}
}
}`,
}
});
가장 간단한 옵션은 상수에 대한 새 파일을 작성하는 것입니다( ).constants.js
를 정의하고 내보냅니다.다음은 예를 들어 다음과 같습니다.
export const cat = 'black'
export const dog = 'brown'
export const mouse = 'grey'
그런 다음 모든 파일을 의 현재 네임스페이스로 Import합니다.store.js
:
import * as constants from './constants'
또는 필요에 따라 선택적으로 가져올 수 있습니다.
import { cat, dog } from './constants'
VueX 저장소는 기본적으로 불변이며 돌연변이를 통해서만 변환될 수 있습니다.Vuex는 이를 위한 모듈을 만들었습니다.모듈을 만들고 모든 상수를 돌연변이 없이 넣을 수 있습니다.
아래 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를 사용하면 다음과 같은 방법으로 constant.js를 스토어 폴더에 추가할 수 있습니다.
export const state = () => ({
example: {}
});
언급URL : https://stackoverflow.com/questions/54615644/import-constants-in-vuex-store
반응형
'programing' 카테고리의 다른 글
Java에서 현재 연도의 정수 값을 가져옵니다. (0) | 2022.08.14 |
---|---|
MySQL 데이터베이스에 연결할 때 SSL 연결에 대한 경고 (0) | 2022.08.14 |
Gradle - 툴체인을 사용한 플랫폼 'JDK 7 (1.7)'을 타깃으로 하지 못했습니다. (0) | 2022.08.14 |
정적 변수는 C 및 C++ 어디에 저장됩니까? (0) | 2022.08.14 |
사용하지 않는 기능에 대해 경고를 받을 수 있는 방법이 있습니까? (0) | 2022.08.14 |