ajax库 vuejs,VueJs Ajax Data Mapping

在Vue应用中,作者遇到了在组件挂载时使用Ajax获取数据的问题。当前的实现方式是在接收到数据后,需要手动创建一个新的Pager对象来重新构造实例,以确保对象具有必要的方法。这在KnockoutJS中可以通过映射功能避免。文章探讨了在Vue或JavaScript中是否存在更优雅的方式来直接映射Ajax返回的数据类型,而无需手动重建对象。

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?

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值