Angular5.0 子組件通過service傳遞值給父組件的方法
一、引言
我們使用ngx-loading,需要在app.component.html上寫模板,綁定一個(gè)布爾值loading.此時(shí)如果我們想在其他組件中使用這個(gè)loading控件,就需要在每個(gè)組件的html重新寫模板,這就顯得累贅了。在此,我們以ngx-loading為例,展示子組件如何通過service改變app組件中的布爾值loading。
// app.component.html
<ngx-loading [show]="loading" [config]="{ backdropBorderRadius: '14px' }"></ngx-loading>
二、實(shí)現(xiàn)
1.安裝ngx-loading 詳情點(diǎn)擊
npm install --save ngx-loading
2.Import the LoadingModule
import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';
import { CoreModule } from './core/core.module';
import { LoadingModule } from 'ngx-loading';
@NgModule({
//...
imports: [
//...
LoadingModule
],
//...
})
export class AppModule { }
3.在app.component.html寫ngx-loading模板
// app.component.html
<ngx-loading [show]="loading" [config]="{ backdropBorderRadius: '14px' }"></ngx-loading>
4.新建一個(gè)UtilsService
import {Injectable} from '@angular/core';
import {Subject} from 'rxjs/Subject';
@Injectable()
export class UtilsService {
private loadingSource = new Subject();
// 獲得一個(gè)Observable;
loadingObservable = this.loadingSource.asObservable();
// 發(fā)射數(shù)據(jù),當(dāng)調(diào)用這個(gè)方法的時(shí)候,Subject就會(huì)發(fā)射這個(gè)數(shù)據(jù),所有訂閱了這個(gè)Subject的Subscription都會(huì)接受到結(jié)果
// loading true為啟用loading,false為關(guān)閉loading
emitBoolean(loading: boolean) {
this.loadingSource.next(loading);
}
}
5.在app.component.ts使用subscribe來訂閱,當(dāng)數(shù)據(jù)被發(fā)射出來的時(shí)候,這里就會(huì)接收到結(jié)果
import {Component, OnDestroy, OnInit} from '@angular/core';
import {Subscription} from "rxjs/Subscription";
import {UtilsService} from "./service/utils.service";
@Component({
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.css']
})
export class AppComponent implements OnInit, OnDestroy {
loading = false;
subscription: Subscription;
constructor(public utils: UtilsService) {
// 使用subscribe來訂閱,當(dāng)數(shù)據(jù)被發(fā)射出來的時(shí)候,這里就會(huì)接收到結(jié)果
this.subscription = this.utils.loadingObservable.subscribe(loading => this.loading = Boolean(loading));
}
ngOnInit() {
}
/* 銷毀的時(shí)候需要取消訂閱,因?yàn)橛嗛喼髸?huì)一直處于觀察者狀態(tài),不取消會(huì)導(dǎo)致泄露*/
ngOnDestroy() {
this.subscription.unsubscribe();
}
}
6.在其他子組件需要啟用或關(guān)閉loading時(shí),只需要一行代碼。
constructor( private utils: UtilsService) {
}
this.utils.emitBoolean(true); // 啟用loading
this.utils.emitBoolean(false); // 關(guān)閉loading
7.額外方法:在子組件注入AppComponent,簡(jiǎn)單粗暴,但不推薦……
constructor(public appComponent: AppComponent) {
}
this.appComponent.loading = true; // 啟用loading
this.appComponent.loading = false; // 關(guān)閉loading
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
angular 實(shí)時(shí)監(jiān)聽input框value值的變化觸發(fā)函數(shù)方法
今天小編就為大家分享一篇angular 實(shí)時(shí)監(jiān)聽input框value值的變化觸發(fā)函數(shù)方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2018-08-08
在angularJs中進(jìn)行數(shù)據(jù)遍歷的2種方法
今天小編就為大家分享一篇在angularJs中進(jìn)行數(shù)據(jù)遍歷的2種方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2018-10-10
微信小程序?qū)崿F(xiàn)左右聯(lián)動(dòng)的實(shí)戰(zhàn)記錄
聯(lián)動(dòng)菜單是大家在開發(fā)小程序經(jīng)常會(huì)遇到的一個(gè)功能,下面這篇文章主要給大家介紹了關(guān)于微信小程序?qū)崿F(xiàn)左右聯(lián)動(dòng)的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),需要的朋友可以參考借鑒,下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2018-07-07
Angular2表單自定義驗(yàn)證器的實(shí)現(xiàn)
本文給大家介紹angular2表單自定義驗(yàn)證器的實(shí)現(xiàn),本文給大家介紹的非常詳細(xì),具有參考借鑒價(jià)值,感興趣的朋友一起看看吧2016-10-10
詳解使用angular框架離線你的應(yīng)用(pwa指南)
這篇文章主要介紹了詳解使用angular框架離線你的應(yīng)用(pwa指南),小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2019-01-01
Angular ng-repeat遍歷渲染完頁面后執(zhí)行其他操作詳細(xì)介紹
這篇文章主要介紹了Angular ng-repeat遍歷渲染完頁面后執(zhí)行其他操作詳細(xì)介紹的相關(guān)資料,需要的朋友可以參考下2016-12-12

