Angular2他们怎么保存到缓存? [英] Angular2 how do they save to cache?

查看:629
本文介绍了Angular2他们怎么保存到缓存?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是想了5分钟Anuglar2教程,当它说,你可以使用我试了一下外部的模板。

I was trying out the 5 min Anuglar2 Tutorial and when it said that you are able to use external templates I tried it.

我的部分看起来像这样

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文件仍缓存,所以我不能在浏览器中的效果。

I had a mistake in my external template and fixed it, but the HTML file was still cached so I couldn't the effects in the browser.

搞清楚他们如何缓存它,我看了看code在 Github上

Figuring out how they cache it I looked at the code on 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 角/模块/ angular2 / src目录/门面/ collection.es6

和用于设置code是刚

and for set the code is just

static set(map, key, value) {
    map[key] = value;
  }

我看到StringMapWrapper来自全球

I saw that the StringMapWrapper comes from global

export var StringMap = global.Object;

但在寻找的角/模块/ angular2 / src目录/门面/ lang.es6 我无法揣摩出的地图被缓存。

But looking in angular/modules/angular2/src/facade/lang.es6 I cant figure out where the Map is cached.

我不知道很多有关缓存过程,并希望有人能解释他们是如何做到这一点在这种情况下。

I do not know much about the caching process and hope someone could explain how they do it in this case.

推荐答案

StringMapWrapper.create()创建一个对象字面 {} 。他们使用像 StringMapWrapper 对凡在其他语言中创建不同的这些原语飞镖支持。总之所有他们正在做的是这样

StringMapWrapper.create() creates an object literal {}. They use something like StringMapWrapper for Dart support where these primitives are created differently in the other language. In short all they're doing is this

var cache = {};
xhr(templateUrl).then(template => {
  cache[templateUrl] = template;
  return template;
})

这篇关于Angular2他们怎么保存到缓存?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆