【问题标题】:ionic2 - ScreenOrientationionic2 - 屏幕方向
【发布时间】:2016-12-31 05:50:54
【问题描述】:

我想在我的ionic2 应用程序的特定页面上将屏幕方向锁定为横向。所以我从离子站点导入the pluginCordova 屏幕方向plugin

import { ScreenOrientation } from 'ionic-native';

然后我尝试在我的constructor 中调用它:

  constructor(public navCtrl: NavController,
              public orientation:ScreenOrientation
              ) {
                ScreenOrientation.lockOrientation('Landscape');
              }

但是我收到了这个错误:

异常:./Test 类 Test_Host 中的错误 - 内联模板:0:0 原因:没有 ScreenOrientation 的提供者!

这似乎是什么问题?

【问题讨论】:

  • 构造函数用于提供者的依赖注入..插件通过全局对象访问..
  • 我应该在哪里调用函数ScreenOrientation.lockOrientation('Landscape');?我尝试了很多地方,它给了我编译警告。
  • 警告是什么?通常你将插件的构造函数放在`this.platform.ready

标签: cordova angular ionic2


【解决方案1】:

错误表明没有可用于 ScreenOrientation“服务”的提供程序。为了使用这些提供者,它们必须首先在 app.module.ts 中声明。

将 ScreenOrientation 添加到 app.module.ts 中的提供程序列表:

  1. 先添加导入:
    import { ScreenOrientation } from '@ionic-native/screen-orientation';

  2. 然后将 ScreenOrientation 添加到 @NgModule 中的提供程序列表中:
    providers: [ StatusBar, SplashScreen, ScreenOrientation, {provide: ErrorHandler, useClass: IonicErrorHandler} ]

【讨论】:

    【解决方案2】:

    从构造函数中删除参数“publicorientation:ScreenOrientation”。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-06-02
      • 1970-01-01
      • 2022-11-13
      • 2011-09-09
      • 2011-07-17
      • 2011-09-29
      • 2011-06-09
      • 1970-01-01
      相关资源
      最近更新 更多