1
嗨,我正在使用Ionic2
。我在使用JSON.parse(this.items1)
時出現了一些錯誤實際上,這不是Javascript
中的錯誤,但它顯示了ts文件中的錯誤...如何避免這些錯誤?如何避免離子2應用中的打字稿錯誤?
錯誤
TypeScript error: E:/Projects 2016/TaskBean/mytaskbeanapp/app/pages/list/list.ts
(38,21): Error TS2345: Argument of type 'any[]' is not assignable to parameter o
f type 'string'.
10.3 MB bytes written (2.13 seconds)
代碼
import {Component,Type} from '@angular/core';
import {ModalController, NavController, NavParams,MenuController} from 'ionic-angular';
import {ItemDetailsPage} from '../item-details/item-details';
import {AddItemPage} from '../addtask/add';
import {ItemDetailPage} from '../item-detail/item-detail';
import {Data} from '../providers/data';
import {Http, Response, Headers, Request, RequestOptions, RequestMethod, URLSearchParams} from
'@angular/http';
import 'rxjs/Rx';
@Component({
templateUrl: 'build/pages/list/list.html',
})
export class ListPage extends Type {
selectedItem: any;
private items = [];
private items1 = [];
result: Object;
public mydata = [];
projectid:string;
constructor(private nav: NavController, navParams: NavParams,public menu:MenuController,private dataService: Data, private modalCtrl: ModalController,public http:Http) {
this.result = {friends:[]};
http.get('http://192.168.0.26:9000/api/task/counttask?projectid=-1&Teamid=-1&RoleId=-1&status=1&PID=-1&mytasks=0&alltasks=0&page=1&row=15&priorityid=-1&typeid=-1&taskname=&JSON&_=1471520478215')
.map((res: Response) => res.json())
.subscribe(
res => { this.items1 = res;
**alert(JSON.parse(this.items1))**
try
{
alert(JSON.parse(JSON.parse(this.items1).Data));
window.localStorage['mydata']=this.items;
}
catch(e)
{
}
},
() => console.log('getUserStatus Complete') // complete
);
this.selectedItem = navParams.get('item');
this.menu.enable(true, 'mymenu');
this.dataService.getData().then((todos) => {
if(todos){
this.items = JSON.parse(todos);
}
});
}
itemTapped(event, item) {
this.nav.push(ItemDetailsPage, {
item: item
});
}
addItem(){
let addModal = this.modalCtrl.create(AddItemPage);
addModal.onDidDismiss((item) => {
if(item){
this.saveItem(item);
}
});
addModal.present();
}
saveItem(item){
this.items.push(item);
this.dataService.save(this.items);
}
viewItem(item){
this.nav.push(ItemDetailPage, {
item: item
});
}
removeItem(item){
for(var i = 0; i < this.items.length; i++) {
if(this.items[i] == item){
this.items.splice(i, 1);
}
}
}
}
非常感謝Lot @sebaferreras其工作 – Gopinath
Np,很高興我能幫忙:) – sebaferreras