在我们的 VueJS 应用程序中,我们只有很少的 API,每次页面重新加载时都会调用这些 API。在那些 API 中。很少有回应永远不会改变,也很少有人很少改变。我计划缓存这些 API 调用响应并将其存储在一个变量中,并在需要时使用它,并减少页面重新加载时的请求数量。
我是 vueJS 的新手,不知道如何实现它。有没有办法在 VueJS 或 Javascript 中实现这一点?非常感激任何的帮助。
示例 HTML 代码,
<div class="col-sm-6">
<span>Is User Available? {{userInfo[is_user_available]}} </span>
<span> User Type : {{userType}} </span>
</div>
Run Code Online (Sandbox Code Playgroud)
API 调用如下所示,
created: function () {
this.checkForUser();
},
methods: {
checkForUser: function() {
this.api.call('user_info', { username : this.username })
.then((response) => {
if (response) {
this.userInfo = response;
this.userType = this.userInfo['user_type'];
}
})
.catch((error) => {
this.userInfo.length = 0;
})
}
}
Run Code Online (Sandbox Code Playgroud)