我最近开始使用vs代码。我使用带有反应和类型脚本的材料ui。但是我不能使用alt(option)+输入快捷方式来导入材料ui的组件。我正在使用Mac,而我正在使用的打字稿版本是3.0.3。但是,我正在显式创建的react组件,我能够使用上述快捷方式导入的组件。以下是我正在使用的package.json:
{
"name": "portfolio",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"build": "webpack"
},
"author": "ahujru",
"license": "ISC",
"dependencies": {
"@material-ui/core": "3.0.3",
"@material-ui/icons": "3.0.1",
"@types/material-ui": "0.21.5",
"@types/react": "16.4.14",
"@types/react-dom": "16.0.7",
"awesome-typescript-loader": "5.2.1",
"css-loader": "1.0.0",
"node-sass": "4.9.3",
"react": "16.5.1",
"react-dom": "16.5.1",
"sass": "1.13.4",
"sass-loader": "7.1.0",
"source-map-loader": "0.2.4",
"style-loader": "0.23.0",
"typeface-roboto": "0.0.54",
"typescript": "3.0.3",
"webpack": "4.19.0",
"webpack-cli": "3.1.0",
"webpack-dev-server": "3.1.8"
}
}
和tsconfig.json:
{
"compilerOptions": {
"baseUrl": ".",
"module": "commonjs",
"watch": true,
"sourceMap": true,
"outDir": "target",
"target": "es6",
"jsx": "react",
"noImplicitAny": true,
"moduleResolution": "node"
},
"include": [
"./src/**/*"
],
"exclude": [
"node_modules"
]
}
我已经安装了npm intellisense,intellij键盘绑定和自动导入之类的插件,但似乎什么也没发生。有人可以告诉我我在这里想念什么。
答案 0 :(得分:1)
我相信我可以重现您遇到的问题。根据我的测试,VS Code内置的自动导入功能仅适用于已在项目中 加载的文件中定义的符号,因为它们匹配,或者由文件直接或间接导入匹配include
中的tsconfig.json
设置。 (有关某些背景,请参见this thread。)
我从您的配置文件和空白的源文件开始,但是删除了@types/material-ui
,因为它用于旧的material-ui
软件包(您尚未安装),这很令人困惑。此时,如果我使用Ctrl-Space调用代码完成功能,则 not 不会提供Material UI组件(AppBar
,Avatar
,...)。但是,如果我添加了一个导入,例如import { AppBar } from "@material-ui/core";
,则它会强制TypeScript服务器加载@material-ui/core
,因此现在,如果我再次调用代码完成功能,我将获得其余的Material UI组件。如果您需要自动导入才能在没有现有导入的情况下正常工作,则可以将适当的.d.ts
文件添加到include
的{{1}}列表中,尽管那样的话您想使用单独的{ {1}}文件用于批量编译,这样您就不会尝试覆盖tsconfig.json
程序包中的JavaScript文件。
我尚未测试任何第三方自动导入插件,也无法谈论其行为。