Angular/Ionic 2 - what is a provider and what does `static get parameters()` do?

一世执手 提交于 2020-01-04 06:51:27

问题


I am learning Angular 2 (with Ionic 2) - there are two concepts which I cannot relate to from Angular 1.

I have a class like follows:

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);
        });
    }
}

What is a provider and what does it do/what is its purpose?

What does the following code do:

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

回答1:


Since you use ES6, you don't have parameter types. The following isn't possible (only TypeScript):

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

Using the static getter you will configure the types of parameters to provide to the constructor. Angular2 dependency injection will leverage this to know which providers to use to inject the parameters of the constructor. It read the content of the parameters property for the class...

Using the getter you define the value of this "static" property.



来源:https://stackoverflow.com/questions/35922092/angular-ionic-2-what-is-a-provider-and-what-does-static-get-parameters-do

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!