js--format和replace的用法和区别

在项目开发中遇到一种openApi的接口调用,也就是接口url中带有动态参数,没有了解format的用法前,都是用replace处理的,下面看实例

后台提供的接口路径:

DeleteMenuCollection: apiPath + '/menuCollection/delete/{menuId}/{menuPerson}',

这里用到了menuId和menuPerson两个动态参数。

 

使用replace方法处理如下:

1、写一个公用的方法

replace(Url, val1, val2) {
    for(let i = 0; i < val1.length; i++) {
        Url = Url.replace(val1[i], val2[i])
    }
    return Url
},

2、调用方法

this.$api.replace(this.$api.DeleteMenuCollection, ["{menuId}","{menuPerson}"], [this.componentData.menuId,this.componentData.menuPerson])

this.$api.replace这样调用方法是把replace方法写在了vue中的api.js文件中,大家可以忽略,注意参数一个或者多个都要用数组的形式

 

使用format方法处理如下:

this.$api.DeleteMenuCollection.format({menuId:this.componentData.menuId,menuPerson:this.componentData.menuPerson});

 

两种方法得到的结果一样

DeleteMenuCollection: apiPath + '/menuCollection/delete/123456/admin',

 

转载于:https://www.cnblogs.com/shenpeng/p/11023233.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值