Sabri Aziri
Sabri Aziri

Reputation: 4184

Ionic 4 - Screen Orientation(landscape) does not work

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) [] ...

Upvotes: 2

Views: 7237

Answers (5)

Yair Abad
Yair Abad

Reputation: 354

This worked for me (Ionic 5). Inside your page.module, import dependency:

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

and provide to the module:

 providers: [
    ScreenOrientation, 
    ...]

Upvotes: 0

Hakan Goker
Hakan Goker

Reputation: 49

providers array in the module using the plugin.

    providers: [
    ScreenOrientation
 ]

Upvotes: 1

Dylan w
Dylan w

Reputation: 2896

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

For example in app.module.ts

Upvotes: 1

user11127940
user11127940

Reputation: 61

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';

Upvotes: 6

Sergio Rinaudo
Sergio Rinaudo

Reputation: 2363

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

Upvotes: 5

Related Questions