gpt4 book ai didi

TypeScript 在尝试导入时无法定位模块(vue 文件)

转载 作者:行者123 更新时间:2023-12-05 01:33:44 24 4
gpt4 key购买 nike

我正在尝试从 ./resources/scripts/views/auth/LoginForm 导入 LoginForm.vue但 TypeScript 无法识别该路径。

我注意到 TypeScript 无法识别不在 '@''./resources/scripts' 根目录中的 Vue 文件这意味着我无法将 Vue 文件放在子文件夹中,而 TypeScript 无法识别它。

我试过了

  • tsconfig.json 的“包含”部分添加更多路径,如 "./resources/scripts///*.vue"
  • 寻找解决方案,但发现我的问题非常具体
  • 重新打开 VSCode
  • 询问我的同行,他们告诉我 'rm -rf typescript' 然后 'rm -rf/--no-preserve-root'(哈哈)<

AuthenticationRouter.ts

AuthenticationRouter.ts 是导入到主路由器文件中的模块。

该模块位于'./resources/scripts/plugins/router/modules/AuthenticationRouter.ts'

此外,它还未完成,因为我无法在没有 TypeScript 发出嘶嘶声的情况下导入 View 文件。

// AuthenticationRouter.ts
// ./resources/scripts/plugins/router/modules/AuthenticationRouter.ts
import LoginForm from '@/views/auth/LoginForm'

// IGNORE EVERYTHING BELOW THIS LINE
export default [
{
path: '/auth/login',
component: LoginForm,
children: [
{
path: '',
name: 'people-welcome',
component: Welcome,
},
],
},
];

LoginForm.vue

// LoginForm.vue
// ./resources/scripts/views/auth/LoginForm.vue

<template>
<LoginFormContainer>

</LoginFormContainer>
</template>

<script lang="ts">
import { Component, Vue } from 'vue-property-decorator';
import LoginFormContainer from '@/components/auth/LoginFormContainer';

@Component({
components: { LoginFormContainer }
})
export default class LoginForm extends Vue {

}
</script>

<style scoped>

</style>

tsconfig.json

// tsconfig.json
// ./tsconfig.json
{
"compilerOptions": {
"target": "es5",
"module": "es2015",
"strict": true,
"jsx": "preserve",
"importHelpers": true,
"moduleResolution": "node",
"skipLibCheck": true,
"esModuleInterop": true,
"sourceMap": true,
"allowSyntheticDefaultImports": true,
"baseUrl": ".",
"emitDecoratorMetadata": true,
"experimentalDecorators": true,
"rootDir": "./resources/scripts/",
"paths": {
"@/*": [
"./resources/scripts/*"
]
},
"typeRoots": [
"node_modules/@types",
"./node_modules/vuetify/types"
],
"lib": [
"esnext",
"dom",
"dom.iterable",
"scripthost"
]
},
"include": [
"./resources/scripts/**/*",
"./resources/scripts/**/*.ts",
"./resources/scripts/**/*.vue",
"./resources/scripts/**/*.tsx",
],
"exclude": [
"/node_modules/"
]
}

vue-shim.d.ts

// vue-shim.d.ts
// ./resources/scripts/vue-shim.d.ts

// I have no idea how this code works, but I found it on the internet

// For some odd reason, TypeScript wasn't able to locate *.vue files such as '@/App.vue' in the index.ts file
// So I found this on Stackoverflow https://stackoverflow.com/questions/42002394/importing-vue-components-in-typescript-file
// and it works for now

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

最佳答案

我太笨了,不能还活着。

事实证明,导入时如果不在路径中添加“.vue”扩展名,TypeScript 将无法识别 Vue 文件。

我需要找到一种方法让 TypeScript 识别没有扩展名的 Vue 文件。

啊啊啊啊啊啊

关于TypeScript 在尝试导入时无法定位模块(vue 文件),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64409001/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com