使用 Ionic3 时出现不支持错误

Dav*_* F. 5 ionic3 angular

我扼杀了能够捕捉到这个错误。在桌面上,此代码引发了以下问题 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')
    }

  }

}
Run Code Online (Sandbox Code Playgroud)

Sur*_*Rao 6

你可以创建一个类的文档中描述嘲笑离子本地类在这里。

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

在您的提供者列表中ngModule提到它应该使用您的模拟类而不是实际的离子本机类。

providers: [..
    { provide: ScreenOrientation, useClass: ScreenOrientationMock }
 ]
Run Code Online (Sandbox Code Playgroud)

这将返回您resolve在ionic serve.

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

编辑:

还有另一种可能性可以抑制您的错误,因此您最终将无事可做:

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