Deo*_*tay 37 model-view-controller typescript
我正在尝试使用导出和导入但它无法正常工作我收到错误
这是我的代码HTML:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0">
</head>
<body>
@RenderBody()
<script src="~/scripts/user.js"></script>
<script src="~/scripts/main.js"></script>
</body>
</html>
Run Code Online (Sandbox Code Playgroud)
用户:
export class User {
firstName: string;
lastName: string;
}
Run Code Online (Sandbox Code Playgroud)
main.ts
import { User } from "./user";
Run Code Online (Sandbox Code Playgroud)
这里也是异常的截图:
Typ*_*rce 14
尝试以下更改
HTML:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0">
</head>
<body>
@RenderBody()
<!-- <script src="~/scripts/user.js"></script> --> <!-- not longer needed -->
<script src="~/scripts/main.js"></script>
</body>
</html>
Run Code Online (Sandbox Code Playgroud)
tsconfig.json
{
"compilerOptions": {
"module": "commonjs",
"target": "es5",
"sourceMap": true,
"outFile": "~/scripts/main.js",
"lib": [
"dom",
"es2015",
"es5",
"es6"
]
}
}
Run Code Online (Sandbox Code Playgroud)
使用此配置,您的输出只是一个js文件,可以使用wunderfull,包含main.ts中的所有引用文件.我只是不知道是否〜/工作或者你是否必须设置相对于配置文件的路径,我不使用linux.
User.ts
class User {
firstName: string;
lastName: string;
}
Run Code Online (Sandbox Code Playgroud)
Main.ts:
/// <reference path="User.ts" />
// import { User } from "./user"; // not needed if referenced
console.log(new User());
Run Code Online (Sandbox Code Playgroud)
所有引用声明都必须写在文件的开头
默认情况下,TypeScript 使用节点模块解析。Node.js / CommonJS 使用exports关键字。但是,CommonJS 不能在没有模块加载器或模块捆绑器的浏览器中运行。因此,您需要 browserify 或 webpack 才能让它在浏览器中运行。
查看此链接https://www.typescriptlang.org/docs/handbook/gulp.html
您还可以在tsconfig.json 的编译器选项部分将模块设置设置为无:
{ "compilerOptions": { "target": "es5", "module": "none" } }
| 归档时间: |
|
| 查看次数: |
39203 次 |
| 最近记录: |