向app.module的构造函数注入服务有什么作用?

时间:2017-10-20 07:26:35

标签: angular

我使用了SmartAdmin主题,他们已将app.service注入app.module

一直试图找出这次注射的目的,但没有遇到任何人这样做。最接近的是在AppState中提供app.module作为提供者,我知道在整个应用中只会使用一个实例。

有人能否阐明这句话在app.module中的作用?我应该如何使用它?

export class AppModule {
  constructor(public appRef: ApplicationRef, public appState: AppState) {}
}

以下是AppState的内容:

import { Injectable } from '@angular/core';

export type InternalStateType = {
  [key: string]: any
};

@Injectable()
export class AppState {
  _state: InternalStateType = { };

  constructor() {

  }

  // already return a clone of the current state
  get state() {
    return this._state = this._clone(this._state);
  }
  // never allow mutation
  set state(value) {
    throw new Error('do not mutate the `.state` directly');
  }


  get(prop?: any) {
    // use our state getter for the clone
    const state = this.state;
    return state.hasOwnProperty(prop) ? state[prop] : state;
  }

  set(prop: string, value: any) {
    // internally mutate our state
    return this._state[prop] = value;
  }


  private _clone(object: InternalStateType) {
    // simple object clone
    return JSON.parse(JSON.stringify( object ));
  }
}

0 个答案:

没有答案
相关问题