我正在清理我的angular2项目,由于很多原因,我决定从种子开始。 This one。NG2:angular2-webpack-starter - HMR的目的是什么?
该种子使用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()
在这里的目的是什么?
谢谢。