Search code examples
cordovascreen-orientationionic4

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


Solution

  • 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