Vue3+ TS + ElementPlus 文本比对

此代码不只能够 文本对比,如果是json 还能在输入框格式化
用的是 Vue3 TS 组合式写法

效果图

在这里插入图片描述

在这里插入图片描述

使用方法

首先安装 v-code-diff

npm install v-code-diff

TextDiff.vue

<template>
    <h1 style="text-align: center;">比较数据</h1>
    <div class="input-area">
        <div class="input-container">
            <el-input type="textarea" :rows="10" v-model="oldStrInput" placeholder="输入旧数据"></el-input>
        </div>
        <div class="input-container">
            <el-input type="textarea" :rows="10" v-model="newStrInput" placeholder="输入新数据"></el-input>
        </div>
        <div class="button-container">
            <el-button @click="compareData">比较数据</el-button>
            <el-popconfirm width="220" confirm-button-text="确认" cancel-button-text="取消" @confirm="reset"
                :icon="InfoFilled" icon-color="#626AEF" title="确认要重置吗">
                <template #reference>
                    <el-button>重置</el-button>
                </template>
            </el-popconfirm>
        </div>
    </div>
    <CodeDiff class="center" :old-string="oldStrToCompare" :new-string="newStrToCompare" :drawFileList="true"
        :context="1000" outputFormat="side-by-side" />
</template>

<script setup lang="ts">
import { ref, reactive, computed, watch } from 'vue'
// import { CodeDiff } from 'v-code-diff'
import { ElMessage } from 'element-plus'
import { InfoFilled } from '@element-plus/icons-vue'

const jsonFlag = ref(false)

const oldStr = reactive([] as any);
const newStr = reactive([] as any);
const oldStrInput = ref('')
const newStrInput = ref('')
// 定义一个响应式对象用于保存解析后的 JSON 数据
const jsonOldData = reactive({});
const jsonNewData = reactive({});

// 监听 obj 的变化,并尝试将其解析为 JSON 对象
watch(oldStrInput, (newValue) => {
    try {
        const parsedData = JSON.parse(newValue);
        oldStrInput.value = JSON.stringify(parsedData, null, 2)
        Object.assign(jsonOldData, parsedData);
    } catch (error) {
        // 如果解析失败,则清空 jsonData 或者保持原样
        Object.assign(jsonOldData, {});
    }
});

watch(newStrInput, (newValue) => {
    try {
        const parsedData = JSON.parse(newValue);
        newStrInput.value = JSON.stringify(parsedData, null, 2)
        Object.assign(jsonNewData, parsedData);
    } catch (error) {
        // 如果解析失败,则清空 jsonData 或者保持原样
        Object.assign(jsonNewData, {});
    }
});

const oldStrToCompare = computed(() => {
    if (jsonFlag.value) {
        return JSON.stringify(oldStr.value, null, 2)
    } else {
        return oldStr.value
    }
})

const newStrToCompare = computed(() => {
    if (jsonFlag.value) {
        return JSON.stringify(newStr.value, null, 2)
    } else {
        return newStr.value
    }
})

function compareData() {
    if (!oldStrInput.value || !newStrInput.value) {
        ElMessage.error("请输入要比较的数据");
    } else {
        try {
            // oldStr
            oldStr.value = JSON.parse(oldStrInput.value);
            newStr.value = JSON.parse(newStrInput.value);
            jsonFlag.value = true
        } catch (error) {
            oldStr.value = oldStrInput.value;
            newStr.value = newStrInput.value;
            jsonFlag.value = false
        }
    }

}
function reset() {
    oldStrInput.value = ''
    newStrInput.value = ''
    oldStr.value = []
    newStr.value = []
}

</script>

<style scoped>
.input-area {
    display: flex;
    justify-content: center;
}

.input-container {
    flex: 1;
    margin: 0 10px;
}

.button-container {
    margin-top: 20px;
}

.input-box {
    width: 100%;
}
</style>

使用

<script setup lang="ts">
import TextDiff from '../components/TextDiff.vue'
</script>

<template>
  <main>
    <TextDiff />
  </main>
</template>

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

子仪_

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值