【问题标题】:Using templateUrl within template in angular2在angular2的模板中使用templateUrl
【发布时间】:2017-02-26 22:39:38
【问题描述】:

我刚开始学习 Angular2 并停留在需要组合 2 个组件的地方。

这是我的 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"];
    }

这是我的 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 { }

它给出了如图所示的输出。

它没有显示 app.component.html 的输出!但它确实显示了“home.component.html”中的内容。 如何修复它以显示“app.component.html”中的内容?

【问题讨论】:

    标签: html css angular angular2-template


    【解决方案1】:

    你可以使用

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

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

    但不能将两者结合或混合在一个组件中。

    【讨论】:

    • 在这种情况下如何加入这两个组件?我的意思是如何加入 app.component.html 与 home.component.html 后跟 about.component.html ?
    • 创建一个包含该 HTML 的组件,并将与其选择器匹配的标签添加到其他组件模板。 angular.io 有很棒的教程
    • 哦,现在我明白了!谢谢你:)
    猜你喜欢
    • 2017-06-30
    • 2017-05-20
    • 2013-04-21
    • 2016-08-17
    • 1970-01-01
    • 1970-01-01
    • 2016-06-12
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多