我想將一個組件的值傳遞給另一個組件。從一個組件調用函數到另一個組件Angularjs 2
即,我需要從儀表板組件傳遞值標題組件
這裏是我的儀表板組件
import{Component}from '@angular/core';
import { Header } from '../../layout/header.component';
export class Dashboard{
showAlert(id : any)
{
setItem(id);
}
}
這裏是我的頭組件
import { Component, OnInit } from '@angular/core';
@Component({
selector: 'header',
templateUrl: './header.component.html',
})
export class Header{
public setItem(id : any)
{
console.log('Exported value'+id)
}
}
但它總是給找不到setIte m
我在做什麼錯誤,我該如何解決這個問題?
注:我做這Angularjs 2
這是不是真的,以延長或使用零部件的合適方式。但是,一旦你導入類,它應該是如果它不是一個靜態類:(新標題).setItem(id) – mygeea
你應該閱讀:https://angular.io/guide/component-interaction。你也可以使'setItem'方法成爲靜態的,並且像這樣調用它:'Header.setItem(id)',但是當使用靜態方法時(如果你不知道靜態方法是什麼)時要小心。 – echonax