码迷,mamicode.com
首页 > 其他好文 > 详细

【angular5项目积累总结】消息订阅服务

时间:2018-01-30 19:54:12      阅读:176      评论:0      收藏:0      [点我收藏+]

标签:edr   res   next   表单   pst   定义   组件   update   obs   

code

import { Injectable } from ‘@angular/core‘;
import { Subject } from ‘rxjs/Subject‘;
@Injectable()
export class CommonService {
    private notify = new Subject<any>();
    /**
     * Observable string streams
     */
    notifyObservable$ = this.notify.asObservable();

    constructor() { }

    public notifyOther(data: any) {
        if (data) {
            this.notify.next(data);
        }
    }
}

项目示例

表单提交后更新其他组件数据列表

定义:

  constructor(
        private router: Router,
        private actRouter: ActivatedRoute,
        private appStoreService: AppStoreService,
        private comService: CommonService) {
         this.subscribeUpdate(comService);
    }

 subscribeUpdate(comService: CommonService) {
        this.comService.notifyObservable$.subscribe(data => {
            if (data == ‘refreshWebApp‘) {
                this.loadWebApp();
            }
        }, error => {
            console.log(`subscribe error:${error}`)
        })
    }

 

调用:

 this.comService.notifyOther(‘refreshWebApp‘);

 

【angular5项目积累总结】消息订阅服务

标签:edr   res   next   表单   pst   定义   组件   update   obs   

原文地址:https://www.cnblogs.com/sybboy/p/8386285.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!