Angular 订阅IONIC2提供程序与页面和自定义组件不同步
这是我的可订阅购物车提供商,其中Angular 订阅IONIC2提供程序与页面和自定义组件不同步,angular,ionic2,angular-promise,subscribe,Angular,Ionic2,Angular Promise,Subscribe,这是我的可订阅购物车提供商,其中.Cart是可订阅的 @Injectable() export class CartProvider { cart: Observable<Array<CartItem>> private _cart: BehaviorSubject<Array<CartItem>>; private cartStore: Array<CartItem> = []; constructor(priva
.Cart
是可订阅的
@Injectable()
export class CartProvider {
cart: Observable<Array<CartItem>>
private _cart: BehaviorSubject<Array<CartItem>>;
private cartStore: Array<CartItem> = [];
constructor(private dialogs: DialogsProvider) {
this._cart = <BehaviorSubject<Array<CartItem>>>new BehaviorSubject([]);
this.cart = this._cart.asObservable();
}
/**
* return obs array cart
*
* @returns {Observable<Array<CartItem>>}
* @memberof CartProvider
*/
getCart(): Observable<Array<CartItem>> {
return this.cart;
}
更新:: 我通过像这样将CartProvider添加到CustomComponent providers数组中解决了这个问题
import { NgModule } from '@angular/core';
import { IonicPageModule } from 'ionic-angular';
import { CartComponent } from './cart';
import { CartProvider } from "../../providers/cart/cart";
@NgModule({
declarations: [
CartComponent,
],
imports: [
IonicPageModule.forChild(CartComponent),
],
exports: [
CartComponent
],
providers:[
CartProvider // <-------- added here instead of adding it to component.providers array
]
})
export class CartComponentModule {}
从'@angular/core'导入{NgModule};
从“离子角度”导入{IonicPageModule};
从“./cart”导入{CartComponent};
从“./../providers/cart/cart”导入{CartProvider}”;
@NGD模块({
声明:[
CartComponent,
],
进口:[
IonicPageModule.forChild(CartComponent),
],
出口:[
CartComponent
],
供应商:[
CartProvider//
<button ion-button clear icon-only (click)="showCart()">
<ion-icon name="cart"></ion-icon>
<span class="badge" *ngIf="cartDisplay > 0">{{cartDisplay}}</span>
</button>
import { NgModule } from '@angular/core';
import { IonicPageModule } from 'ionic-angular';
import { CartComponent } from './cart';
import { CartProvider } from "../../providers/cart/cart";
@NgModule({
declarations: [
CartComponent,
],
imports: [
IonicPageModule.forChild(CartComponent),
],
exports: [
CartComponent
],
providers:[
CartProvider // <-------- added here instead of adding it to component.providers array
]
})
export class CartComponentModule {}