【发布时间】:2019-08-14 03:32:22
【问题描述】:
我尝试使用类型脚本为 vue 添加 self 插件。
但是当我使用 vue 原型中的方法时,我的方法 $auth 在 myComponent 类型上不存在。我还为插件添加了.d.ts,但我认为他有点不正确,而且我认为在插件中不需要使用安装,还是需要?只是在一些示例中我没有看到安装,但在文档中说 - 需要使用安装。
我的插件
import _Vue from 'vue';
import store from '@/store'
import * as firebase from 'firebase';
export default {
install: (Vue: typeof _Vue, options?: any) => {
const base = firebase.initializeApp(config);
const auth = firebase.auth();
Vue.prototype.$auth = {
login: async (username: string, pass: string) => {
return await auth.signInWithEmailAndPassword(username, pass)
},
logout: async () => {
await auth.signOut()
}
};
auth.onAuthStateChanged((user: any) => {
store.commit('updateUser',{ user })
})
}
}
myPlugin.d.ts
declare module 'vue/types/vue' {
interface Vue {
$auth: {
login: (username: string, pass: string) => Promise<any>
};
}
}
组件
export default class SignUp extends Vue {
email: string = '';
password: string = '';
async onSubmit(){
if ((this.$refs.form as any).validate()) {
const auth = await this.$auth.login(this.email, this.password)
}
}
}
【问题讨论】:
标签: typescript vue.js vuejs2