Angular 非父子組件間的service數據通信


完成思路:以service.ts(主題subject---訂閱sbuscribe模式)為數據中轉中間件,通過sku.ts的數據更改監測機制,同步更改service.ts中的數據,同時buy.ts組件實時接收service.ts的變化后的數據完成數據共享傳遞。

1、定義service.ts共享數據中轉ts文件

 1 import {Injectable} from '@angular/core';
 2 import { Subject } from "rxjs/Subject";
 3 
 4 export class Sku{
 5     skuInfo:{}
 6 }
 7 @Injectable()
 8 
 9 export class TongxinProvider {
10    public skuList:Sku = {
11         skuInfo:{}
12     };
13     constructor(
14     ) {
15         console.log('Hello TongxinProvider Provider');
16     }
17 //實例化主題Subject類對象
18     private Source=new Subject<any>();
19     public Status$=this.Source.asObservable();
20 // 定義數據傳遞函數
21 // 商品詳情頁商品規格選擇與購買組件交互
22     public skuBuy(data){
23         this.skuList.skuInfo = data;
24         this.Source.next(this.skuList.skuInfo);
25     }
26 
27 }

 

  

2、商品規格列表sku.ts組件向外部其他組件發送當前選中的商品規格信息

 1  1 import {Component, Input, OnChanges} from '@angular/core';
 2  2 import {TongxinProvider} from "../../providers/tongxin";
 3  3 import {ValidateProvider} from "../../providers/validate";
 4  4 import {PopProvider} from "../../providers/pop";
 5  5 
 6  6 @Component({
 7  7     selector: 'good-sku',
 8  8     templateUrl: 'good-sku.html'
 9  9 })
10 10 export class GoodSkuComponent implements OnChanges {
11 11     // angular @Input接受父組件傳遞的商品規格數據
12 12     @Input() skuInfo: object = {};
13 13     public colorSku:number = 0;
14 14     public sizeSku:number = 0;
15 15     public goodNum:number = 0;
16 16     constructor(public TongXin: TongxinProvider,
17 17                 public Validate: ValidateProvider,
18 18                 public Pop:PopProvider) {
19 19         console.log('Hello GoodSkuComponent Component');
20      // 當組件數據不是有父級組件的可輸入屬性導入而是當前組件的靜態數據時,需先在construcor函數里首先調用下,手動向buy.ts導入初始化數據,服務才能正常起作用。如:
21         this.changeSku();
22     
23 20     }
24 21     // 在ngOnChanges中初始化調用導入商品規格數據到service.ts服務中去
25 22     ngOnChange方法會在this.skuInfo數據發生改變是自動調用執行
26 23     ngOnChanges(){
27 24         this.goodNum = this.skuInfo['stock'];
28 25         this.changeSku();
29 26     }
30 27     // 更改發布商品規格信息
31 28     public changeSku() {
32 29         this.TongXin.skuBuy(this.skuInfo);
33 30     }
34 31     //商品規格一切換
35 32     public setColorSku(index) {
36 33         this.colorSku = index;
37 34     }
38 35     // 商品規格二切換
39 36     public setSizeSku(index) {
40 37         this.sizeSku = index;
41 38     }
42 39     // 增加商品數量
43 40     public addGood(){
44 41         console.log(this.goodNum)
45 42         if(this.skuInfo['num'] >= this.goodNum){
46 43             this.Pop.toast("不能再多了!");
47 44             return false;
48 45         }
49 46         this.skuInfo['num'] ++;
50 47         this.skuInfo['stock'] --;
51 48     }
52 49     // 減少商品數量
53 50     public descGood(){
54 51         if(this.skuInfo['num'] <= 1){
55 52             this.Pop.toast("不能再少了!");
56 53             return false;
57 54         }
58 55         this.skuInfo['num'] --;
59 56         this.skuInfo['stock'] ++;
60 57     }
61 58 }    

 

 

3、buy.ts組件接收sku.ts組件發送的規格信息

 1 import {Component, Input, OnInit, Output} from '@angular/core';
 2 import { PopProvider } from "../../providers/pop";
 3 import {serviceProvider} from "../../providers/service";
 4 
 5 @Component({
 6     selector: 'share-header',
 7     templateUrl: 'share-header.html'
 8 })
 9 export class ShareHeaderComponent implements OnInit{
10 
11     @Input() buy: string = '';
12     
13     //聲明事件發射器
14     @Output() checkEmitter = new EventEmitter<boolean>();
15     private isLogin:boolean = false;
16     public goodSku:Object = {};
17     constructor(public Pop:PopProvider,
18                 public navCtrl:NavController,
19                 public navParams:NavParams,
20                 public service:serviceProvider) {
21         console.log('Hello ShareHeaderComponent Component');
22     }
23     // 在ngOnInit(組件初始化完成以后)接收sku.ts組件發送的數據並保存到this.goodSku屬性中,便於buyGood方法中調用商品規格數據
24     ngOnInit(): void {
25         this.getSkuInfo();
26     }
27     public getSkuInfo(){
28        this.service.Status$.subscribe(res=>{
29              // 至此完成了由sku.ts組件到buy.ts組件的數據共享傳遞流程
30             this.goodSku  = res;
31         })
32     }
33 
34     // 點擊購買商品
35     public buyGood(id) {
36 
37         console.log(this.goodSku)
38 
39     }
40 }

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM