简体   繁体   English

Nuxt Vue Typescript 全局插件在脚本语言 ts 中不可用

[英]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.

相关问题 使用 TypeScript 设置时,Vue 3 模板中的 ref() 出现掉毛错误<setup lang="ts" script></setup> - Linting errors for ref()'s within Vue 3 templates when using setup with TypeScript <setup lang="ts" script> 将查询选择器结果转换为 vue 组件内的 HTMLElement 时出现编译错误<script lang="ts"> code - Compilation error on casting query selector result to HTMLElement inside a vue component's <script lang="ts"> code 由于 lang="ts" 属性,基于 typescript 的 vue 文件缺少源映射 - Sourcemap missing for typescript based vue file due to lang="ts" attribute 如何在 Vue 中使用 ESLint 规则在脚本标签中强制执行 lang="ts" - How to enforce lang="ts" in script tags with an ESLint rule in Vue Nuxt + TypeScript 类型在布局和插件中不起作用 - Nuxt + TypeScript types not working in layouts and plugins 在Vue / Nuxt中对局部属性进行突变(打字稿) - Mutating local properties in Vue / Nuxt (typescript) 是否有必要在所有组件中导入 vue? Nuxt 和打字稿 - is it necessary to import vue in all components? Nuxt and typescript Nuxt / Vue 2 + Typescript – VS 代码道具自动完成 - Nuxt / Vue 2 + Typescript – VS Code props Autocomplete 无法在 Vue.js 应用程序的 main.ts 中导入打字稿模块 - Cannot import typescript modules inside main.ts on Vue.js app Typescript 显示“this”错误 Object 说 TS2532: Object 在 vue 方法中可能是“未定义” - Typescript displays error for "this" Object saying TS2532: Object is possibly 'undefined' inside of vue methods
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM