这是我的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扩展,但我不知道它是如何发生的.
我将不胜感激任何解决问题的建议和答案.提前致谢.
如果您想完全体验该错误,请转到项目的回购(https://github.com/haoliangyu/angular2-leaflet-starter)并执行以下步骤:
>取消注释public_src / app / app.component.ts上的第12行
>设置应用程序
>转到localhost:3000并检查错误
更新:
System.config({ defaultJSExtension: true,packages: { 'app': { format: 'register' },'components/navigator': { format: 'register' } } });
这有助于应用程序找到其他组件:)
它试图从http://localhost:3000/components/navigator/navigator.component加载它,但它应该从地图’app’加载,ergo:http://localhost:3000/app/components/navigator/navigator.component.js.js扩展将被SystemJS配置中的默认扩展集添加.
在app.component.ts中注释第12行后错误消失的原因是即使navigator.component.ts的import语句仍然保留在代码中,typescript编译器也不会将它添加到实际输出中,因为该类未在当前文件中使用.
我无法安装您的项目,因为您似乎已经对其进行了一些更改.但我相信解决这个问题的方法是更改app.component.ts中navigator.component的导入位置:
import {NavigatorComponent} from './../components/navigator/navigator.component'
更好的方法是在index.html中添加一个基本的href标记:
<base href="http://localhost/app/">
原文地址:https://www.jb51.cc/angularjs/142046.html
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。