javascript - “类型上不存在属性 $notify” - 无法在 Vue 应用程序上使用 npm 包
问题描述
我正在使用这个 npm 包在我的 Vue 应用程序中发送通知。按照说明操作并在 上添加所需的用法后main.ts
,当我尝试使用它的功能时,我不断得到:
Property '$notify' does not exist on type 'Shop'
主要.ts:
import Vue from 'vue'
import Notifications from 'vue-notification'
import App from './App.vue'
Vue.use(Notifications)
new Vue({
render: h => h(App)
}).$mount('#app')
<script lang="ts">
import { Component, Vue } from "vue-property-decorator";
import Character from "./Character.vue";
import Vendor from "./Vendor.vue";
@Component<Shop>({
components: {
Character,
Vendor
},
})
export default class Shop extends Vue {
sellItem(itemID) {
this.$notify({
title: 'Important message',
text: 'Hello user!'
});
}
}
</script>
我尝试在 .vue 文件中导入组件,但它无法识别类型。我究竟做错了什么?我找不到任何解决方案...
谢谢你。
解决方案
添加垫片类型文件
您需要一个导入和重新导出“Vue”类型的文件,它被命名为 vue-file-import.d.ts,但在 Internet 上的其他地方,它通常被称为 vue-shim.d.ts。不管名字如何,需要的内容都是一样的:
// vue-file-import.d.ts
declare module "*.vue" {
import Vue from "vue";
export default Vue;
}
尝试将上述文件放置在 /src
位置。但有时当你四处走动改变东西时它可能不起作用,所以我建议你把它放在/typings
位置内
推荐阅读
- c# - 带有继承对象的 JsonConvert DeserializeObject
- php - 在 symfony JMS PHP 中将单个字符串属性反序列化为对象
- spock - Micronaut @Replaces 用声明式客户端
- python - 使用 unittest RuntimeError 进行异步测试
- python - 如何使用自定义金额创建贝宝付款?
- visual-studio-code - 如何去除VS CODE上的竖线
- python - 如果在另一个数据框中的一组列之间存在匹配项,如何删除 Pandas 数据框中的行?
- javascript - react native : 如何在用户检查 checxbox 并在变量中保存 json 后退出屏幕时捕捉用户
- python - 当第一个字节为零时,添加位会导致错误的数字
- c++ - 有人能解释一下向量的含义吗
()