In a Vue page, I have a call to get data using Ajax when the mounted() event is fired. The code recreates the existing Pager using a new Pager object where it has to pass in all the parameters in the constructor to reconstruct it.
If I don't do this, vm.Pager is just an Object and does not have some needed methods, and fails the prop type check that it gets passed to.
axios.post("/Home/GetList", vm.Pager)
.then(function (result)
{
var p = result.data.Pager;
vm.Pager = new Pager(p.PageSize, p.CurrentRecord, p.TotalCount);
// etc. (Pager has additional fields...)
vm.ItemList = result.data.ListItems;
})
.catch(function (error)
{
alert(error);
});
In the knockoutjs, there was a mapping function and you could what tell it what types to map without having to recreate the object. This was convenient, particularly for more complicated or nested Ajax data.
Is there a better way to do this in Vue (or javascript) where it maps the type from the Ajax without having to recreate it?
在Vue应用中,作者遇到了在组件挂载时使用Ajax获取数据的问题。当前的实现方式是在接收到数据后,需要手动创建一个新的Pager对象来重新构造实例,以确保对象具有必要的方法。这在KnockoutJS中可以通过映射功能避免。文章探讨了在Vue或JavaScript中是否存在更优雅的方式来直接映射Ajax返回的数据类型,而无需手动重建对象。
964

被折叠的 条评论
为什么被折叠?



