我正在尝试 5 分钟的 Anuglar2 教程,当它说您可以使用外部模板时,我尝试了它。
我的组件看起来像这样
import {Component, Template, bootstrap} from 'angular2/angular2';
// Annotation section
@Component({
selector: 'my-app'
})
@Template({
url: "component.html"
})
// Component controller
class MyAppComponent {
constructor() {
this.name = 'Alice';
}
}
bootstrap(MyAppComponent);
我的外部模板出错并修复了它,但 HTML 文件仍然被缓存,所以我无法在浏览器中使用效果。
弄清楚他们是如何缓存它的,我查看了Github上的代码
我找到了这个
#angular/modules/angular2/src/core/compiler/template_loader.js
@Injectable()
export class TemplateLoader {
_xhr: XHR;
_htmlCache: StringMap;
_baseUrls: Map<Type, string>;
_urlCache: Map<Type, string>;
_urlResolver: UrlResolver;
constructor(xhr: XHR, urlResolver: UrlResolver) {
this._xhr = xhr;
this._urlResolver = urlResolver;
this._htmlCache = StringMapWrapper.create();
this._baseUrls = MapWrapper.create();
this._urlCache = MapWrapper.create();
}
// TODO(vicb): union type: return an Element or a Promise<Element>
load(template: Template) {
if (isPresent(template.inline)) {
return DOM.createTemplate(template.inline);
}
if (isPresent(template.url)) {
var url = this.getTemplateUrl(template);
var promise = StringMapWrapper.get(this._htmlCache, url);
if (isBlank(promise)) {
promise = this._xhr.get(url).then(function (html) {
var template = DOM.createTemplate(html);
return template;
});
StringMapWrapper.set(this._htmlCache, url, promise);
}
return promise;
}
所以我检查了 StringMapWrapper angular/modules/angular2/src/facade/collection.es6
并且设置代码只是
static set(map, key, value) {
map[key] = value;
}
我看到 StringMapWrapper 来自全球
export var StringMap = global.Object;
但是查看angular/modules/angular2/src/facade/lang.es6 我无法弄清楚地图的缓存位置。
我对缓存过程知之甚少,希望有人能解释在这种情况下他们是如何做到的。