【问题标题】:Angular/Ionic 2 - what is a provider and what does `static get parameters()` do?Angular/Ionic 2 - 什么是提供者,`static get parameters()` 做什么?
【发布时间】:2016-03-10 16:31:25
【问题描述】:

我正在学习 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]];
}

【问题讨论】:

    标签: angular ionic-framework ionic2


    【解决方案1】:

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

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

    使用静态 getter,您将配置要提供给构造函数的参数类型。 Angular2 依赖注入将利用这一点来知道使用哪些提供者来注入构造函数的参数。它读取了类的parameters 属性的内容...

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

    【讨论】:

    • 这是一个很好的答案 - 非常感谢;它们是数组中的数组...内部数组可以包含多个项目还是最多长度为 1?
    • 不客气!我不知道为什么数组中有一个数组。但可以肯定的是,如果您希望将多个元素注入到构造函数中,则可以在其中包含多个元素。类似的东西:return [[Platform,Http,ElementRef]];
    猜你喜欢
    • 2016-06-25
    • 1970-01-01
    • 2015-04-11
    • 2021-09-04
    • 1970-01-01
    • 1970-01-01
    • 2020-08-18
    • 2015-02-09
    • 2012-05-04
    相关资源
    最近更新 更多