sybboy

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\');

 

分类:

技术点:

相关文章: