Reputation: 7359
Hi I am Working With Ionic2
.Here I got some errors while using JSON.parse(this.items1)
Actually that is not an error in Javascript
but it shows error in ts file ... How to avoid these errors?
Error
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)
Code
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);
}
}
}
}
Upvotes: 1
Views: 349
Reputation: 44659
Argument of type 'any[]' is not assignable to parameter of type 'string'
The reason you're getting that error just in Typescript is because items1
is of type any[] and you're assigning it a string (this.items1 = res
).
It's not an error in execution time, because you can assign what ever you want to a variable in JS and it won't complain about it as far as the methods you invoke or the things you do with it don't throw an exception.
To avoid issues like that one, try to explicitly declare the types of the properties you're using in your code
private items: Array<any> = []; // Instead of any you can declare even how the objects inside the array are
And keep in mind types when assigning values to them
this.items1 = JSON.parse(res); // Instead of this.items1 = res;
Upvotes: 1