js/vue怎么把函数(方法)当做参数传递(已解决)

72 篇文章 1 订阅
67 篇文章 1 订阅

很简单,不带方法括号当做参数传递就可以了

例子

       //测试方法
       demo(){
          alert('我是测试')
       },
       haha(event){
        this.clickEnter(event,this.demo) //this.demo是方法,但是这里当做参数传递
     }  

this.clickEnter()是公共方法,

//接收
        Vue.prototype.clickEnter = function(e,method){//e对应传递的第一个参数 ,method是你传递的方法
            let key = e.keyCode;
            if(key == 13){
                method()
            }
            
        }

最后执行成功,总结 传递方法名不带括号就可以了

需求 项目中所有按钮增加确认提示框

,封装confirm 组件,但是有些点完 确定按钮后 还需再调用列表接口 实现刷新当前表格功能
需要将方法名 作为参数,传给提示框,提示框里点击了确认按钮后进行调用
在这里插入图片描述

首先在 utils-------> infoMsg.js 文件里 定义一个 提示框函数,

将需要展示的数据 作为参数传进来

在这里插入图片描述

全部代码

apiName:'接口名称', 
params:'调取接口传的参数', 
refreshList:'点击确定按钮需要调取的列表方法刷新页面', 
isElse:'接口返回错误提示时是否需要调取列表方法刷新页面',
type:'notify通知框的类型', 
that
// 出库按钮  确认提示框
function popConfirm(apiName, params, refreshList, isElse, type, that) {
    console.log(that, 'reMethods')
    that.$confirm(
        window.vm.$i18n.t("tips.excutOp"),
        window.vm.$i18n.t("tips.tip"),
        {
            confirmButtonText: window.vm.$i18n.t("backTips.confirm"),
            cancelButtonText: window.vm.$i18n.t("backTips.cancel"),
            type: "warning",
        }
    ).then(() => {
        apiName(params).then((res) => {
            // 有返回状态值
            if (res.code == 0 && res.msg) {
                that.$notify({
                    title: res.msg,
                    type: 'success'
                });
                if (refreshList) {
                    refreshList()
                }
            } else if (res.code == 0 && res.msg == '') {
                that.$notify({
                    title: window.vm.$i18n.t("tips.issue"),
                    type: 'success'
                });
                if (refreshList) {
                    refreshList()
                } else {
                    if (isElse == true) {
                        refreshList()
                    }
                    that.$notify({
                        title: res.msg,
                        type: type
                    });
                }
            }

        }).catch((error) => {
            that.$notify({
                title: error.response.data.message,
                type: type
            });
            refreshList()
        });
    })
}

export default { popConfirm, }

页面使用

<el-button icon="el-icon-tickets" size="mini" type="primary"  @click="handleBatchOut">
            批量出库
</el-button>
methods:{
  // 批量出库
    handleBatchOut() {
      let delData = [];
      delData = this.crud.selections.map((item) => {
        return item.id;
      });
      let data = {
        pids: delData,
        singleOut: false,
      };
      this.$infoMsg.popConfirm(outStoragePos, data, this.bothRefresh, false, 'error', this)
    },
}
  // 批量出库
    handleBatchOut() {
      let delData = [];
      delData = this.crud.selections.map((item) => {
        return item.id;
      });
      let data = {
        pids: delData,
        singleOut: false,
      };
      this.$infoMsg.popConfirm(outStoragePos, data, this.bothRefresh, false, 'error', this)
    },
    //弹框刷新使用
    bothRefresh() {
      this.crud.toQuery();
      this.fathermethod();
    },
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值