【问题标题】:Getting Not Supported Error while using Ionic3使用 Ionic3 时出现不支持的错误
【发布时间】:2017-05-15 11:45:07
【问题描述】:

我扼杀了能够捕捉到这个错误。 在桌面上,此代码引发此NotSupportedError

我通常在 chrome 上调试。

代码如下:

import {Component} from "@angular/core";
import {ScreenOrientation} from "@ionic-native/screen-orientation";

@IonicPage()
@Component({
  selector: 'page-loading',
  templateUrl: 'page-loading.html',
})
export class PageLoading {

  constructor(private screenOrientation:ScreenOrientation) {}

  ionViewDidLoad() {
    console.log('ionViewDidLoad PageLoading');

    try{
        this.screenOrientation.lock(this.screenOrientation.ORIENTATIONS.PORTRAIT).then(()=>{
          console.log('lock');
        });
    }catch(e){
      console.warn('No cordova.js')
    }

  }

}

【问题讨论】:

  • 离子原生或者cordova不适用于离子服务..你只需要在设备或模拟器中运行
  • 是的,我知道,但是对于其他没有问题的插件,他们抱怨沉默。我只是想在我的桌面上消除这个错误,但是 try catch 没有帮助
  • 哦,你的意思是在离子视图屏幕中?
  • 不,我像你说的那样使用离子服务,我只是不知道为什么我能捕捉到这些错误

标签: angular ionic3


【解决方案1】:

您可以创建一个类来模拟离子原生类,如文档here 中所述。

class ScreenOrientationMock extends ScreenOrientation {
  lock(type) {
    return new Promise((resolve, reject) => {
      resolve("locked");
    })
  }
}

ngModule 的提供者列表中提到它应该使用你的模拟类而不是实际的离子原生类。

providers: [..
    { provide: ScreenOrientation, useClass: ScreenOrientationMock }
 ]

这将返回您在resolve 中为ionic serve 期间设置的屏幕方向。

一旦在设备中运行,您就可以将其删除。

编辑:

还有另一种可能来抑制你的错误,所以你最终将无事可做:

if(this.platform.is('cordova')){
  this.platform.ready().then(()=>{
   this.screenOrientation.lock(this.screenOrientation.ORIENTATIONS.PORTRAIT);
  })
}

【讨论】:

  • remove it once it is run in a device. 你的意思是我们在部署应用程序时必须稍后手动更改代码以提供真实的类而不是模拟?或者有没有办法检测您正在运行的设备并自动提供模拟?
  • 嗨,不,事实上你不应该在最后使用任何模拟。我更新响应
  • @DavidF。谢谢..您可以添加自己的答案
猜你喜欢
  • 2020-11-07
  • 2015-12-02
  • 1970-01-01
  • 1970-01-01
  • 2012-11-04
  • 2023-01-02
  • 1970-01-01
  • 1970-01-01
  • 2021-12-26
相关资源
最近更新 更多