@Input父子组件绑定

时间:2019-01-27 21:44:42

标签: angular

我正在使用this代码来测试父/子@Input装饰器。

我希望在子页面上看到“示例:Hello Angular 7”,我只会看到“示例:”

这是父组件文件:

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

@Component({
  selector: 'app-parent',
  template: `
    <app-child [childExample]="parentExample"></app-child>
  `
 })
export class ParentComponent{
parentExample: string = 'Hello Angular 7';

}

这是子组件代码:

import { Component, Input } from '@angular/core';
@Component({
  selector: 'app-child',
  template: `
      Example: {{ childExample }}
  `
})
export class ChildComponent {
  @Input() childExample: string;
}

这是app.component.html组件

<app-nav></app-nav><router-outlet></router-outlet>

这是我的简化版app.module.ts文件:

import { BrowserModule } from '@angular/platform-browser'
import { NgModule } from '@angular/core'
import { ParentComponent } from './parent.component'
import { ChildComponent } from './child.component'

@NgModule({
  declarations: [
    AppComponent,
    CreditCardComponent,
    ParentComponent,
   ChildComponent   
  ],
  imports: [
   BrowserModule,
    BrowserAnimationsModule,
    AppRoutingModule,


  ],
  providers: [
  { provide: HTTP_INTERCEPTORS, useClass: JwtInterceptor, multi: true },
  { provide: HTTP_INTERCEPTORS, useClass: ErrorInterceptor, multi: true },
  StripeComponent

  ],
  bootstrap: [AppComponent]

})
export class AppModule { }

这是app-routing.module

import { ParentComponent } from './parent.component'
import { ChildComponent } from './child.component'

const appRoutes: Routes = [
{ path: '', redirectTo: '/home', pathMatch: 'full' },
{ path: 'home', component: HomeComponent},
{ path: 'stocks', component: StocksComponent, canActivate: [AuthGuard] },
{ path: 'register', component: RegisterComponent },
{ path: 'login',  component: LoginComponent},
{ path: 'stripe',  component: StripeComponent},
{ path: 'cc',  component: CreditCardComponent},
{ path: 'parent',  component: ParentComponent},
{ path: 'child',  component: ChildComponent},

1 个答案:

答案 0 :(得分:0)

Eliseo是正确的。我没有呈现“ app-parent”元素。一旦完成,它就会起作用。