Ionic 4 - Screen Orientation(landscape) does not w

2019-08-07 08:59发布

I am new on Ionic 4 and I am trying to set the screen orientation to landscape and referring to docs this is what I am doing:

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

and during compilation, I get this error:

[ng] ERROR in src/app/app.component.ts(24,33): error TS2345: Argument of type 'string' is not assignable to parameter of type 'OrientationLockType'.

and on browser console:

Uncaught Error: Can't resolve all parameters for AppComponent: ([object Object], [object Object], [object Object], ?). at syntaxError (compiler.js:2426) [] ...

3条回答
贼婆χ
2楼-- · 2019-08-07 09:17

There is a problem in the injection for the fourth parameter, did you correctly install the screen orientation plugin?

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/

If this doesn't work you should try this way ( withoud the import and injection )

cordova plugin add cordova-plugin-screen-orientation

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

Also, if you want to force landscape orientation for the whole application it is possibile to add a preference in config.xml

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

I've tested it and it works in android platform 7.1.4

查看更多
Anthone
3楼-- · 2019-08-07 09:20

Make sure to add ScreenOrientation to the providers array in the module using the plugin.

For example in app.module.ts

查看更多
forever°为你锁心
4楼-- · 2019-08-07 09:27

you use ionic V4, please see the doc, import correct things.

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

not

import {ScreenOrientation} from '@ionic-native/screen-orientation';
查看更多
登录 后发表回答