VueJS/Typescript - Cannot find module './components/Navigation' or its corresponding type declarations

你。 提交于 2020-12-26 07:58:47

问题


When I create a script as typescript (lang="ts") I get an error stating

"Cannot find module './components/Navigation' or its corresponding type declarations (Vetur 2307).".

I realized that this only happens when I set the lang as ts, which is what I need to build my Vue application.

app.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>

Navigation.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>

回答1:


In the src folder add the file shims-vue.d.ts with the following content :

declare module "*.vue" {
  import Vue from 'vue'
  export default Vue
}

and import the component with its extension .vue :

 import Navigation from './components/Navigation.vue';

instead of :

import Navigation from './components/Navigation';


来源:https://stackoverflow.com/questions/64213461/vuejs-typescript-cannot-find-module-components-navigation-or-its-correspon

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!