【问题标题】:angular-cli: Error during data binding <> @Input property is nullangular-cli:数据绑定期间出错 <> @Input 属性为空
【发布时间】:2017-07-17 06:01:04
【问题描述】:

我已经阅读了有关此问题的每个论坛,但仍然无法使其正常工作。 我有 1 个父组件并希望在其中加载一些子组件(为每个子组件传递不同的输入参数)。

我的应用程序设置如下:

  • 我的 app.component 将有 3 个子组件:页眉、内容(我什至还没有实现)和页脚;
  • 还有一个组件需要在我的 Header 组件中重复,所以我创建了一个 Child 组件(仅在 Header 组件中使用);

以下是我的所有代码:


app.module.ts

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

import { AppComponent } from './app.component';
import { HeaderComponent } from './header/header.component';
import { FooterComponent } from './footer/footer.component';
import { ChildComponent } from './child/child.component';

@NgModule({
  declarations: [
    AppComponent,
    HeaderComponent,
    FooterComponent,
    ChildComponent
  ],
  imports: [
    BrowserModule,
    HttpModule
  ],
  providers: [

  ],
  bootstrap: [
    AppComponent,
    HeaderComponent,
    FooterComponent,
    ChildComponent
  ]
})

export class AppModule { }

app.component.ts

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

@Component({
  selector: 'app-root',
  template: `
  <header class="container-fluid">
    <app-header></app-header>
  </header>

  <content class="container-fluid">
  </content>

  <footer class="container-fluid">
    <app-footer></app-footer>
  </footer>`
})

export class AppComponent { }

header.component.ts

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

@Component({
  selector: 'app-header',
  template: `
  <div>
    <h2>HEADER</h2>
    <app-child [code]="'1st value'"></app-child><br>
    <app-child [code]="'2nd value'"></app-child><br>
    <app-child [code]="'3rd value'"></app-child>
  </div>`
})

export class HeaderComponent { }

footer.component.ts

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

@Component({
  selector: 'app-footer',
  template: `<div><h2>FOOTER</h2></div>`
})

export class FooterComponent { }

child.component.ts

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

@Component({
  selector: 'app-child',
  template: `<div>Hello World! {{this.code}}</div>`
})

export class ChildComponent {
    @Input() code: string;
}

但由于某种原因,绑定不适用于第一个子组件。输出是:

Hello World!
Hello World! 2nd value
Hello World! 3rd value

为了让事情变得更加混乱,如果我删除了我的页脚组件,这可以工作......但是页脚组件与我的页眉组件或其子组件没有任何关系。

有人能帮我弄清楚为什么这只是在第一次出现时失败,但在所有其他绑定中都可以正常工作吗?

角度属性:

    _                      _                 ____ _     ___
   / \   _ __   __ _ _   _| | __ _ _ __     / ___| |   |_ _|
  / △ \ | '_ \ / _` | | | | |/ _` | '__|   | |   | |    | |
 / ___ \| | | | (_| | |_| | | (_| | |      | |___| |___ | |
/_/   \_\_| |_|\__, |\__,_|_|\__,_|_|       \____|_____|___|
               |___/
@angular/cli: 1.2.1
node: 6.10.0
os: win32 x64
@angular/animations: 4.2.6
@angular/common: 4.2.6
@angular/compiler: 4.2.6
@angular/compiler-cli: 4.2.6
@angular/core: 4.2.6
@angular/forms: 4.2.6
@angular/http: 4.2.6
@angular/platform-browser: 4.2.6
@angular/platform-browser-dynamic: 4.2.6
@angular/platform-server: 4.2.6
@angular/router: 4.2.6
@angular/cli: 1.2.1
@angular/language-service: 4.2.6

提前感谢您的帮助。

【问题讨论】:

    标签: data-binding angular-cli input-parameters


    【解决方案1】:

    我复制了您的代码,但无法重现您指定的错误。能否请您检查是否还有其他遗漏。

    【讨论】:

    • 萨钦,感谢您回复我。你是对的......我发送的代码将起作用。我试图避免在这里写下这么多代码,但我现在编辑了我最初的问题并添加了我所有的代码供您考虑。如果您能对此有所了解,请告诉我。谢谢
    • 当你引导 ChildComponent 类时,Angular 会寻找 标签并实例化它,这会删除你通过 HeaderComponent 发送给它的值。 (Angular 只在第一次匹配时这样做)。如果您在 ChildComponent 的 ngOnInit 中使用 console.log(this.code),您会注意到您传递的 3 个值出现了两次,然后出现了 1 个未定义。
    • 嗨@john.acb。 Angular 按照引导数组中定义的顺序进行引导。要解决此问题,您可以从引导程序中删除除 AppComponent 之外的所有组件,或者如果您需要在 index.html 中使用 ,则将 ChildComponent 保留在 HeaderComponent 之前。
    • Saschin...非常感谢...事实上我就是这样引导应用程序的。非常感谢
    猜你喜欢
    • 2018-09-29
    • 2018-11-29
    • 1970-01-01
    • 2017-11-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-10-16
    • 1970-01-01
    相关资源
    最近更新 更多