【发布时间】:2016-10-30 21:46:48
【问题描述】:
我正在清理我的 angular2 项目,出于多种原因,我决定从种子开始。 This one.
这个种子使用HMR,但我不完全明白它的目的是什么。
一开始,我认为 HMR 是关于动态加载和 在 Web 应用运行时替换组件。
但自从我把目光投向app.service.ts 之后,我就迷路了。这是该服务的代码:
import { Injectable } from '@angular/core';
import { HmrState } from 'angular2-hmr';
@Injectable()
export class AppState {
// @HmrState() is used by HMR to track the state of any object during a hot module replacement
@HmrState() _state = { };
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[prop] || state;
}
set(prop: string, value: any) {
// internally mutate our state
return this._state[prop] = value;
}
_clone(object) {
// simple object clone
return JSON.parse(JSON.stringify( object ));
}
}
我在想服务只是提供了一个空间来存储一些数据。毕竟,这只是一个例子。
但这条线确实让我感到困惑:@HmrState() _state = { };。该服务是否使用 HMR 来管理我们可以使用 this.appState.set('value', value); 管理的数据(来自 HomeComponent),就像一个小的 Redux 存储(没有操作、调度程序、blabla)?
装饰器@HmrState()这里的作用是什么?
谢谢。
【问题讨论】:
标签: typescript angular webpack seed