简体   繁体   English

在angular2的模板中使用templateUrl

[英]Using templateUrl within template in angular2

I've just started learning Angular2 and stuck at a point wherein I need to combine 2 components. 我刚刚开始学习Angular2,并停留在需要组合两个组件的地步。

Here's my app.component.ts 这是我的app.component.ts

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

    @Component({
     selector: 'app-root',
     template: './app.component.html <app-home></app-home>',
     styleUrls: ['./app.component.css']
    })
    export class AppComponent {
      mylogo = 'ABC Engineering';
      headlist = ["Home", "About", "Portfolio", "Pricing", "Contact"];
      technology = ["HTML", "CSS", "JavaScript", "jQuery", "AngularJS",    "PHP"];
    }

Here's my app.module.ts 这是我的app.module.ts

import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';
import { FormsModule } from '@angular/forms';
import { HttpModule } from '@angular/http';

import { AppComponent } from './app.component';
import { HomeComponent } from './home/home.component';
import { AboutComponent } from './about/about.component';

@NgModule({
  declarations: [
    AppComponent,
    HomeComponent,
    AboutComponent,
  ],
  imports: [
    BrowserModule,
    FormsModule,
    HttpModule
  ],

  providers: [],
  bootstrap: [AppComponent]
})
export class AppModule { }

It gives the output as shown in the image. 如图所示,它给出了输出。 产量

It is not showing the output of app.component.html! 它没有显示app.component.html的输出! But it does show the content from 'home.component.html'. 但是它确实显示了“ home.component.html”中的内容。 How do I fix it to show the content from 'app.component.html'? 如何修复它以显示“ app.component.html”中的内容?

You can either use 您可以使用

   @Component({
     selector: 'app-root',
     templateUrl: './app.component.html',
     styleUrls: ['./app.component.css']
    })

or 要么

   @Component({
     selector: 'app-root',
     template: '<app-home></app-home>',
     styleUrls: ['./app.component.css']
    })

but not combine or mix both within a single component. 但不能将两者结合或混合在一个组件中。

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

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