簡體   English   中英

... Vue-TypeScript 應用程序中的 mapActions 導致錯誤

[英]…mapActions in Vue-TypeScript application cause error

我正在嘗試在 Vue-Typescript 應用程序中創建身份驗證功能。 對於這個特性,我創建了一個 Vuex 存儲模塊“AuthModule”。 我已將 Vuex 操作映射到 class 組件方法。 但是當我從其他 class 方法調用這些操作時,它會引發如下錯誤。

Property 'login' does not exist on type 'AuthView'.
    70 |                 rememberMe: this.rememberMe
    71 |             };
  > 72 |             this.login(user)
       |              ^
    73 |                 .then(() => this.$router.push('/home'))
    74 |                 .catch(err => console.log(err))
    75 |         }

AuthModule 商店

import {Action, Module, Mutation, VuexModule, getModule} from "vuex-module-decorators";
import Api from "@/helpers/Api";
import axios from "axios";
import User from "@/dao/User";
import store from '@/store'


@Module({dynamic: true, store, name: "AuthModule", namespaced: true})

class AuthModule extends VuexModule {
    public token = localStorage.getItem('token') || '';
    public status = '';
    public user = new User();
    public isLoggedIn = false;

    @Mutation
    public authRequest(): void {
        this.status = 'loading'
    }

    @Mutation
    public authSuccess(token: string, user: User): void {
        this.token = token;
        this.user = user;
        this.isLoggedIn = true;
    }

    @Mutation
    public authError() {
        this.status = 'error'
    }

    @Action({rawError: true})
    public login(user: any) {
        return new Promise((resolve, reject) => {
            this.context.commit('authRequest');
            return Api.post('/auth/login', user)
                .then(resp => {
                    const token = resp.data.accessToken;
                    const user = new User();
                    user.firstName = resp.data.firstName;
                    user.lastName = resp.data.lastName;
                    user.UserId = resp.data.userId;
                    localStorage.setItem('token', token);
                    Api.defaults.headers.common['Authorization'] = 'Bearer ' + token;
                    this.context.commit('authSuccess', {token, user});
                    resolve(resp)
                })
                .catch(err => {
                    this.context.commit('authError');
                    localStorage.removeItem('token');
                    reject(err)
                })
        })
    }
}

存儲 index.ts

import Vue from 'vue'
import Vuex from 'vuex'
import {authModule} from "./auth/AuthModule";

Vue.use(Vuex);


export default new Vuex.Store({
    modules: {
        authModule
    }
})

登錄.ts

<script lang="ts">
    import {Component, Vue} from 'vue-property-decorator'
    import {mapActions} from "vuex";

    @Component({
        methods: {
            ...mapActions('authModule', ['login'])
        }
    })
    export default class AuthView extends Vue {
        private email = "";
        private password = "";
        private rememberMe = "";

        public login(user:object) {};

        performLogin() {
            const user = {
                email: this.email,
                password: this.password,
                rememberMe: this.rememberMe
            };
            this.login(user)
                .then(() => this.$router.push('/home'))
                .catch(err => console.log(err))
        }
    }
</script>

如果有人遇到這樣的問題,請提出解決方案。

在您的AuthModule.store中,您應該使用getModule來導出商店,即:

export const authModuleStore = getModule(AuthModule);

然后,您可以直接在組件中導入authModuleStore並直接引用其方法:

import { authModuleStore } from '/path/to/authModule.store';

export default class AuthView extends Vue {
    private email = "";
    private password = "";
    private rememberMe = "";

    public login(user:object) {};

    performLogin() {
        const user = {
            email: this.email,
            password: this.password,
            rememberMe: this.rememberMe
        };
        authModuleStore .login(user)
            .then(() => this.$router.push('/home'))
            .catch(err => console.log(err))
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM