nuxt.js 使用 Typescript 在 VSCode 報錯: File 'xxx/components/Logo.vue' is not a module. Vetur(2306)


nuxt.js 生成的默認文件 components/Logo.vue 源碼大概如下:

 1 <template>
 2   <svg
 3     class="NuxtLogo"
 4   >
 5   </svg>
 6 </template>
 7 <style>
 8 .NuxtLogo {
 9   margin: auto;
10 }
11 </style>

在 page/index.vue 文件中引入 `import Logo from '~/components/Logo.vue';` 會報錯:`File 'xxx/components/Logo.vue' is not a module. Vetur(2306)`。

typescript 提示新增 vue-shims.d.ts ( https://github.com/Microsoft/TypeScript-Vue-Starter#single-file-components ),試過還是不行。

github issue 翻了一下,最后找到這個:https://github.com/vuejs/vetur/issues/1709

給 components/Logo.vue 添加 export 即可:

 1 <template>
 2   <svg
 3     class="NuxtLogo"
 4   >
 5   </svg>
 6 </template>
 7 <style>
 8 .NuxtLogo {
 9   margin: auto;
10 }
11 </style>
12 <script lang="ts">
13 import Vue from 'vue';
14 
15 export default Vue.extend({
16   name: 'LOGO',
17 });
18 </script>

添加之后,還需關閉重啟 VSCode !


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM