【问题标题】:Typescript Casting error in Angular 2 / Ionic 2Angular 2 / Ionic 2中的打字稿铸造错误
【发布时间】:2016-10-06 12:50:54
【问题描述】:

我正在 ionic 2 中编写一个简单的应用程序,但我的 anc(Angular 编译器)有问题,因为当我运行 ionic serve 它给我警告但应用程序在浏览器上正常运行,但是当我尝试构建对于我的手机,它坏了,构建过程会导致错误。

  var tempResults: ResultModel = new ResultModel(currentOne.id, (me.res.length > 1) ? me.res[0] : me.res[0], (me.res.length > 1) ? me.res[1] : me.res[0], currentOne.question, currentOne.imagePath);
    console.log(localforage.hasOwnProperty('result'))
    localforage.getItem('results').then(data=> {
      var allResponses: Array<ResultModel> = new Array<ResultModel>();
      if (data != null) {

        var allResponses: Array<ResultModel> = <Array<ResultModel>>JSON.parse(data);
            allResponses.push(tempResults);
        localforage.setItem('results', JSON.stringify(allResponses));
      } else {
        allResponses.push(tempResults);
        localforage.setItem('results', JSON.stringify(allResponses));
      }
    });

触发错误的行是: var allResponses: Array = >JSON.parse(data);

错误是:

ngc 错误:错误:C:/Users/.../result-item.ts:23:52 错误:“{}”类型的参数不可分配给“字符串”类型的参数。

我在编译器中遇到类型错误,我开始认为这可能是一个错误。

【问题讨论】:

  • .then((data: string) =&gt; { 或许可行

标签: angular typescript ionic2


【解决方案1】:

根据文档, json.parse() 需要一个字符串作为输入,我相信您的 data 参数是一个对象。

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/parse

【讨论】:

  • 是的,您实际上是对的:/问题是它在浏览器上正常运行并正确解析。感谢您的快速响应!
猜你喜欢
  • 2017-10-21
  • 1970-01-01
  • 2018-11-17
  • 2019-11-28
  • 2016-04-07
  • 2017-06-22
  • 2017-10-10
  • 2017-05-02
  • 1970-01-01
相关资源
最近更新 更多