【问题标题】:Ionic 4 - Screen Orientation(portrait) does not work, NullInjectorErrorIonic 4 - 屏幕方向(纵向)不起作用,NullInjectorError
【发布时间】:2020-04-01 20:39:19
【问题描述】:

我是 Ionic 4 的新手,我正在尝试将屏幕方向设置为纵向,但 Ionic 4 的 ScreenOrientation 插件出现问题。 我已经尝试了一段时间的其他方法,但这些方法也不起作用。而且我不能让这个插件解决这个导入问题。我在构造函数问题中也有参数确认。 我在手机上安装了这个apk,但它不起作用。

这里是 tabs.page.ts:

import { Component } from '@angular/core';
import { MenuController, Platform } from '@ionic/angular';
import { ScreenOrientation } from '@ionic-native/screen-orientation/ngx';

@Component({
  selector: 'app-tabs',
  templateUrl: './tabs.page.html',
  styleUrls: ['./tabs.page.scss'],
})
export class TabsPage {

  constructor( private platform: Platform,
               private screenOrientation: ScreenOrientation ) {
    this.platform.ready().then(() => {
    this.screenOrientation.unlock();
    this.screenOrientation.lock(this.screenOrientation.ORIENTATIONS.LANDSCAPE);
    });

   }

}

这里是 app.module.ts:

import { NgModule } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import { RouteReuseStrategy } from '@angular/router';

import { IonicModule, IonicRouteStrategy } from '@ionic/angular';
import { SplashScreen } from '@ionic-native/splash-screen/ngx';
import { StatusBar } from '@ionic-native/status-bar/ngx';

import { AppComponent } from './app.component';
import { AppRoutingModule } from './app-routing.module';
import { AlphabeticalOriginOrderPipe } from './pipes/alphabetical-origin-order.pipe';
import { NumericalCostOrderPipe } from './pipes/numerical-cost-order.pipe';
import { DateHourOrderPipe } from './pipes/date-hour-order.pipe';
import { ImageModalPageModule } from './image-modal/image-modal.module';
import { MenuComponent } from './components/menu/menu.component';
import { ScreenOrientation } from '@ionic-native/screen-orientation/ngx';

@NgModule({
  declarations: [AppComponent, AlphabeticalOriginOrderPipe, NumericalCostOrderPipe, DateHourOrderPipe, MenuComponent],
  entryComponents: [],
  imports: [BrowserModule, IonicModule.forRoot(), AppRoutingModule, ImageModalPageModule],
  providers: [
    StatusBar,
    ScreenOrientation,
    SplashScreen,
    { provide: RouteReuseStrategy, useClass: IonicRouteStrategy }
  ],
  bootstrap: [AppComponent]
})
export class AppModule {}

这是错误:

ERROR Error: Uncaught (in promise): NotSupportedError: screen.orientation.lock() is not available on this device.
    at resolvePromise (zone-evergreen.js:797)
    at zone-evergreen.js:707
    at ZoneDelegate.invoke (zone-evergreen.js:359)
    at Object.onInvoke (core.js:39699)
    at ZoneDelegate.invoke (zone-evergreen.js:358)
    at Zone.run (zone-evergreen.js:124)
    at zone-evergreen.js:855
    at ZoneDelegate.invokeTask (zone-evergreen.js:391)
    at Object.onInvokeTask (core.js:39680)
    at ZoneDelegate.invokeTask (zone-evergreen.js:390)

【问题讨论】:

  • 你是 Cordova 还是 Capacitor?

标签: angular ionic-framework ionic4 screen-orientation ionic-native


【解决方案1】:

如果我没记错的话,如果您使用的是 Ionic Cordova,则必须在文件 config.xml 中添加这些代码行。

<preference name="Orientation" value="portrait" />
<preference name="Fullscreen" value="true" />

我认为应该可以解决它。

对于那个错误,你能告诉我你在什么平台上编译吗?

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-01-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-12-30
    • 1970-01-01
    • 1970-01-01
    • 2017-04-26
    相关资源
    最近更新 更多