首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >@Input父子组件绑定

@Input父子组件绑定
EN

Stack Overflow用户
提问于 2019-01-28 05:44:42
回答 1查看 151关注 0票数 0

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

我期望在子页面上看到"Example: Hello Angular 7“,我只看到"Example:”

这是父组件文件:

代码语言:javascript
复制
import { Component } from '@angular/core';

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

}

下面是子组件代码:

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

这是app.component.html组件

代码语言:javascript
复制
<app-nav></app-nav><router-outlet></router-outlet>

这是我的一个删节的app.module.ts文件:

代码语言:javascript
复制
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

代码语言:javascript
复制
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},
EN

回答 1

Stack Overflow用户

发布于 2019-01-29 05:29:29

Eliseo是正确的。我没有渲染'app-parent‘元素。一旦我这样做了,它就起作用了。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/54393171

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档