【问题标题】:fail to locate template html of imported commponent in Angular2无法在 Angular 2 中找到导入组件的模板 html
【发布时间】: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) 执行以下步骤:

  1. public_src/app/app.component.ts

  2. 中取消注释第 12 行
  3. 设置应用程序

  4. 转到localhost:3000并检查错误


更新

这个问题是通过抵抗 SystemJS 配置中的其他组件来解决的,比如

System.config({
    defaultJSExtension: true,
    packages: {
        'app': {
            format: 'register'
        },
        'components/navigator': {
            format: 'register'
        }
    }
});

这有助于应用找到其他组件 :)

【问题讨论】:

  • 您如何托管您的服务器?与浏览器同步?
  • 我使用了包lite-server。根据它的依赖文件,我猜是的。

标签: angular


【解决方案1】:

问题不在于找不到模板文件。但它无法加载navigator.component.js

它正在尝试从http://localhost:3000/components/navigator/navigator.component 加载它,但它应该从地图“app”加载,即:http://localhost:3000/app/components/navigator/navigator.component.jsjs 扩展名将由 SystemJS 配置中设置的默认扩展名添加。

app.component.ts 中注释第 12 行后错误消失的原因是即使 navigator.component.ts 的导入语句仍保留在代码中,打字稿编译器不会将其添加到实际输出中,因为当前文件中没有使用该类。

我无法安装您的项目,因为您似乎已经对其进行了一些更改。但我相信解决此问题的方法是更改​​ navigator.componentapp.component.ts 内的导入位置:

import {NavigatorComponent} from './../components/navigator/navigator.component'

注意我是如何在当前文件夹前面添加./


最好在 index.html 中添加一个基本的 href 标签:

<base href="http://localhost/app/">

或者从app 文件夹为您的网站提供服务,这样http://localhost 实际上是指应用程序文件夹,而不是其父文件夹

【讨论】:

  • 虽然这不是解决方案,但你引导我走向正确的方向。谢谢
  • 如果我可以问,解决方案是什么?
  • 我需要在 System.config 中注册除 AppComponet 之外的其他组件。请查看我的问题底部的更新,此脚本位于项目的 public_src/index.html 中。
  • 虽然文件夹放置是正确的,但是我遇到了,如果我不添加module.id,就会出现html not found错误。@Component({ moduleId: module.id, // DONT MISS MODULE ID, ELSE它显示 .HTML NOT FOUND ERROR 选择器:'communication',templateUrl:'communication.component.html'})
  • @HydTechie 感谢您的反馈,我一直在关注 Heroes tut,但由于某种原因我找不到解决方案。您的解决方案是正确的。
猜你喜欢
  • 2017-06-23
  • 2017-10-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-07-11
  • 2016-10-17
相关资源
最近更新 更多