angular 发布订阅
生活随笔
收集整理的這篇文章主要介紹了
angular 发布订阅
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
創建service
import { Subject } from 'rxjs/Subject'; import { Observable } from 'rxjs/observable';export class MessageService {private subject = new Subject<any>();send(message: any) {this.subject.next(message);}get(): Observable<any> {return this.subject.asObservable();} }組件1 發布
import { Component, OnInit } from '@angular/core'; import { Subject } from 'rxjs/Subject'; import { MessageService } from '../../service/message.service';@Component({selector: 'app-video-demo-home',templateUrl: './video-demo-home.component.html',styleUrls: ['./video-demo-home.component.sass'] }) export class VideoDemoHomeComponent implements OnInit {private _clickPoint: Subject<any> = new Subject<any>();public name = 'www';constructor(public srv: MessageService) { }ngOnInit() {}clickBtn() {this.srv.send(this.name);}}組件2 訂閱
import { Component, OnInit } from '@angular/core'; import { MessageService } from '../../service/message.service';@Component({selector: 'app-subscribe-home',templateUrl: './subscribe-home.component.html',styleUrls: ['./subscribe-home.component.sass'] }) export class SubscribeHomeComponent implements OnInit {constructor(public srv: MessageService) { }public message = '';ngOnInit() {this.srv.get().subscribe((result) => {console.log('111111111111111111');this.message = result;console.log(this.message);});}}?
轉載于:https://www.cnblogs.com/stacey-zy/p/10815380.html
總結
以上是生活随笔為你收集整理的angular 发布订阅的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 莫队+带修莫队模板与总结
- 下一篇: D3.js比例尺 定量比例尺 之 线