简体   繁体   中英

Angular 2 “template” vs “templateUrl” issue

I am building up an app using 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

Console throws: Uncaught (in promise): Error: Error in app/about.component.html:1:30 caused by: Maximum call stack size exceeded

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

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-block>

   </about-block>

Remove <about-block></about-block> from about.component.html

Because of your about-block tag in html template, it is going recursive. Hence, you are receiving Maximum call stack size exceeded error.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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