如何使用Typescript将基本url添加到src中的文件夹?
我正在做一个typescript项目,我想设置基本url,这样我就可以使用相对于src文件夹的路径进行导入。我的文件夹结构如下如何使用Typescript将基本url添加到src中的文件夹?,typescript,base-url,Typescript,Base Url,我正在做一个typescript项目,我想设置基本url,这样我就可以使用相对于src文件夹的路径进行导入。我的文件夹结构如下 src -client --index.html --application.ts -server -shared tsconfig 当我尝试在index.html中使用基本url时 <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8">
src
-client
--index.html
--application.ts
-server
-shared
tsconfig
当我尝试在index.html中使用基本url时
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Game</title>
</head>
<body>
<canvas id="game-canvas"></canvas>
<p>Client</p>
<script src="~/client/ts/index.ts"></script>
</body>
</html>
它将抛出以下错误
错误:enoint:没有这样的文件或目录,请打开
'D:\Game\src\client\client\ts\index.ts'
它有多次客户端,为什么会发生这种情况?这几乎就像是从./中看到的,我的脚本是从中编译的,这将是客户机文件夹。所以这不会有问题,因为我可以做../来访问我的共享文件夹,因此我将index.html中的脚本改为~/ts/index.ts,它就可以工作了。但是index.ts以同样的方式导入应用程序,当我在浏览器中运行编译后的构建时,这种方式就可以工作了。所以它起作用了。但是现在vscode抛出以下错误
因为vscode不知何故认为它需要是~/client/ts/application
但在运行脚本时,这会在命令提示符中引发错误。那么如何在vscode中解决这个错误呢?最好的办法是,如果路径可以相对于我的src文件夹,但老实说,我不知道如何才能让它工作。这是包裹医生说的
tsconfig.json
{
"compilerOptions": {
/* Basic Options */
"target": "ES6", /* Specify ECMAScript target version: 'ES3' (default), 'ES5', 'ES2015', 'ES2016', 'ES2017', 'ES2018', 'ES2019' or 'ESNEXT'. */
"module": "commonjs", /* Specify module code generation: 'none', 'commonjs', 'amd', 'system', 'umd', 'es2015', or 'ESNext'. */
"lib": ["DOM", "ES2018"], /* Specify library files to be included in the compilation. */
"allowJs": false, /* Allow javascript files to be compiled. */
"checkJs": false, /* Report errors in .js files. */
"sourceMap": true, /* Generates corresponding '.map' file. */
"removeComments": true, /* Do not emit comments to output. */
//"noEmit": false, /* Do not emit outputs. */
"declaration": true,
"baseUrl": "./src",
"paths": {
"~*": ["./*"]
},
/* Strict Type-Checking Options */
"strict": true, /* Enable all strict type-checking options. */
"noImplicitAny": true, /* Raise error on expressions and declarations with an implied 'any' type. */
"strictNullChecks": true, /* Enable strict null checks. */
"strictFunctionTypes": true, /* Enable strict checking of function types. */
"strictBindCallApply": true, /* Enable strict 'bind', 'call', and 'apply' methods on functions. */
"strictPropertyInitialization": true, /* Enable strict checking of property initialization in classes. */
"noImplicitThis": true, /* Raise error on 'this' expressions with an implied 'any' type. */
"alwaysStrict": true, /* Parse in strict mode and emit "use strict" for each source file. */
/* Additional Checks */
"noUnusedLocals": true, /* Report errors on unused locals. */
"noUnusedParameters": true, /* Report errors on unused parameters. */
"noImplicitReturns": true, /* Report error when not all code paths in function return a value. */
"noFallthroughCasesInSwitch": true, /* Report errors for fallthrough cases in switch statement. */
/* Module Resolution Options */
"moduleResolution": "node", /* Specify module resolution strategy: 'node' (Node.js) or 'classic' (TypeScript pre-1.6). */
"typeRoots": ["node_modules/@types"], /* List of folders to include type definitions from. */
"esModuleInterop": true, /* Enables emit interoperability between CommonJS and ES Modules via creation of namespace objects for all imports. Implies 'allowSyntheticDefaultImports'. */
"forceConsistentCasingInFileNames": true, /* Disallow inconsistently-cased references to the same file. */
"experimentalDecorators": true
},
"include": ["src/**/*"],
}
您的
tsconfig.json
在您的文件夹结构中位于何处?与src的级别相同您的tsconfig.json
在您的文件夹结构中位于何处?与src的级别相同
{
"compilerOptions": {
/* Basic Options */
"target": "ES6", /* Specify ECMAScript target version: 'ES3' (default), 'ES5', 'ES2015', 'ES2016', 'ES2017', 'ES2018', 'ES2019' or 'ESNEXT'. */
"module": "commonjs", /* Specify module code generation: 'none', 'commonjs', 'amd', 'system', 'umd', 'es2015', or 'ESNext'. */
"lib": ["DOM", "ES2018"], /* Specify library files to be included in the compilation. */
"allowJs": false, /* Allow javascript files to be compiled. */
"checkJs": false, /* Report errors in .js files. */
"sourceMap": true, /* Generates corresponding '.map' file. */
"removeComments": true, /* Do not emit comments to output. */
//"noEmit": false, /* Do not emit outputs. */
"declaration": true,
"baseUrl": "./src",
"paths": {
"~*": ["./*"]
},
/* Strict Type-Checking Options */
"strict": true, /* Enable all strict type-checking options. */
"noImplicitAny": true, /* Raise error on expressions and declarations with an implied 'any' type. */
"strictNullChecks": true, /* Enable strict null checks. */
"strictFunctionTypes": true, /* Enable strict checking of function types. */
"strictBindCallApply": true, /* Enable strict 'bind', 'call', and 'apply' methods on functions. */
"strictPropertyInitialization": true, /* Enable strict checking of property initialization in classes. */
"noImplicitThis": true, /* Raise error on 'this' expressions with an implied 'any' type. */
"alwaysStrict": true, /* Parse in strict mode and emit "use strict" for each source file. */
/* Additional Checks */
"noUnusedLocals": true, /* Report errors on unused locals. */
"noUnusedParameters": true, /* Report errors on unused parameters. */
"noImplicitReturns": true, /* Report error when not all code paths in function return a value. */
"noFallthroughCasesInSwitch": true, /* Report errors for fallthrough cases in switch statement. */
/* Module Resolution Options */
"moduleResolution": "node", /* Specify module resolution strategy: 'node' (Node.js) or 'classic' (TypeScript pre-1.6). */
"typeRoots": ["node_modules/@types"], /* List of folders to include type definitions from. */
"esModuleInterop": true, /* Enables emit interoperability between CommonJS and ES Modules via creation of namespace objects for all imports. Implies 'allowSyntheticDefaultImports'. */
"forceConsistentCasingInFileNames": true, /* Disallow inconsistently-cased references to the same file. */
"experimentalDecorators": true
},
"include": ["src/**/*"],
}