組件間傳值之@Input

組件間傳值,可以用Input或者路由傳值。
例如:由列表頁進(jìn)入詳情頁
1、如果用路由傳值,就只需要在路由中添加id參數(shù)。
2、如果用Input傳值,那么就需要常見一個(gè)detail組件,并定義Input參數(shù),在list中調(diào)用detail組件,并傳入值。

Input傳值代碼演示:
A是B的父組件,B中有Input參數(shù)how,A中調(diào)用B組件,并傳入[how]參數(shù)。

//A頁面代碼
<table class="table table-bordered">
      <tbody>
      <tr>
        ....
      </tr>
      <tr *ngFor="let item of tasks|filter:'title':keyword;let i=index">
        ....
        <td><app-jinji [how]="item.jinji"></app-jinji></td>
        ....
      </tr>
      </tbody>
    </table>
//B頁面代碼
<span class="badge bg-green" *ngIf="how==1">不重要 不緊急</span>
<span class="badge bg-yellow" *ngIf="how==2">不重要 很緊急</span>
<span class="badge bg-yellow" *ngIf="how==3">很重要 不緊急</span>
<span class="badge bg-red" *ngIf="how==4">很重要 很緊急</span>
//B后臺(tái)代碼
import {Component, Input, OnInit} from '@angular/core';

@Component({
  selector: 'app-jinji',
  templateUrl: './jinji.component.html',
  styleUrls: ['./jinji.component.css']
})
export class JinjiComponent implements OnInit {
  @Input()
  private how: Number;

  constructor() {
  }

  ngOnInit() {
  }

}

最終效果圖:


image.png
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容