首页 > 解决方案 > Ionic 4 - 屏幕方向(横向)不起作用

问题描述

我是 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:“字符串”类型的参数不可分配给“方向锁定类型”类型的参数。

在浏览器控制台上:

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

标签: cordovascreen-orientationionic4

解决方案


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

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

不是

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

推荐阅读