Vue.js: Nuxt error handling

前端 未结 4 1244
面向向阳花
面向向阳花 2021-02-05 00:33

Struggling a bit to set up error handling with vuex. There seems to be quite a few ways to do so and little documentation on proper error handling. I\'ve been experimenting with

4条回答
  •  Happy的楠姐
    2021-02-05 00:53

    To address the Con from Alternative 2 you can either

    (a) pass in the name of the component or even a reference to the component or

    (b) you can persist the error in the state for the component that made the call. Then in your component you could check if there is an error and display it. For that you could use a mixin to forgo the need for boiler plate.,

    in store/auth.js:

    export const actions = {
        login({ commit }, { email, password }) {
            return this.$axios.post('/api/login', {
                email,
                password,
            }).then((res) => {
                doSomething(res);
                commit('save_to_state', { response: res });
            }).catch((error) => {
                commit('save_to_state', { error });
            });
        },
    }
    

提交回复
热议问题