Angular/Ionic 2 - what is a provider and what does

2019-07-11 06:08发布

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条回答
Animai°情兽
2楼-- · 2019-07-11 06:54

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.

查看更多
登录 后发表回答