我想从服务器获取 mustache 模板并将其存储在本地以供以后使用。
我使用 $.get 在我的代码中具有类似 promise 的行为,但是在第一次调用后,我如何存储结果以供以后使用并使用此存储的代码而不是另一个服务器调用?
我现在拥有的:
...
getTemplate() {
return $.get(this.templateUrl);
}
...
this.getTemplate().then((template) => {}
最佳答案
将其存储在局部变量中?:
getTemplate() {
if(this._cached)
return this._cached;
return this._cached = $.get(this.templateUrl);
}
剩下的应该通过浏览器的 caching 来完成
关于javascript - 如何使用延迟对象来存储结果并在下次从本地内存中提供服务?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54291076/