【发布时间】:2020-12-08 09:22:50
【问题描述】:
我正在尝试在我的 vue 3 项目中使用 vue-toast-notification。我面临的问题是 vue 增强类型。我已经看到其他答案并应用了,但它对我不起作用是错误
TS2339: Property '$toast' does not exist on type 'Vue<unknown, {}, {}>'.
我尝试调用 toast 方法的测试组件
import { Vue } from "vue-class-component";
export default class Login extends Vue {
private StoreID = "";
private StorePassword = "";
private errorMessage = "";
testToast(e)
{
e.preventDefault();
const vm = new Vue()
console.log(vm.$toast)
}
}
我的 Tsconfig.json 文件
{
"compilerOptions": {
"target": "esnext",
"module": "esnext",
"strict": true,
"jsx": "preserve",
"importHelpers": true,
"moduleResolution": "node",
"experimentalDecorators": true,
"noImplicitAny": false,
"skipLibCheck": true,
"esModuleInterop": true,
"allowSyntheticDefaultImports": true,
"sourceMap": true,
"baseUrl": ".",
"types": [
"webpack-env"
],
"typeRoots": [
"node_modules/@types",
"typings/vue.d.ts"
],
"paths": {
"@/*": [
"src/*"
]
},
"lib": [
"esnext",
"dom",
"dom.iterable",
"scripthost"
]
},
"include": [
"src/**/*.ts",
"src/**/*.tsx",
"src/**/*.vue",
"tests/**/*.ts",
"tests/**/*.tsx"
],
"exclude": [
"node_modules"
]
}
我的 vue.d.ts 在类型文件夹中。在 main.ts 文件所在项目的根目录下创建了 typings 文件夹。
// 1. Make sure to import 'vue' before declaring augmented types
import Vue from 'vue'
import VueToast from 'vue-toast-notification';
// 2. Specify a file with the types you want to augment
// Vue has the constructor type in types/vue.d.ts
declare module 'vue/types/vue' {
// 3. Declare augmentation for Vue
interface Vue {
$toast: VueToast
}
}
【问题讨论】:
标签: typescript vue.js vuejs3