vue 调用方法怎么设置返回值_vue 封装通用方法,其他接受返回值返回

项目如果一个方法多次调用,建议常胜剥离出来,写一个方法其他方法可以调用此方法,本文是亲身测试经验主要解决挂载一个通用接口的方法,但是需要同步接收其返回值再往下进行。

一、挂载公共方法

1、可以直接在main.js挂载全局方法

Vue.prototype.msgSuccess = function (msg) {

this.$message({ showClose: true, message: msg, type: "success" });

}

2、也可以在main.js里引入其他文件的方法再挂载成全局方法

新建一个通用的common.js

import {getCommon } from "@/utils/common.js";

Vue.prototype.getCommon = getCommon

common.js里面的方法,必须要加上return返回值,不然取不到值

/****/

import unrecondOrder from '../unrecondOrder'

var commonMethod={

//获取订单类型

getCommon(){

return unrecondOrder.getservicedata("post").then(response =>{

return response.data

})

},

}

export default common

二、其他地方调用

接口调用

this.getCommon().then(response=>{

console.log("调用接口返回值",response)

this.formTreeData=response

})

亲测有效,如有疑问,可以留言

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值