[英]Nuxt Vue Typescript global plugins unavailable inside script lang ts
I am currently converting a nuxt vue js (v2 not v3) project into typescript and I cannot figure out why plugins do not get recognized inside.vue files, while they work in all.ts files.我目前正在将一个 nuxt vue js(v2 不是 v3)项目转换为 typescript,我无法弄清楚为什么插件在 all.ts 文件中工作时无法在.vue 文件中被识别。
As you can see in the code snippets, I also try using bootstrap vue and i18n which are both not recognized inside.vue script tags either, only work in ts files.正如您在代码片段中看到的,我还尝试使用 bootstrap vue 和 i18n,它们在 .vue 脚本标签中也无法识别,只能在 ts 文件中使用。 The only workaround is to create a function that uses them inside the mixin.ts files and extend them that way.
唯一的解决方法是创建一个 function 在 mixin.ts 文件中使用它们并以这种方式扩展它们。 Or import all the plugins manually into each script.
或者手动将所有插件导入每个脚本。 This kind of defeats the point of global plugins, so is there something I am missing, or why are they not recognized by default?
这种破坏了全局插件的观点,所以我缺少什么,或者为什么默认情况下它们不被识别?
I also tried extending Vue directly in the main.vue components instead of a mixin, but the same issue is still there.我也尝试直接在 main.vue 组件中扩展 Vue 而不是 mixin,但同样的问题仍然存在。
Component.vue组件.vue
<template>
...
<div>
{{$testFunction('test')}} this also works, but does not give any type indication or warnings
</div>
</template
<script lang = "ts">
import Component, { mixins } from 'vue-class-component';
import { TableMixin } from '../../mixins/tablemixin';
@Component
class Contacts extends mixins(TableMixin) {
test() {
// Property '$testFunction' does not exist on type 'Contacts'.Vetur(2339)
console.log(this.$testFunction('test'));
}
testMixin() {
// Works fine through mixin...
console.log(this.testMixinFunction('test'));
}
}
export default Contacts;
</script>
mixins/tablemixin.ts mixins/tablemixin.ts
import Vue from 'vue';
import Component from 'vue-class-component';
@Component
export class TableMixin extends Vue {
testMixinFunction(str: string) {
return this.$testFunction(str);
}
}
plugins/helpers.ts插件/helpers.ts
import Vue from 'vue'
declare module 'vue/types/vue' {
interface Vue {
$testFunction(str: string): string;
}
}
Vue.prototype.$testFunction = (str: string): string => {
return str + 'tested';
}
nuxt.config.js nuxt.config.js
plugins: [
'~/plugins/helpers'
]
tsconfig.json tsconfig.json
{
"compilerOptions": {
"target": "ES2018",
"module": "ESNext",
"moduleResolution": "Node",
"lib": [
"ESNext",
"ESNext.AsyncIterable",
"DOM"
],
"esModuleInterop": true,
"allowJs": true,
"sourceMap": true,
"strict": true,
"noEmit": true,
"baseUrl": ".",
"experimentalDecorators": true,
"paths": {
"~/*": [
"./*"
],
"@/*": [
"./*"
]
},
"types": [
"@types/node",
"@nuxt/types",
"nuxt-i18n",
"bootstrap-vue"
]
},
"exclude": [
"node_modules"
]
}
Looks like it was just vetur that was complaining.看起来只是 vetur 在抱怨。 The TS compiler was okay with the global plugins.
TS 编译器对全局插件没问题。 The project was missing the vetur.config.js, so it was looking in the wrong directory for the ts types (project has client and api).
该项目缺少 vetur.config.js,因此它在错误的目录中查找 ts 类型(项目有客户端和 api)。 Simply adding it and pointing to the right place fixed the problem for me.
只需添加它并指向正确的位置就可以解决我的问题。
Solution found here: Vetur documentation在这里找到的解决方案: Vetur 文档
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.