我很努力地抓住这个错误。在台式机上,此代码引发此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')
}
}
}
您可以创建一个类来模拟文档本机类,如文档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);
})
}