在这方面,我需要您的帮助:两个月前,我的Ionic 2应用程序在使用ScreenOrientation cordova插件时工作正常,代码如下:
window.addEventListener('orientationchange', ()=>{
console.info('DEVICE ORIENTATION CHANGED!');
console.debug(ScreenOrientation.orientation);
if(ScreenOrientation.orientation.type.indexOf('landscape') !== -1){
this.screenOrientationIsPortrait = false;
} else if(ScreenOrientation.orientation.type.indexOf('portrait') !== -1){
this.screenOrientationIsPortrait = true;
}
});
在一台新的笔记本电脑中,我安装了最新的Ionic 2版本和最近版本的cordova ScreenOrientation插件,并使用相同的应用程序代码,我在编译时收到了这个错误:
属性“type”不存在于“string”类型上。
我尝试使用ScreenOrientation插件的github存储库中的示例:
window.addEventListener('orientationchange', ()=>{
console.info('DEVICE ORIENTATION CHANGED!');
console.debug(ScreenOrientation.orientation);
if(screen.orientation.type.indexOf('landscape') !== -1){
this.screenOrientationIsPortrait = false;
} else if(screen.orientation.type.indexOf('portrait') !== -1){
this.screenOrientationIsPortrait = true;
}
});
但是有了这些代码,我也会在编译时得到这个错误:
属性“取向”在“屏幕”类型上不存在
Ionic 2和ScreenOrientation插件都是最近更新的。根据我的研究,这是一个TypeScript错误,而不是更新版本冲突。
我怎么知道出什么事了?如何调试才能找到新错误的原因?有什么想法吗?谢谢,谢谢你的帮助。
Github回购示例:https://github.com/apache/cordova-plugin-screen-orientation
离子2例:https://ionicframework.com/docs/v2/native/screen-orientation/
发布于 2017-01-04 20:59:21
您实际上可以检测到屏幕方向的变化,而不需要添加插件。使用类型窗口的属性方向。
window.addEventListener('orientationchange', () => {
console.info('DEVICE ORIENTATION CHANGED!');
console.debug(ScreenOrientation.orientation);
switch (window.orientation) {
case -90:
case 90:
console.log("Landscape orientation");
this.screenOrientationIsPortrait = true;
break;
case 0:
console.log("Landscape orientation");
this.screenOrientationIsPortrait = false;
break;
}
});
https://stackoverflow.com/questions/41476962
复制