解决Element-Plus中el-switch的change方法自动触发问题

下面el-switch代码片段是在el-table里使用 

<el-switch 
    v-else-if="col.prop == 'status'" 
    v-model="scope.row.status" 
    :active-value="'0'" 
    :inactive-value="'1'" 
    :before-change="beforeStatus" 
    @change="changStatus(scope.row.status, scope.row.goodsId)">
</el-switch>

el-switch方法代码是Vue3.0框架 typeScript语法格式

<script setup lang="ts">
// 该引入是全局封装getCurrentInstance()方法 代码在下面
import comInstance from "@/hooks/comInstance";

// 获取全局属性
const { global } = comInstance();

// 判断是否在点击弹窗确认按钮时才调用接口
const tags = ref('')

// before方法加一个弹窗提示是否操作
const beforeStatus = async () => {
    const confirm = await global.$confirm('确定操作吗?');
    return new Promise((resolve, reject) => {
        if (confirm) {
            // 点击确认按钮时设置tags值
            tags.value = 'confirmStatus'
            return resolve(confirm)
        } else {
            return reject(confirm)
        }
    })
}

const changStatus = async (type: string, goodsId: string) => {
    // 判断tags值 这样就不会进页面时调用了
    if (tags.value == 'confirmStatus') {
        const res = await upAndDownApi({
            goodsId: goodsId,
            status: type
        }).catch(() => { })
        if (res && res.code == 200) {
            // 刷新表格
            getGoodsList()
        }
    }
}
</script>

@/hooks/comInstance代码 全局封装getCurrentInstance()方法

getCurrentInstance()方法是Vue3 Composition API中的一个API函数。它的作用是返回一个组件实例对象,可以用来操作当前组件的各种属性和方法。这个方法是一个全局API,可以在任何地方使用。

import { getCurrentInstance, ComponentInternalInstance } from "vue";

// 获取挂载属性
export default function useInstance() {
    const { appContext, proxy } = getCurrentInstance() as ComponentInternalInstance
    const global = appContext.config.globalProperties;
    return {
        proxy,
        global
    }
}

相关推荐

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-01-06 01:04:01       98 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-01-06 01:04:01       106 阅读
  3. 在Django里面运行非项目文件

    2024-01-06 01:04:01       87 阅读
  4. Python语言-面向对象

    2024-01-06 01:04:01       96 阅读

热门阅读

  1. kotlin first/last/indexOf/elementAt

    2024-01-06 01:04:01       64 阅读
  2. Vue3中使用动态组件

    2024-01-06 01:04:01       68 阅读
  3. Docker项目:搭建服务器监控面板

    2024-01-06 01:04:01       54 阅读
  4. 【sql】_![CDATA[]]_和转义字符:

    2024-01-06 01:04:01       53 阅读
  5. 前端加密库 jsencrypt的使用

    2024-01-06 01:04:01       61 阅读
  6. 字母异位词分组【哈希】

    2024-01-06 01:04:01       67 阅读
  7. SSH 端口转发:如何将服务绑定到本地 IP 地址

    2024-01-06 01:04:01       60 阅读
  8. leetcode链表相关题目

    2024-01-06 01:04:01       61 阅读
  9. 笙默考试管理系统-MyExamTest----codemirror(62)

    2024-01-06 01:04:01       50 阅读
  10. neo4j配置详解

    2024-01-06 01:04:01       65 阅读
  11. 保障企业数据安全的29个最佳实践

    2024-01-06 01:04:01       58 阅读