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>

好的,下面是关于vue3+ts+elementplus聊天界面的开发方案: 1. 安装Vue CLI并创建Vue3项目 首先,您需要安装Vue CLI来创建Vue3项目。您可以使用以下命令安装Vue CLI: ``` npm install -g @vue/cli ``` 安装完成后,您可以使用以下命令创建Vue3项目: ``` vue create my-project ``` 2. 安装Element Plus Element Plus是一套基于Vue3的组件库,适用于PC端和移动端。您可以使用以下命令安装Element Plus: ``` npm install element-plus --save ``` 3. 集成TypeScript Vue3已经默认支持TypeScript,您可以使用以下命令安装TypeScript: ``` npm install typescript --save-dev ``` 4. 开始开发聊天界面 在开发聊天界面之前,您需要先确定聊天数据的结构。您可以使用以下数据结构: ``` interface Message { id: number; content: string; sender: string; receiver: string; time: string; } ``` 接下来,您可以使用Element Plus中的组件来构建聊天界面。以下是一个简单的聊天界面示例: ``` <template> <div class="chat-window"> <div class="chat-history"> <div v-for="message in messages" :key="message.id" class="message-row"> <div class="message-sender">{{ message.sender }}</div> <div class="message-content">{{ message.content }}</div> <div class="message-time">{{ message.time }}</div> </div> </div> <div class="chat-input"> <el-input v-model="inputMessage" placeholder="请输入消息"></el-input> <el-button @click="sendMessage">发送</el-button> </div> </div> </template> <script lang="ts"> import { defineComponent, ref } from 'vue'; interface Message { id: number; content: string; sender: string; receiver: string; time: string; } export default defineComponent({ name: 'ChatWindow', setup() { const messages = ref<Message[]>([ { id: 1, content: '你好', sender: '小明', receiver: '小红', time: '2022-01-01 12:00:00', }, { id: 2, content: '你好,很高兴认识你', sender: '小红', receiver: '小明', time: '2022-01-01 12:01:00', }, ]); const inputMessage = ref(''); const sendMessage = () => { const newMessage: Message = { id: messages.value.length + 1, content: inputMessage.value, sender: '小明', receiver: '小红', time: new Date().toLocaleString(), }; messages.value.push(newMessage); inputMessage.value = ''; }; return { messages, inputMessage, sendMessage, }; }, }); </script> <style> .chat-window { display: flex; flex-direction: column; width: 500px; height: 500px; border: 1px solid #ccc; overflow: hidden; } .chat-history { flex: 1; padding: 10px; overflow-y: auto; } .message-row { display: flex; flex-direction: column; margin-bottom: 10px; } .message-sender { font-weight: bold; margin-bottom: 5px; } .message-content { margin-bottom: 5px; } .message-time { font-size: 12px; color: #999; } .chat-input { display: flex; padding: 10px; background-color: #f0f0f0; } .el-input { flex: 1; margin-right: 10px; } .el-button { width: 80px; } </style> ``` 以上示例中,我们使用了Element Plus中的Input和Button组件来实现发送消息的功能,使用了Vue3中的Composition API来管理组件状态。 希望这个开发方案对您有所帮助!
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

子仪_

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

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

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

打赏作者

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

抵扣说明:

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

余额充值