Ionic 4 - 屏幕方向(横向)不起作用

Sab*_*iri 1 screen-orientation cordova ionic4

我是 Ionic 4 的新手,我正在尝试将屏幕方向设置为横向并参考文档,这就是我正在做的:

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

@Component({
  selector: 'app-root',
  templateUrl: 'app.component.html'
})
export class AppComponent {

  constructor(
    private platform: Platform,
    private splashScreen: SplashScreen,
    private statusBar: StatusBar,
    private screenOrientation: ScreenOrientation,
  ) {
    this.initializeApp();
  }

  initializeApp() {
    this.screenOrientation.lock(ScreenOrientation.ORIENTATIONS.LANDSCAPE);
    ...
  }
}
Run Code Online (Sandbox Code Playgroud)

在编译期间,我收到此错误:

[ng] src/app/app.component.ts(24,33) 中的错误:错误 TS2345:“字符串”类型的参数不可分配给“OrientationLockType”类型的参数。

并在浏览器控制台上:

未捕获的错误:无法解析 AppComponent 的所有参数:([object Object], [object Object], [object Object], ?)。在语法错误 (compiler.js:2426) [] ...

小智 6

您使用 ionic V4,请参阅文档,导入正确的内容。

import { ScreenOrientation } from '@ionic-native/screen-orientation/ngx';
Run Code Online (Sandbox Code Playgroud)

不是

import {ScreenOrientation} from '@ionic-native/screen-orientation';
Run Code Online (Sandbox Code Playgroud)


Ser*_*udo 5

第四个参数注入有问题,你是否正确安装了屏幕方向插件?

ionic cordova plugin add cordova-plugin-screen-orientation
npm install @ionic-native/screen-orientation
Run Code Online (Sandbox Code Playgroud)

https://cordova.apache.org/docs/en/latest/reference/cordova-plugin-screen-orientation/

如果这不起作用,您应该尝试这种方式(没有导入和注入)

cordova plugin add cordova-plugin-screen-orientation
Run Code Online (Sandbox Code Playgroud)

https://cordova.apache.org/docs/en/latest/reference/cordova-plugin-screen-orientation/

此外,如果您想为整个应用程序强制横向,则可以在 config.xml 中添加首选项

<preference name="orientation" value="landscape" />
Run Code Online (Sandbox Code Playgroud)

我已经测试过了,它可以在 android 平台 7.1.4 上运行