简体   繁体   English

任何类型的打字稿类型都会在Ionic 2产生错误

[英]Typescript type of any gives error at Ionic 2

I have question about the next error: 我对下一个错误有疑问:

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]

This is my code: 这是我的代码:

search.ts and search.html search.ts和search.html

I suppose that it has something to do with type of itemResults : any; 我想这与itemResults的类型有关:any; I did place any, but still it gives me error. 我确实放置了任何东西,但仍然给我错误。 This is ionic 2 application with typescript and if anybody can help me on this please give me some tips. 这是带有打字稿的ionic 2应用程序,如果有人可以帮助我,请给我一些提示。

I guess you probably meant: 我想你可能是说:

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

You might also want to use the $onInit method instead of the constructor 您可能还想使用$onInit方法而不是构造函数

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

The constructor should never load data or have any side effects. 构造函数永远不要加载数据或有任何副作用。

Because you're doing this.itemResults = this.getResults; 因为您正在执行this.itemResults = this.getResults; without the parenthesis, you're assigning an object of type function to itemResults and that's why the erros says: 如果没有括号, itemResults 类型为type对象分配给itemResults ,这就是错误提示的原因:

Cannot find a differ supporting object 'function () { ... } of type 'function'. 找不到类型为“功能”的其他支持对象 “功能(){...}”。 NgFor only supports binding to Iterables such as Arrays. NgFor仅支持绑定到Iterables,例如数组。

That line of code should be this.itemResults = this.getResults(); 那行代码应该是this.itemResults = this.getResults(); in order to execute the method and assign the result to the itemResults property. 为了执行该方法并将结果分配给itemResults属性。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM