鍍金池/ 問答/HTML/ 是否可以定義多個mutations

是否可以定義多個mutations

我在store.js里面定義了mutations,在store Module里面也定義mutations,是不是不行?

store.js
import Vue from 'vue'
import Vuex from 'vuex'
import moduleDashboard from './modules/dashboard'
import axios from 'axios'
Vue.use(Vuex)

export default new Vuex.Store({

state: {
    userServerURL: 'http://localhost:8080',
    stationServerURL: 'http://localhost:8080',
    modules: {
        dashboard: moduleDashboard
    },

。。。

mutations: {
    userToActive (state, inputData) {
        this.state.loginSuccessUser = inputData.user
    },

dashboard.js
const moduleDasboard = {

namespaced: true,
state: {
    temperature: 0,
    humidity: 0,
    solar: 0,
    atmosphere: 0,

。。。

mutations: {
    temperature (state, value) {
        state.temperature = value
    },
    humidity (state, value) {
        state.humidity = value
    },
    solar (state, value) {
        state.solar = value
    },
    atmosphere (state, value) {
        state.atmosphere = value
    }
    

用commit調(diào)module的方法

        store.commit('temperature', temperatureValue)
        store.commit('humidity', humidityValue)
        store.commit('solar', solarValue)
        store.commit('dashboard/atmosphere', { 'atmosphere': atmosphereValue }, { root: true })

報錯
[vuex] unknown mutation type: temperature
[vuex] unknown mutation type: humidity
[vuex] unknown mutation type: solar
[vuex] unknown mutation type: dashboard/atmosphere

回答
編輯回答
薔薇花

可以了,再把組件注冊一下
thisObj.registerModule('dashboard', thisObj.state.modules.dashboard)

2018年7月10日 02:11