首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
社区首页 >问答首页 >离子2 ScreenOrientation插件的变化

离子2 ScreenOrientation插件的变化
EN

Stack Overflow用户
提问于 2017-01-05 03:39:17
回答 1查看 2.4K关注 0票数 0

在这方面,我需要您的帮助:两个月前,我的Ionic 2应用程序在使用ScreenOrientation cordova插件时工作正常,代码如下:

代码语言:javascript
代码运行次数:0
运行
复制
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存储库中的示例:

代码语言:javascript
代码运行次数:0
运行
复制
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/

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2017-01-05 04:59:21

您实际上可以检测到屏幕方向的变化,而不需要添加插件。使用类型窗口的属性方向。

代码语言:javascript
代码运行次数:0
运行
复制
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;
      }      
    });
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/41476962

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档