2
我創建一個ionic 2 application
與購物車功能,但我問題當我save the item in the cart
和去其他頁面在這種情況下和開放the cart
車是空離子2保存和檢索的購物車項目的localStorage
我的問題:
如何創建一個購物車功能並從下面的代碼中檢索另一個頁面?
這裏是我的代碼:
用戶data.ts:
import { Injectable } from '@angular/core';
import { Events, LocalStorage, Storage } from 'ionic-angular';
@Injectable()
export class UserData {
_cart = [];
HAS_LOGGED_IN = 'hasLoggedIn';
storage = new Storage(LocalStorage);
constructor(public events: Events) {}
hasItem(item) {
return (this._cart.indexOf(item) > -1);
}
addToCart(item) {
this._cart.push(item);
}
removeFromCart(item) {
let index = this._cart.indexOf(item);
//item.checked=false;
if (index > -1) {
this._cart.splice(index, 1);
}
}
clearCart(){
this._cart = [];
}
indexOfItem(item) {
return this._cart.indexOf(item);
}
countOfCart() {
return this._cart.length;
}
login(name) {
this.storage.set(this.HAS_LOGGED_IN, true);
this.setUsername(name);
this.events.publish('user:login');
}
signup() {
this.storage.set(this.HAS_LOGGED_IN, true);
this.events.publish('user:signup');
}
logout() {
this.storage.remove(this.HAS_LOGGED_IN);
this.storage.remove('username');
this.events.publish('user:logout');
}
setUsername(username) {
this.storage.set('username', username);
}
getUsername() {
return this.storage.get('username').then((value) => {
return value;
});
}
// return a promise
hasLoggedIn() {
return this.storage.get(this.HAS_LOGGED_IN).then((value) => {
return value;
});
}
}
是的,這是正確的100%,謝謝你的迴應:)。 – mahmoudismail
很高興我能幫忙:) – sebaferreras