Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting Not Supported Error while using Ionic3

Tags:

angular

ionic3

I strangle to be able to catch this error. While on desktop, this code raise this NotSupportedError:

enter image description here

I normally debug on chrome.

Here is the code:

import {Component} from "@angular/core";
import {ScreenOrientation} from "@ionic-native/screen-orientation";

@IonicPage()
@Component({
  selector: 'page-loading',
  templateUrl: 'page-loading.html',
})
export class PageLoading {

  constructor(private screenOrientation:ScreenOrientation) {}

  ionViewDidLoad() {
    console.log('ionViewDidLoad PageLoading');

    try{
        this.screenOrientation.lock(this.screenOrientation.ORIENTATIONS.PORTRAIT).then(()=>{
          console.log('lock');
        });
    }catch(e){
      console.warn('No cordova.js')
    }

  }

}
like image 434
David Faure Avatar asked May 15 '17 11:05

David Faure


1 Answers

You can create a class to mock ionic native classes as described in the docs here.

class ScreenOrientationMock extends ScreenOrientation {
  lock(type) {
    return new Promise((resolve, reject) => {
      resolve("locked");
    })
  }
}

In your providers list in ngModule mention it should use your mocked class instead of the actual ionic native one.

providers: [..
    { provide: ScreenOrientation, useClass: ScreenOrientationMock }
 ]

This will return whatever you have set in resolve for screenorientation during ionic serve.

You can remove it once it is run in a device.

EDIT:

There is another possibility to suppress your error, so you will not have anything to do at the end:

if(this.platform.is('cordova')){
  this.platform.ready().then(()=>{
   this.screenOrientation.lock(this.screenOrientation.ORIENTATIONS.PORTRAIT);
  })
}
like image 121
Suraj Rao Avatar answered Sep 28 '22 11:09

Suraj Rao