相关疑难解决方法(0)

VueJS/Typescript - 找不到模块“./components/Navigation”或其相应的类型声明

当我将脚本创建为打字稿 (lang="ts") 时,我收到一条错误消息

“找不到模块‘./components/Navigation’或其相应的类型声明(Vetur 2307)。”。

我意识到这只有在我将 lang 设置为 ts 时才会发生,这正是我构建 Vue 应用程序所需要的。

应用程序

<template>
  <Navigation />
</template>

<script lang="ts">
import Navigation from './components/Navigation'; // This is where I get the error message

export default {
  name: 'app',
  components: {
    Navigation,
  }
}
</script>
Run Code Online (Sandbox Code Playgroud)

导航.vue

<template>
  <div id="nav">
    <router-link to="/">Home</router-link> 
    <router-link to="/about">About</router-link> 
    <router-link to="/categories">Categories</router-link> 
    <router-link to="/random">Random</router-link>
  </div>
  <router-view />
</template>

<script lang="ts">
export default {
  name: 'Navigation',
}
</script>
Run Code Online (Sandbox Code Playgroud)

javascript typescript vue.js vuejs2 vuejs3

13
推荐指数
1
解决办法
1万
查看次数

打字稿:无法识别 .d.ts 文件

这个可能非常简单,但我想弄清楚它会发疯。

我在外部 index.ts 文件中有一个非常简单的自执行函数,需要在 window 对象上注册一个全局变量。我想像这样分配它:

(() => {
  window.myCustomGlobal = new MyCustomGlobal();
})()
Run Code Online (Sandbox Code Playgroud)

我在 index.ts 旁边创建了 index.d.ts:

import { MyCustomGlobal} from './classes';

declare interface Window {
  myCustomGlobal: MyCustomGlobal;
}
Run Code Online (Sandbox Code Playgroud)

上面没有错误,一切看起来都很好。应用程序编译正常。

但是,在 IDE 中,我收到一个错误: ts2339: Property 'myCustomGlobal' does not exist on type 'Window'

我一生都无法弄清楚究竟是什么让 Typescript“弄清楚”要包含什么 .d.ts 文件。在某些库中,将 .d.ts 文件与 .js 文件放在一起会导致它被拾取,并且工作正常。事实上,我使用了与上面完全相同的接口和一个全局第三方库,效果很好;似乎我的 IDE 无法接收它。

我已经尝试添加index.d.ts到tsconfig.jsoninclude以及files没有运气。

有人可以解释一下,就像对孩子一样,是什么导致 TS 拿起声明文件?有时我觉得我明白了,然后在像这样愚蠢的简单例子中,我觉得自己像个白痴。

谢谢

typescript

6
推荐指数
2
解决办法
7601
查看次数

标签 统计

typescript ×2

javascript ×1

vue.js ×1

vuejs2 ×1

vuejs3 ×1