Ionic的Typescript类型在Ionic 2中给出错误

时间:2016-07-20 18:28:09

标签: typescript angular ionic2

我对下一个错误有疑问:

EXCEPTION: Error: Uncaught (in promise): EXCEPTION: Error in build/pages/search/search.html:17:14 
ORIGINAL EXCEPTION: Cannot find a differ supporting object 'function () {
    return [
        {
            "wrapperType": "track",
            "kind": "song",
            "artistId": 5869117,
            "collectionId": 459904774,
            "trackId": 459904911,
            "artistName": "Lil Wayne",
            "collectionName": "Tha Carter IV (Deluxe Edition)",
            "trackName": "John (feat. Rick Ross)",
            "collectionCensoredName": "Tha Carter IV (Deluxe Edition)",
            "trackCensoredName": "John (feat. Rick Ross)",
            "artistViewUrl": "https://itunes.apple.com/us/artist/lil-wayne/id5869117?uo=4",
            "collectionViewUrl": "https://itunes.apple.com/us/album/john-feat.-rick-ross/id459904774?i=459904911&uo=4",
            "trackViewUrl": "https://itunes.apple.com/us/album/john-feat.-rick-ross/id459904774?i=459904911&uo=4",
            "previewUrl": "http://a1557.phobos.apple.com/us/r1000/085/Music/de/c6/37/mzm.vjfeqrjw.aac.p.m4a",
            "artworkUrl30": "http://is4.mzstatic.com/image/thumb/Music/v4/c8/d2/f9/c8d2f98d-d2a8-35d0-fcd1-7adffc2560a9/source/30x30bb.jpg",
            "artworkUrl60": "http://is4.mzstatic.com/image/thumb/Music/v4/c8/d2/f9/c8d2f98d-d2a8-35d0-fcd1-7adffc2560a9/source/60x60bb.jpg",
            "artworkUrl100": "http://is4.mzstatic.com/image/thumb/Music/v4/c8/d2/f9/c8d2f98d-d2a8-35d0-fcd1-7adffc2560a9/source/100x100bb.jpg",
            "collectionPrice": 11.99,
            "trackPrice": 1.29,
            "releaseDate": "2011-08-29T07:00:00Z",
            "collectionExplicitness": "explicit",
            "trackExplicitness": "explicit",
            "discCount": 1,
            "discNumber": 1,
            "trackCount": 19,
            "trackNumber": 9,
            "trackTimeMillis": 286973,
            "country": "USA",
            "currency": "USD",
            "primaryGenreName": "Hip-Hop/Rap",
            "contentAdvisoryRating": "Explicit",
            "isStreamable": true
        }
    ];
}' of type 'function'. NgFor only supports binding to Iterables such as Arrays. ORIGINAL STACKTRACE: Error: Cannot find a differ supporting object 'function () {
    return [
        {
            "wrapperType": "track",
            "kind": "song",
            "artistId": 5869117,
            "collectionId": 459904774,
            "trackId": 459904911,
            "artistName": "Lil Wayne",
            "collectionName": "Tha Carter IV (Deluxe Edition)",
            "trackName": "John (feat. Rick Ross)",
            "collectionCensoredName": "Tha Carter IV (Deluxe Edition)",
            "trackCensoredName": "John (feat. Rick Ross)",
            "artistViewUrl": "https://itunes.apple.com/us/artist/lil-wayne/id5869117?uo=4",
            "collectionViewUrl": "https://itunes.apple.com/us/album/john-feat.-rick-ross/id459904774?i=459904911&uo=4",
            "trackViewUrl": "https://itunes.apple.com/us/album/john-feat.-rick-ross/id459904774?i=459904911&uo=4",
            "previewUrl": "http://a1557.phobos.apple.com/us/r1000/085/Music/de/c6/37/mzm.vjfeqrjw.aac.p.m4a",
            "artworkUrl30": "http://is4.mzstatic.com/image/thumb/Music/v4/c8/d2/f9/c8d2f98d-d2a8-35d0-fcd1-7adffc2560a9/source/30x30bb.jpg",
            "artworkUrl60": "http://is4.mzstatic.com/image/thumb/Music/v4/c8/d2/f9/c8d2f98d-d2a8-35d0-fcd1-7adffc2560a9/source/60x60bb.jpg",
            "artworkUrl100": "http://is4.mzstatic.com/image/thumb/Music/v4/c8/d2/f9/c8d2f98d-d2a8-35d0-fcd1-7adffc2560a9/source/100x100bb.jpg",
            "collectionPrice": 11.99,
            "trackPrice": 1.29,
            "releaseDate": "2011-08-29T07:00:00Z",
            "collectionExplicitness": "explicit",
            "trackExplicitness": "explicit",
            "discCount": 1,
            "discNumber": 1,
            "trackCount": 19,
            "trackNumber": 9,
            "trackTimeMillis": 286973,
            "country": "USA",
            "currency": "USD",
            "primaryGenreName": "Hip-Hop/Rap",
            "contentAdvisoryRating": "Explicit",
            "isStreamable": true
        }
    ];
}' of type 'function'. NgFor only supports binding to Iterables such as Arrays.
at new BaseException (http://localhost:8100/build/js/app.bundle.js:1805:23)
at NgFor.set [as ngForOf] (http://localhost:8100/build/js/app.bundle.js:540:27)
at DebugAppView._View_SearchPage0.detectChangesInternal (SearchPage.template.js:143:30)
at DebugAppView.AppView.detectChanges (http://localhost:8100/build/js/app.bundle.js:29958:14)
at DebugAppView.detectChanges (http://localhost:8100/build/js/app.bundle.js:30063:44)
at DebugAppView.AppView.detectViewChildrenChanges (http://localhost:8100/build/js/app.bundle.js:29984:19)
at DebugAppView.AppView.detectChangesInternal (http://localhost:8100/build/js/app.bundle.js:29969:14)
at DebugAppView.AppView.detectChanges (http://localhost:8100/build/js/app.bundle.js:29958:14)
at DebugAppView.detectChanges (http://localhost:8100/build/js/app.bundle.js:30063:44)
at ViewRef_.detectChanges (http://localhost:8100/build/js/app.bundle.js:30401:65) ERROR CONTEXT [object Object]

这是我的代码:

search.ts and search.html

我认为它与itemResults的类型有关:any;我确实放了任何东西,但它仍然给我错误。 这是带有打字稿的离子2应用程序,如果有人可以帮我这个,请给我一些提示。

2 个答案:

答案 0 :(得分:1)

我猜你可能意味着:

this.itemResults = this.getResults();//notice the paranthesis .. this is how you call the method.

您可能还想使用$onInit方法而不是构造函数

$onInit(){
  this.itemResults = this.getResults();
}

构造函数永远不应该加载数据或有任何副作用。

答案 1 :(得分:0)

因为您在没有括号的情况下执行this.itemResults = this.getResults;,所以您要为itemResults分配类型函数的对象,这就是错误所说的原因:

  

无法找到“function”类型的不同支持对象'function(){...}。 NgFor仅支持绑定到Iterables,例如Arrays。

该行代码应为this.itemResults = this.getResults();,以便执行该方法并将结果分配给itemResults属性。