简体   繁体   English

Angular 2“模板”与“ templateUrl”问题

[英]Angular 2 “template” vs “templateUrl” issue

I am building up an app using RouterModule. 我正在使用RouterModule构建一个应用程序。 The problem is that when I use "template" in my Component decoration everything works great, but when I use "templateUrl" instead, the app starts glitching 问题是,当我在组件装饰中使用“模板”时,一切正常,但是当我使用“ templateUrl”时,应用程序开始出现故障

Console throws: Uncaught (in promise): Error: Error in app/about.component.html:1:30 caused by: Maximum call stack size exceeded 控制台抛出:未捕获(承诺): 错误:app / about.component.html:1:30中的错误,原因是:超出了最大调用堆栈大小

Just don't know what files could be helpful, because it seems like the issue is not there. 只是不知道哪些文件会有所帮助,因为似乎问题不存在。

Not working properly 无法正常运作

   import { Component } from '@angular/core';

     @Component({
        selector: 'about-block',
        templateUrl: 'app/about.component.html'
     })

   export class AboutComponent {}

Working properly 好好工作

import { Component } from '@angular/core';

    @Component({
       selector: 'about-block',
       template: '<h1>This is About section</h1>'
    })

   export class AboutComponent {}

PS PS

app.module.ts app.module.ts

import { NgModule }      from '@angular/core';
import { BrowserModule } from '@angular/platform-browser'
import { AppComponent }   from './app.component';

import { AppRoutingModule, routingComponents}  from './app.routing';

 @NgModule({
       imports:      [ BrowserModule, AppRoutingModule ],
       declarations: [ AppComponent, routingComponents ],
       bootstrap:    [ AppComponent ]
     })
 export class AppModule { }

about.component.html about.component.html

   <about-block>

   </about-block>

Remove <about-block></about-block> from about.component.html 从about.component.html中删除<about-block></about-block>

Because of your about-block tag in html template, it is going recursive. 由于您在html模板中使用about-block标记,因此它是递归的。 Hence, you are receiving Maximum call stack size exceeded error. 因此,您收到最大呼叫堆栈大小超出错误。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM