Получение ScreenMetrics в приложении NativeScript Angular

Как получить доступ к ScreenMetrics в приложении NativeScript Angular 2 ?

Можно ли использовать систему DI Angular, вводя ScreenMetrics? Думаю, нет, потому что, хотя инструкция Import в порядке, следующее не работает. В консоли наблюдаются странные ошибки.

import { Component } from '@angular/core';
import { ScreenMetrics } from 'platform';

@Component({
  selector: 'home',
  templateUrl: 'components/home/home.component.html',
})
export class HomeComponent {
  constructor(private screenMetrics: ScreenMetrics) {
    console.log(screenMetrics);
  }
}

К сожалению, документация не очень доработана, и большинство примеров кода написаны на простом коде JavaScript.


person Merott    schedule 15.06.2016    source источник


Ответы (1)


В NativeScript Angular для получения информации об экране вы должны использовать import { screen } from "platform". Затем у вас есть mainScreen, хотя у вас будет доступ к свойствам heightDIPs, heightPixels, scale, widthDIPs, widthPixels. В приведенном ниже примере вы можете посмотреть, как получить к ним доступ в NS Angular.

import { Component } from '@angular/core';
import { screen } from 'platform';

@Component({
  selector: 'home',
  templateUrl: 'components/home/home.component.html',
})
export class HomeComponent {
  constructor() {
    console.dump(screen.mainScreen);
    console.log(screen.mainScreen.widthPixels);
  }
}
person Nikolay Tsonev    schedule 15.06.2016
comment
Спасибо! Я закончил тем, что завернул platform в Injectable сервис Angular. - person Merott; 16.06.2016