首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Vuex不装载

Vuex不装载
EN

Stack Overflow用户
提问于 2021-07-06 18:45:29
回答 1查看 167关注 0票数 0

我已经把一种全新版本的Vuex装进了一台百拉威尔/vue。

我把商店导入app.js

代码语言:javascript
复制
import store from './store';

然后我在这里用它

代码语言:javascript
复制
const app = new Vue({
    el: '#app',
    store,
    components: { App },
    router
});

然后在我的商店/index.js文件中

代码语言:javascript
复制
// import dependency to handle HTTP request to our back end
import Vue from 'vue';
import Vuex from 'vuex';
import axios from 'axios';

//load Vuex
Vue.config.devtools = true;
Vue.use(Vuex);

//to handle state
const state = {
    videos: [],
    test: ''
}

//to handle state
const getters = {}

//to handle actions
const actions = {
    getVideos({commit}) {
        axios.post('https://jsonplaceholder.typicode.com/posts')
            .then(response => {
                commit('SET_POSTS', response.data)
            })
    }
}

//to handle mutations
const mutations = {
    SET_POSTS(state, payload) {
        state.videos = payload
    }
}

//export store module
export default {
    state,
    getters,
    actions,
    mutations
}

我认为运行npm运行手表并将其全部构建完毕,我得到以下错误

“没有检测到Veux存储”

也是

代码语言:javascript
复制
this.$store.dispatch('getVideos');

在挂载钩子中出现错误:"TypeError: this.$store.dispatch不是函数“

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2021-07-06 19:05:19

你可以这样用它。

在商店/帐户中

代码语言:javascript
复制
import landlordService from '../service/landlord'
const landlord = JSON.parse(localStorage.getItem('landlord'));
const state = landlord
    ? {
        status: {
            loggedIn: true
        },
    }
    : {
        status: {},

    };


const actions = {
    login({commit}, { email, password }) {
        commit('loginRequest', { email });
        landlordService.auth( email, password)
            .then(landlord => {
                commit('loginSuccess', landlord);
            }).catch(() => {
                commit('loginFailure')
            })
    },
    logout({ commit }) {
        localStorage.removeItem('landlord')
        commit('logout');
    },
};
const mutations = {
    loginRequest(state, landlord) {
        state.status = { loggingIn: true };
        state.landlord = landlord;
    },
    loginSuccess(state, data) {
        state.status = { loggedIn: true };
        state.landlord = {
            id: data.id,
        };
    },
    loginFailure(state) {
        state.status = {};
        state.landlord = null;
    },
    logout(state) {
        state.status = {};
        state.landlord = null;
    },
};
export default {
    namespaced: true,
    state,
    actions,
    mutations
};

尤其是您错过了商店/index.js

代码语言:javascript
复制
import Vue from 'vue';
import Vuex from 'vuex';
import account  from './account';

Vue.use(Vuex);

export default new Vuex.Store({
    modules: {
        account
    }
});
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/68275937

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档