0

如何访问ScreenMetricsNativeScript Angular 2 应用程序?

是否应该使用 Angular 的 DI 系统,注入 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 代码。

4

1 回答 1

2

在 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);
  }
}
于 2016-06-15T10:00:16.623 回答