node.js - tsconfig 路径不适用于 vue/node 项目
问题描述
我正在尝试为 vue 应用程序设置 tsconfig 文件以利用路径属性。该项目最初是作为一个 vue-cli 项目开始的,但现在我需要重组项目以添加 node/express/mongo。我已经阅读了有关“路径”属性的 ts 文档。我仍然收到以下错误:起初它列出了我所有的 vue 组件打字稿文件、vuex 存储打字稿文件和我的 types.ts,这是我定义自定义类型并说找不到它们的地方。
ERROR Failed to compile with 23 errors 4:14:16 AM
These dependencies were not found:
* @/components/../...vue
...
ERROR in C:/Development/my-project/client/src/views/my-view/MyView.ts(5,38):
5:38 Cannot find module '@/types/types' or its corresponding type declarations.
3 |
4 | import { Action, Getter, State } from 'vuex-class';
> 5 | import { User, UserState } from '@/types/types';
项目结构如下:
/my-project
/client
/public
/src
/components
...
App.vue
main.ts
/server
server.js
config.js
db.js
tsconfig.json
tsconfig.app.json
vue.config.js
.eslintrc.js
...
我的 tsconfig 如下:
{
"compilerOptions": {
"baseUrl": ".",
"target": "esnext",
"module": "esnext",
"strict": true,
"strictPropertyInitialization": false,
"jsx": "preserve",
"importHelpers": true,
"moduleResolution": "node",
"resolveJsonModule": true,
"experimentalDecorators": true,
"esModuleInterop": true,
"allowSyntheticDefaultImports": true,
"sourceMap": true,
"types": [
"webpack-env",
"jest"
],
"lib": [
"esnext",
"dom",
"dom.iterable",
"scripthost"
]
},
"exclude": [
"node_modules"
]
}
我的 tsconfig.app 如下:
{
"extends": "./tsconfig.json",
"compilerOptions": {
"baseUrl": "./client",
"paths": {
"@/*": ["src/*"]
}
},
"include": [
"client/src/**/*.ts",
"client/src/**/*.tsx",
"client/src/**/*.vue",
"client/tests/**/*.ts",
"client/tests/**/*.tsx"
]
}
作为参考,我的 vue.config 如下:
const path = require('path');
function addStyleResource(rule) {
rule.use('style-resource')
.loader('style-resources-loader')
.options({
patterns: [
path.resolve(__dirname, './client/src/styles/style.scss')
]
});
}
module.exports = {
assetsDir: 'client/src/assets',
indexPath: 'client/public/index.html',
pages: {
index: {
entry: 'client/src/main.ts'
}
},
chainWebpack: config => {
const types = ['vue-modules', 'vue', 'normal-modules', 'normal'];
types.forEach(type => addStyleResource(config.module.rule('scss').oneOf(type)));
},
publicPath: '/client',
};
解决方案
推荐阅读
- docker - 如何在 windows server 2016 docker 容器上启用 websocket
- vue.js - 在嵌套项目列表中显示 VUE 中可拖动项目的计数
- azure-api-management - 自定义身份验证策略
- javascript - 尝试使用 D3 在地图上显示条形图,出现错误:
属性高度:属性的意外结束。预期长度,“” - angular - ag-grid cellRenderer 元素的操作不起作用
- python - 在 Python 3 中生成随机数量的特定字母
- c++ - 类替代中的命名空间别名
- node.js - 在 Centos 7 上配置运行 Nodejs 的 Nginx
- python - 如何以更好的方式解包这个嵌套元组?
- c++ - 检查两个向量是否平行的最有效方法