私は NativeScript + Angular 2 を使い始めており、デバイスの向きに応じて異なるルートをアクティブにするコンポーネントを実装したいと考えています。多くの検索の後、私はこれを機能させることができましたが、初期のデバイスの向きを取得する方法をまだ理解していません。
これが私のAppComponent
コードです。見てくださいngAfterViewInit
:
import {Component, OnInit, OnDestroy, AfterViewInit} from "@angular/core";
import {Router} from "@angular/router";
import _application = require('application');
@Component({
selector: "my-app",
templateUrl: "app.component.html",
})
export class AppComponent implements OnInit, AfterViewInit, OnDestroy {
constructor(private router: Router) {}
ngOnInit() {
_application.on(_application.orientationChangedEvent, this.setOrientation.bind(this));
}
ngAfterViewInit() {
// How do I get the initial orientation here instead of hardcoding to 'portrait'?
this.setOrientation({newValue: 'portrait'})
}
ngOnDestroy() {
_application.off(_application.orientationChangedEvent, this.setOrientation);
}
setOrientation(args) {
if (args.newValue === 'landscape') {
this.router.navigate(['/landscape']);
} else {
this.router.navigate(['/portrait']);
}
}
}