从 rootState vuex 获取一个对象而不是一个数字
Getting an object instead a number from rootState vuex
我正在尝试从表状态中获取 activeTables。
const state = {
activeTable: 0,
tables: [{id:1},{id:2}],
}
当我从另一个模块执行 rootState.tables.activeTable 时,我得到了对象,但我想要这个值。
我试图在 console.log 中更深入地了解树结构,但我无法获得值
const actions = {
async addCommand ({commit, rootState},articles) {
console.log(rootState.tables.activeTable) // getting an object instead a number
commit('ADD_COMMAND', {table_id:rootState.tables.activeTable,list:articles})
await api.insert(articles)
},
}
// store/index.js
import Vuex from 'vuex'
import Vue from 'vue'
import products from './modules/products.js'
import tables from './modules/tables.js'
import command from './modules/command.js'
Vue.use(Vuex)
export default new Vuex.Store({
modules : {
products,
tables,
command
},
})
store/modules/tables.js
const state = {
activeTable: 0,
tables: [{id:1},{id:2}],
}
const getters = {
tables: state => state.tables,
activeTable: state => state.activeTable,
}
const actions = {
setActiveTableId (id) {
this.commit('SET_ACTIVE_TABLE', id)
}
}
const mutations = {
SET_TABLES:(state, tables) => {
state.tables = tables;
},
SET_ACTIVE_TABLE:(state, id) => {
state.activeTable = id
}
}
export default {
state: state,
mutations: mutations,
getters: getters,
actions: actions,
strict: true,
}
store/modules/command.js
const state = {
listCommand: []
}
const getters = {
listCommand: state => state.listCommand
}
const actions = {
async addCommand ({commit, rootState},articles) {
console.log(rootState.tables)
api.setUrl(api.url+'&table=command')
commit('ADD_COMMAND', {table_id:rootState.tables.activeTable,list:articles})
await api.insert(articles)
},
async addOrderLine({commit}, product){
api.setUrl(api.url+'&table=orderline')
await api.insert(product)
}
}
const mutations = {
ADD_COMMAND: (state, articles) => {
state.listCommand.push(articles)
},
SET_LISTCOMMAND:(state, articles) => {
state.listCommand = {table_id:state.activeTable,list:articles};
},
}
export default {
state: () => state,
mutations: mutations,
getters: getters,
actions: actions,
strict: true,
}
我的错误在“store/modules/tables.js”
我忘记添加 {commit} 参数..
before
const actions = {
setActiveTableId (id) {
this.commit('SET_ACTIVE_TABLE', id)
}
}
after
const actions = {
setActiveTableId ({commit},id) {
this.commit('SET_ACTIVE_TABLE', id)
}
}
谢谢大家
我正在尝试从表状态中获取 activeTables。
const state = {
activeTable: 0,
tables: [{id:1},{id:2}],
}
当我从另一个模块执行 rootState.tables.activeTable 时,我得到了对象,但我想要这个值。
我试图在 console.log 中更深入地了解树结构,但我无法获得值
const actions = {
async addCommand ({commit, rootState},articles) {
console.log(rootState.tables.activeTable) // getting an object instead a number
commit('ADD_COMMAND', {table_id:rootState.tables.activeTable,list:articles})
await api.insert(articles)
},
}
// store/index.js
import Vuex from 'vuex'
import Vue from 'vue'
import products from './modules/products.js'
import tables from './modules/tables.js'
import command from './modules/command.js'
Vue.use(Vuex)
export default new Vuex.Store({
modules : {
products,
tables,
command
},
})
store/modules/tables.js
const state = {
activeTable: 0,
tables: [{id:1},{id:2}],
}
const getters = {
tables: state => state.tables,
activeTable: state => state.activeTable,
}
const actions = {
setActiveTableId (id) {
this.commit('SET_ACTIVE_TABLE', id)
}
}
const mutations = {
SET_TABLES:(state, tables) => {
state.tables = tables;
},
SET_ACTIVE_TABLE:(state, id) => {
state.activeTable = id
}
}
export default {
state: state,
mutations: mutations,
getters: getters,
actions: actions,
strict: true,
}
store/modules/command.js
const state = {
listCommand: []
}
const getters = {
listCommand: state => state.listCommand
}
const actions = {
async addCommand ({commit, rootState},articles) {
console.log(rootState.tables)
api.setUrl(api.url+'&table=command')
commit('ADD_COMMAND', {table_id:rootState.tables.activeTable,list:articles})
await api.insert(articles)
},
async addOrderLine({commit}, product){
api.setUrl(api.url+'&table=orderline')
await api.insert(product)
}
}
const mutations = {
ADD_COMMAND: (state, articles) => {
state.listCommand.push(articles)
},
SET_LISTCOMMAND:(state, articles) => {
state.listCommand = {table_id:state.activeTable,list:articles};
},
}
export default {
state: () => state,
mutations: mutations,
getters: getters,
actions: actions,
strict: true,
}
我的错误在“store/modules/tables.js”
我忘记添加 {commit} 参数..
before
const actions = {
setActiveTableId (id) {
this.commit('SET_ACTIVE_TABLE', id)
}
}
after
const actions = {
setActiveTableId ({commit},id) {
this.commit('SET_ACTIVE_TABLE', id)
}
}
谢谢大家