我需要尝试将JSON数据连接到我的应用程序的帮助,拥有本地存储的JSON数据文件可以正常工作,但是当我将其重定向到Firebase数据库时,它将无法正常工作。
这是处理条形码扫描器工作方式的主要代码
Home.ts
import { Component } from '@angular/core';
import { NavController } from 'ionic-angular';
import { BarcodeScanner } from '@ionic-native/barcode-scanner';
import { Toast } from '@ionic-native/toast';
import { DataServiceProvider } from '../../providers/data-service/data-service';
@Component({
selector: 'page-home',
templateUrl: 'home.html'
})
export class HomePage {
products: any[] = [];
selectedProduct: any;
productFound:boolean = false;
constructor(public navCtrl: NavController,
private barcodeScanner: BarcodeScanner,
private toast: Toast,
public dataService: DataServiceProvider) {
this.dataService.getProducts()
.subscribe((response)=> {
this.products = response
console.log(this.products);
});
}
scan() {
this.selectedProduct = {};
this.barcodeScanner.scan().then((barcodeData) => {
this.selectedProduct = this.products.find(product => product.sku[0] === barcodeData.text);
if(this.selectedProduct !== undefined) {
this.productFound = true;
console.log(this.selectedProduct);
} else {
this.selectedProduct = {};
this.productFound = false;
this.toast.show('Product not found', '5000', 'center').subscribe(
toast => {
console.log(toast);
}
);
}
}, (err) => {
this.toast.show(err, '5000', 'center').subscribe(
toast => {
console.log(toast);
}
);
});
}
}
从本地存储的json文件获取数据的部分,我也希望它可以访问firebase数据库。
DataServiceProvider.ts
import { Injectable } from '@angular/core';
import { Http, Response } from '@angular/http';
import 'rxjs/add/operator/map';
/*
Generated class for the DataServiceProvider provider.
See https://angular.io/guide/dependency-injection for more info on providers
and Angular DI.
*/
@Injectable()
export class DataServiceProvider {
constructor(public http: Http) {
console.log('Hello DataServiceProvider Provider');
}
getProducts(){
return this.http.get('https://acis-676.firebaseio.com/0')
.map((response:Response)=>response.json());
}
}
在json文件的卡片视图中显示数据的主主页。 Home.html
<ion-header>
<ion-navbar>
<ion-title>
Ionic Blank
</ion-title>
</ion-navbar>
</ion-header>
<ion-content padding>
<p>
Internal Inventory System for
</p>
<h1>
<button ion-button color="dark" full round (click)="scan()">Start Scan</button>
</h1>
<ion-card *ngIf="productFound">
<ion-card-header>
<h2>Product Name: {{selectedProduct.name}}</h2>
</ion-card-header>
<ion-card-content>
<ul>
<p>SKU: {{selectedProduct.sku[0]}}</p>
<p>Stock: {{selectedProduct.stock[0]}}</p>
</ul>
</ion-card-content>
</ion-card>
</ion-content>