我刚刚开始使用 Angular2,遇到了一个奇怪的组件文件扩展名问题:
让我们使用来自 angular.io 的 5 分钟快速入门演示 https://angular.io/docs/ts/latest/quickstart.html(我将在这里重现代码以供参考)。
文件结构
angular2-quickstart
|- app
| |- app.component.ts
| |- boot.ts
|
|- index.html
|- package.json
|- tsconfig.json
包.json
{
"name": "angular2-quickstart",
"version": "1.0.0",
"scripts": {
"tsc": "tsc",
"tsc:w": "tsc -w",
"lite": "lite-server",
"start": "concurrent \"npm run tsc:w\" \"npm run lite\" "
},
"license": "ISC",
"dependencies": {
"angular2": "2.0.0-beta.0",
"systemjs": "0.19.6",
"es6-promise": "^3.0.2",
"es6-shim": "^0.33.3",
"reflect-metadata": "0.1.2",
"rxjs": "5.0.0-beta.0",
"zone.js": "0.5.10"
},
"devDependencies": {
"concurrently": "^1.0.0",
"lite-server": "^1.3.1",
"typescript": "^1.7.3"
}
}
tsconfig.json
{
"compilerOptions": {
"target": "ES5",
"module": "system",
"moduleResolution": "node",
"sourceMap": true,
"emitDecoratorMetadata": true,
"experimentalDecorators": true,
"removeComments": false,
"noImplicitAny": false
},
"exclude": [
"node_modules"
]
}
应用程序/应用程序组件.ts
import {Component} from 'angular2/core';
@Component({
selector: 'my-app',
template: '<h1>My First Angular 2 App</h1>'
})
export class AppComponent { }
应用程序/boot.ts
import {bootstrap} from 'angular2/platform/browser'
import {AppComponent} from './app.component'
bootstrap(AppComponent);
索引.html
<html>
<head>
<title>Angular 2 QuickStart</title>
<!-- 1. Load libraries -->
<script src="node_modules/angular2/bundles/angular2-polyfills.js"></script>
<script src="node_modules/systemjs/dist/system.src.js"></script>
<script src="node_modules/rxjs/bundles/Rx.js"></script>
<script src="node_modules/angular2/bundles/angular2.dev.js"></script>
<!-- 2. Configure SystemJS -->
<script>
System.config({
packages: {
app: {
format: 'register',
defaultExtension: 'js'
}
}
});
System.import('app/boot')
.then(null, console.error.bind(console));
</script>
</head>
<!-- 3. Display the application -->
<body>
<my-app>Loading...</my-app>
</body>
</html>
这就像一个魅力,但暴露了整个文件结构,包括node_modules
和配置,这似乎是一个坏主意。
所以我试图只暴露app
文件夹。为了实现这一目标,我做了以下工作:
- 将引用的 javascript 文件复制到
app/scripts
folder
- 移动
index.html
in app
- 更改此文件中的引用路径
- 改变
lite
脚本在package.json
to lite-server --baseDir app
运行这个时,一切正常,除了angular2-polyfills.js
回 :
Error: XHR error (404 Not Found) loading http://localhost:3000/app.component(…)
run @ angular2-polyfills.js:138
zoneBoundFn @ angular2-polyfills.js:111
lib$es6$promise$$internal$$tryCatch @ angular2-polyfills.js:1511
lib$es6$promise$$internal$$invokeCallback @ angular2-polyfills.js:1523
lib$es6$promise$$internal$$publish @ angular2-polyfills.js:1494
lib$es6$promise$$internal$$publishRejection @ angular2-polyfills.js:1444
(anonymous function) @ angular2-polyfills.js:243
run @ angular2-polyfills.js:138zoneBoundFn @ angular2-polyfills.js:111
lib$es6$promise$asap$$flush @ angular2-polyfills.js:1305
当然,如果我要求http://localhost:3000/app.component.js
文件已正确返回。
所以我的问题是:为什么不.js
扩展名被附加到app.component
,导致此错误,当服务器 baseDir 是项目的根目录时,它在哪里起作用?我错过了一些配置选项吗?