typescript - 如何为 vue 插件添加类型?
问题描述
我尝试使用类型脚本为 vue 添加自我插件。
但是当我使用 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)
}
}
}
解决方案
推荐阅读
- javascript - 删除给定子元素之后的所有内容(元素和文本)
- c++ - Eigen 为函数的就地版本和非就地版本给出了不同的结果
- drupal - 我应该使用 drupal 的 graphql 模块吗?
- php - 如何在php中使用双重连接变量获取第三个变量
- html - 带有边框半径的元素在 Microsoft Edge 中显示过渡后的伪影
- java - java char '\249' "不是语句" 和 "未闭合字符文字"
- python - 在 docker 容器中运行 tkinter 并将画布导出为 img
- java - ServerSocket 在它已经连接后如何接受一个 Socket?
- devexpress - 如何在计算字段 xtrareport 中获取名称月份
- sql - 为什么我的 where 语句没有返回结果?