Angular 2中的$ templateCache吗? [英] $templateCache in Angular 2?

查看:60
本文介绍了Angular 2中的$ templateCache吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Angular 1中,我们使用$ templateCache作为避免XHR在引导应用程序时从服务器中删除每个模板的方法.在Angular 2中,似乎没有现成的机制.

In Angular 1, we use the $templateCache as a way to avoid XHR'ing down every template from the server as we bootstrap our app. In Angular 2, there doesn't appear to be such a mechanism ready-built.

我能做的是在编译器中替换XHR:

What I can do is replace XHR in the compiler:

function myXhr() {};
myXhr.prototype.get = function(url) {
    return new Promise(function(resolve, reject) {
        $.ajax({
            url:url,
            method:'GET',
            success:resolve,
            error:reject
        });
    });
};

ng.platform.browser.bootstrap(MyApp, [
    ng.core.provide(ng.compiler.XHR, {useClass:myXhr})
]);

然后调整我的XHR getter以首先检查本地缓存.这是首选方法吗?

And then adjust my XHR getter to check a local cache first. Is this the preferred approach?

推荐答案

我已经习惯于在所有组件的打字稿中使用模板字符串.我发现在一个TS文件中同时包含html和view逻辑非常方便. 您还可以查看诸如 JSPM

I have become accustomed to using template strings in typescript for all my components. I find having the html and view logic all in one TS file very convenient. You could also look into a module loader/bundler like JSPM or WebPack. These great build tools pull in all your templates into your builds using module syntax like below:

let html = require('./about.html!text');
    @Component({
        selector:"my-about",
        template:html
    })

这篇关于Angular 2中的$ templateCache吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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