Lock orientation with cordova-plugin-screen-orientation

时间:2017-08-13 13:59:15

标签: cordova ionic2

I'm using the plugin in my Ionic2 application:

cordova-plugin-screen-orientation 2.0.1 "Screen Orientation"

app.component.ts

import { ScreenOrientation } from '@ionic-native/screen-orientation';

export class MyApp {
  constructor(screenOrientation: ScreenOrientation) {
    screenOrientation.lock(screenOrientation.ORIENTATIONS.LANDSCAPE);
  }
}

package.json

"cordova": {
    "plugins": {
      "cordova-plugin-console": {},
      "cordova-plugin-device": {},
      "cordova-plugin-statusbar": {},
      "cordova-plugin-whitelist": {},
      "ionic-plugin-keyboard": {},
      "cordova-plugin-http": {},
      "cordova-plugin-screen-orientation": {}
    },
    "platforms": [
      "android",
      "browser",
      "ios"
    ]
  }

Why the application not working ? error:

NotSupportedError: Operation is not supported

1 个答案:

答案 0 :(得分:1)

您应该从ionic-native导入Platform并将ScreenOrientation来电包裹在Platform.ready()来电。 ready函数返回Promise,该平台在平台准备就绪时可以解析,并且可以调用本机功能。它现在不适合你的原因可能是因为在调用函数时平台还没有准备好。

import { Platform } from 'ionic-angular';
import { ScreenOrientation } from '@ionic-native/screen-orientation';

export class MyApp {
    constructor(private screenOrientation: ScreenOrientation, private platform: Platform) {
        platform.ready().then(() => {
            this.screenOrientation.lock(screenOrientation.ORIENTATIONS.LANDSCAPE);
        });
    }
}