简体   繁体   English

使用 TypeScript 和 FirebaseAuthUI 在 Store 中使用 vue-router 进行 Vue 导航

[英]Vue navigation with vue-router in Store using TypeScript and FirebaseAuthUI

I'm trying to learn Vue.js and have created a Quasar (Vue) project with TypeScript support, using Quasar CLI.我正在尝试学习 Vue.js 并使用 Quasar CLI 创建了一个支持 TypeScript 的 Quasar (Vue) 项目。

I'm using FirebaseAuthUI for authentication and have created a boot-file called firebase.ts.我正在使用 FirebaseAuthUI 进行身份验证,并创建了一个名为 firebase.ts 的引导文件。 FirebaseAuth has a 'onAuthStateChanged', which is called each time a user logs in or out. FirebaseAuth 有一个“onAuthStateChanged”,每次用户登录或注销时都会调用它。 The 'onAuthStateChanged' method is placed as a Store action (handleAuthStateChanged). 'onAuthStateChanged' 方法作为存储操作 (handleAuthStateChanged) 放置。

My problem is that there is no access to this.$router using Typescript in the store.ts file.我的问题是无法访问 this.$router 在 store.ts 文件中使用 Typescript 。 Is it wrong to use navigation from a Store Action?使用商店操作中的导航是错误的吗? Is this a Typescript problem?这是 Typescript 问题吗? Or am I overlooking something?还是我忽略了什么?

Project setup:项目设置:

'handleAuthStateChanged' is registered in App.vue 'handleAuthStateChanged' 在 App.vue 中注册

///App.vue

<script lang="ts">
import { mapActions } from 'vuex';

export default {
  methods: {
    ...mapActions('userStore', ['handleAuthStateChanged'])
  },
  mounted() {
    this.handleAuthStateChanged();
  }
};
</script>
///store.ts

const actions = {
  handleAuthStateChanged({ commit }: { commit: any }) {
    
    firebaseAuth.onAuthStateChanged(user => {
      if (user) {
        // User is logged in
    
        this.$router.push('/myPage'); //$router shows Typescript ERROR, but works???
      } else {
        // User is logged out
        
        this.$route.push('/login'); //$router shows Typescript ERROR, but works???
      }
    });
  },
  logoutUser() {
    firebaseAuth.signOut();
  }
};

Error message in VScode: VScode 中的错误消息:

Property '$router' does not exist on type '{ handleAuthStateChanged({ commit }: { commit: any; }): void; logoutUser(): void; }'.ts(2339)

WHen you use:当你使用:

          this.$router.push('/myPage'); //$router shows Typescript ERROR, but works???

you should first check if the route you are pushing is not already the current one.您应该首先检查您正在推动的路线是否还不是当前路线。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM