在項目開發中遇到一種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',