使用Ionic3时出现不受支持的错误

问题描述 投票:2回答:1

我很努力地抓住这个错误。在台式机上,此代码引发此NotSupportedError

enter image description here

我通常在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')
    }

  }

}
angular ionic3
1个回答
2
投票

您可以创建一个类来模拟文档本机类,如文档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);
  })
}
© www.soinside.com 2019 - 2024. All rights reserved.