[英]Can VS Code automatically update JavaScript and TypeScript import paths on file rename/move?
Is there a module for vscode that would update paths towards files?是否有用于 vscode 的模块可以更新文件的路径? eg if I have:
例如,如果我有:
import './someDir/somelib'
and I rename or move somelib
, would it automatically update the file path in all the files where it is being referred as above?并且我重命名或移动
somelib
,它会自动更新上面提到的所有文件中的文件路径吗?
This feature was added for JavaScript and TypeScript in VS Code 1.24 ( tracking issue ) VS Code 1.24 中为 JavaScript 和 TypeScript 添加了此功能(跟踪问题)
When you move or rename a file, you will now be prompted to see if you want to update imports:当您移动或重命名文件时,现在将提示您查看是否要更新导入:
This is controlled by the javascript.updateImportsOnFileMove.enabled
and typescript.updateImportsOnFileMove.enabled
settings.这由
javascript.updateImportsOnFileMove.enabled
和typescript.updateImportsOnFileMove.enabled
设置控制。 Valid values are:有效值为:
"prompt"
— The default. "prompt"
——默认值。 Prompt for each file rename/move"always"
— Always try to update imports automatically without prompting "always"
— 总是尝试在没有提示的情况下自动更新导入"never"
— Do not update imports and do not prompt "never"
— 不更新导入且不提示As @jamey graham mentioned, it stopped working for me in a React typescript
project.正如@jamey graham 提到的,它在 React
typescript
项目中停止为我工作。 The fix was to update the tsconfig.json
file to have:修复方法是将
tsconfig.json
文件更新为:
{
"compilerOptions": {
"baseUrl": "./src",
// ...
},
}
and to remove the "include": ["src"]
option just for moving the files.并删除仅用于移动文件的
"include": ["src"]
选项。 VS Code should update the paths nicely after this. VS Code 应该在此之后很好地更新路径。
You can bring back the "include": ["src"]
option after this.您可以在此之后恢复
"include": ["src"]
选项。
I do not know why removing the "include": ["src"]
worked though.我不知道为什么删除
"include": ["src"]
有效。
If this doesn't do the trick, try opening a .ts
file and trigger the Typescript: Restart TS server
option in the command prompt and try again.如果这不起作用,请尝试打开
.ts
文件并在命令提示符下触发Typescript: Restart TS server
选项,然后重试。
Import updating stopped working for me after I set up typescript in my project.在我的项目中设置打字稿后,导入更新对我停止工作。
The solution was to follow: https://github.com/microsoft/vscode/issues/66937#issuecomment-475087628解决方案是遵循: https : //github.com/microsoft/vscode/issues/66937#issuecomment-475087628
Specifically, to add a jsconfig.json
file in the root of my project containing:具体来说,在我的项目的根目录中添加一个
jsconfig.json
文件,其中包含:
{
"compilerOptions": {
"module": "commonjs",
"target": "es2016",
"jsx": "preserve"
},
"exclude": ["node_modules", "**/node_modules/*"]
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.