angular2 組件之間通過(guò)service互相傳遞的實(shí)例
母組件傳值給子組件
母組件通過(guò)service傳值給子組件,很簡(jiǎn)單,聲明一個(gè)service
@Injectable()
export class ToolbarTitleService {
title:string;
}
然后在母組件中依賴(lài)注入
@Component({
selector: 'admin',
templateUrl: './admin.component.html',
styleUrls: ['./admin.component.scss'],
providers: [ToolbarTitleService],
})
子組件中直接聲明即可使用
export class RoleListComponent implements OnInit {
constructor(private toolbarTitleService:ToolbarTitleService) {
console.log(this.toolbarTitleService.title);
}
ngOnInit() { }
}
子組件傳值給母組件
那么我想反過(guò)來(lái)傳值回去該怎么辦,即使我在子組件注入了service,母組件也不會(huì)在我修改了servie的值之后得到通知,這時(shí)候就需要用到subscribe
service代碼:
import { Injectable } from '@angular/core';
import { Subject } from 'rxjs/Subject';
@Injectable()
export class ToolbarTitleService {
private titleSource = new Subject();
//獲得一個(gè)Observable
titleObservable =this.titleSource.asObservable();
constructor() { }
//發(fā)射數(shù)據(jù),當(dāng)調(diào)用這個(gè)方法的時(shí)候,Subject就會(huì)發(fā)射這個(gè)數(shù)據(jù),所有訂閱了這個(gè)Subject的Subscription都會(huì)接受到結(jié)果
emitTitle(title: string) {
this.titleSource.next(title);
}
}
子組件代碼:
import { ToolbarTitleService } from './../../common/toolbar-title.service';
import { Component, OnInit ,Output,EventEmitter} from '@angular/core';
@Component({
selector: 'role-list',
templateUrl: 'role-list.component.html',
styleUrls: ['./role-list.component.css'],
providers: [],
})
export class RoleListComponent implements OnInit {
constructor(private toolbarTitleService:ToolbarTitleService) {
//調(diào)用方法,發(fā)射數(shù)據(jù)
this.toolbarTitleService.emitTitle('角色列表');
}
ngOnInit() { }
}
母組件:
import { Component, OnInit } from '@angular/core';
import { ToolbarTitleService } from "app/common/toolbar-title.service";
import { Subscription } from "rxjs/Subscription";
@Component({
selector: 'admin',
templateUrl: './admin.component.html',
styleUrls: ['./admin.component.scss'],
providers: [ToolbarTitleService],
})
export class AdminComponent implements OnInit {
title: string;
subscription: Subscription;
constructor(private toolbarTitleService: ToolbarTitleService) {
//使用subscribe來(lái)訂閱,當(dāng)數(shù)據(jù)被發(fā)射出來(lái)的時(shí)候,這里就會(huì)接收到結(jié)果
this.subscription = this.toolbarTitleService.titleObservable.subscribe(src => console.log('得到的title:' + src));
}
ngOnInit() {
}
//銷(xiāo)毀的時(shí)候需要取消訂閱,因?yàn)橛嗛喼髸?huì)一直處于觀察者狀態(tài),不取消會(huì)導(dǎo)致泄露
ngOnDestroy() {
this.subscription.unsubscribe();
}
}
以上這篇angular2 組件之間通過(guò)service互相傳遞的實(shí)例就是小編分享給大家的全部?jī)?nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
angularjs實(shí)現(xiàn)對(duì)表單輸入改變的監(jiān)控(ng-change和watch兩種方式)
這篇文章主要介紹了angularjs通過(guò)ng-change和watch兩種方式實(shí)現(xiàn)對(duì)表單輸入改變的監(jiān)控,需要的朋友可以參考下2018-08-08
利用Angularjs和原生JS分別實(shí)現(xiàn)動(dòng)態(tài)效果的輸入框
現(xiàn)在的很多網(wǎng)站都將輸入框做成了動(dòng)態(tài)的效果,這樣對(duì)于用戶(hù)體檢來(lái)說(shuō)非常好,這篇文章分別用Angularjs和原生JS兩種方法來(lái)實(shí)現(xiàn)動(dòng)態(tài)效果的輸入框,具有一定的參考價(jià)值,有需要的小伙伴們可以來(lái)參考借鑒。2016-09-09
Angular的$http的ajax的請(qǐng)求操作(推薦)
這篇文章主要介紹了Angular的$http的ajax的請(qǐng)求操作的相關(guān)資料,需要的朋友可以參考下2017-01-01
對(duì)angular 實(shí)時(shí)更新模板視圖的方法$apply詳解
今天小編就為大家分享一篇對(duì)angular 實(shí)時(shí)更新模板視圖的方法$apply詳解,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-10-10
整理AngularJS框架使用過(guò)程當(dāng)中的一些性能優(yōu)化要點(diǎn)
這篇文章主要介紹了AngularJS框架使用過(guò)程當(dāng)中的一些性能優(yōu)化要點(diǎn),Angular通常被用來(lái)制作大型單頁(yè)應(yīng)用,因而性能問(wèn)題也是必須考慮的因素,需要的朋友可以參考下2016-03-03
關(guān)于angular瀏覽器兼容性問(wèn)題的解決方案
這篇文章主要給大家介紹了關(guān)于angular瀏覽器兼容性問(wèn)題的解決方案,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者使用angular具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-07-07
Angular使用 ng-img-max 調(diào)整瀏覽器中的圖片的示例代碼
本篇文章主要介紹了Angular使用 ng-img-max 調(diào)整瀏覽器中的圖片的示例代碼,具有一定的參考價(jià)值,有興趣的可以了解一下2017-08-08

