【发布时间】:2016-03-31 18:59:29
【问题描述】:
我正在开发一个包含多个组件的 Angular2 演示。该演示使用 AppComponent 进行引导,我在 AppComponent 的模板 html 中导入了其他组件。我成功设置了demo并看到了AppComponent的内容,但是加载其他组件失败。
这是我的 angular2 组件的代码示例
app.component.html
<div id="map">
<navigator></navigator>
</div>
app.component.ts
import {Component, View} from 'angular2/core';
import {NavigatorComponent} from '../components/navigator/navigator.component'
@Component({
selector: 'app'
})
@View({
templateUrl: './app/app.component.html',
directives: [NavigatorComponent]
})
export class AppComponent { }
navigator.component.html
<input type="text">
navigator.component.ts
import {Component, View} from 'angular2/core';
@Component({
selector: 'navigator'
})
@View({
templateUrl: './components/navigator/navigator.component.html'
})
export class NavigatorComponent { }
当我设置服务器并尝试查看应用程序时,出现以下错误
"NetworkError: 404 Not Found - http://localhost:3000/components/navigator/navigator.component"
和
Error: XHR error (404 Not Found) loading http://localhost:3000/components/navigator/navigator.component
Error loading http://localhost:3000/components/navigator/navigator.component as "../components/navigator/navigator.component" from http://localhost:3000/app/app.component.js
很明显,服务器试图找到一个 navigator.component,但它不存在。但是,当我尝试http://localhost:3000/components/navigator/navigator.component.html 时,我成功地看到了带有文本框的页面。
所以问题似乎是缺少 html 扩展,但我不知道它是怎么发生的。
我将不胜感激任何解决问题的建议和答案。提前致谢。
如果你想完全体验这个bug,请到项目的repo (https://github.com/haoliangyu/angular2-leaflet-starter) 执行以下步骤:
在 public_src/app/app.component.ts
中取消注释第 12 行
设置应用程序
转到localhost:3000并检查错误
更新:
这个问题是通过抵抗 SystemJS 配置中的其他组件来解决的,比如
System.config({
defaultJSExtension: true,
packages: {
'app': {
format: 'register'
},
'components/navigator': {
format: 'register'
}
}
});
这有助于应用找到其他组件 :)
【问题讨论】:
-
您如何托管您的服务器?与浏览器同步?
-
我使用了包lite-server。根据它的依赖文件,我猜是的。
标签: angular