Angular / Ionic 2 - 什么是提供者以及`static get parameters()`做什么?

时间:2016-03-10 16:31:26

标签: angular ionic-framework ionic2

我正在学习Angular 2(使用Ionic 2) - 有两个概念我无法与Angular 1相关。

我有一个类如下:

import {App, Platform} from 'ionic-angular';
import {RegisterPage} from './pages/register/register';


@App({
    templateUrl: 'build/index.html',
    config: {} // http://ionicframework.com/docs/v2/api/config/Config/
})
export class MyApp {
    static get parameters() {
        return [[Platform]];
    }

    constructor(platform) {
        this.rootPage = RegisterPage;

        platform.ready().then(() => {
            // The platform is now ready. Note: if this callback fails to fire, follow
            // the Troubleshooting guide for a number of possible solutions:
            //
            // Okay, so the platform is ready and our plugins are available.
            // Here you can do any higher level native things you might need.
            //
            // First, let's hide the keyboard accessory bar (only works natively) since
            // that's a better default:
            //
            // Keyboard.setAccessoryBarVisible(false);
            //
            // For example, we might change the StatusBar color. This one below is
            // good for dark backgrounds and light text:
            StatusBar.setStyle(StatusBar.LIGHT_CONTENT);
        });
    }
}

什么是提供者?它的作用/目的是什么?

以下代码的作用如下:

static get parameters() {
    return [[Platform]];
}

1 个答案:

答案 0 :(得分:3)

由于您使用ES6,因此您没有参数类型。以下是不可能的(只有TypeScript):

export class MyApp {
  constructor(platform:Platform) {
  }
}

使用静态getter,您将配置要提供给构造函数的参数类型。 Angular2依赖注入将利用它来了解用于注入构造函数参数的提供程序。它读取了类的parameters属性的内容......

使用getter定义此“static”属性的值。