簡體   English   中英

angular-cli:數據綁定期間錯誤<> @Input屬性為null

[英]angular-cli: Error during data binding <> @Input property is null

我已經閱讀了每個論壇的相關內容,但仍然無法正常使用。 我有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

在此先感謝您的幫助。

我復制了您的代碼,但無法重現您指定的錯誤。 您能檢查一下您是否還有其他遺漏嗎?

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM