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

问题描述 投票:0回答:2

我是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);
    ...
  }
}

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

[ng] src / app / app.component.ts(24,33)中的错误:错误TS2345:类型'string'的参数不能分配给'OrientationLockType'类型的参数。

并在浏览器控制台上:

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

cordova screen-orientation ionic4
2个回答
1
投票

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

ionic cordova plugin add cordova-plugin-screen-orientation
npm install @ionic-native/screen-orientation

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

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

cordova plugin add cordova-plugin-screen-orientation

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

此外,如果要强制整个应用程序的横向方向,可以在config.xml中添加首选项

<preference name="orientation" value="landscape" />

我测试了它,它可以在android平台7.1.4中运行


1
投票

你使用离子V4,请看文档,导入正确的东西。

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

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

0
投票

确保使用插件将ScreenOrientation添加到模块中的providers数组。

例如在app.module.ts

© www.soinside.com 2019 - 2024. All rights reserved.