10 Vue3 实战解决方法

本文介绍了一个基于Vue的聊天机器人组件,实现实时消息交互并保持滚动条在底部。通过用户输入触发对话,每次发送后自动将滚动条滚动到底部,展示完整的对话历史。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

滚动条到底

<template>
    <div id="box">
        <div class="b-head">
            <img class="h-img" alt="" src="../assets/img/logo.png" width=80; height=60;/>
            <span class="h_span">知识图谱机器人</span>
        </div>
        <div class="b-body" ref="main">
            <template v-for="(item, index) in contentList" :key="index">
                <div :class="{rotWord: item.type==='bot', mWord: item.type==='user'}">
                    <span></span>
                    <p>{{item.content}}</p>
                </div>
            </template>
        </div>
        <div class="b-footer">
            <input type="text" name="text" id="f-left" @keyup.enter="ask" v-model="userSay"/>
            <div id="btn" @click="ask">发送</div>
        </div>
    </div>

</template>

<script>
    import {defineComponent, computed} from 'vue';

    export default defineComponent({
        name: "ChatBot",
        data() {
            return {
                userSay: "",
                contentList: [
                    {
                        "type": "bot",
                        "content": "机器人说话"
                    },
                    {
                        "type": "user",
                        "content": "用户所说的话"
                    },
                ]
            }
        },
        methods: {
            ask() {
                if (this.userSay === '') {
                    return
                }
                // 添加用户说的话
                let userContent = {
                    "type": "user",
                    "content": this.userSay
                };
                this.contentList.push(userContent);

                // 添加bot说的话
                let botSay = `bot:${this.userSay}`;
                let botContent = {
                    "type": "bot",
                    "content": botSay
                };
                this.contentList.push(botContent);

                // 最后的动作
                this.userSay = "";

                this.$nextTick(() => {
                    // 通过 $refs 获取 <div class="b-body" ref="main">...</div>其DOM
                    this.$refs.main.scrollTop = this.$refs.main.scrollHeight;
                });
            }
        },
    })
</script>

<style lang="scss" scoped>
    a:link {
        color: #fff;
        text-decoration: none;
    }

    a:visited {
        text-decoration: none;
        color: #fff;
    }

    a:hover {
        text-decoration: none;
        color: #fff;
    }

    a:active {
        text-decoration: none;
        color: #fff;
    }

    * {
        margin: 0;
        padding: 0;
    }


    #box {
        width: 800px;
        height: 600px;
        /*background: rgba(255, 255, 255, .5);*/
        margin: 50px auto 0;
        background: url(../assets/img/bg.jpg) no-repeat;
    }

    .b-head {
        width: 100%;
        height: 60px;
        background-color: #4CAF50;
    }

    .h-img {
        margin: 0 20px;
        float: left;
        user-select: none;
    }

    .h_span {
        color: #fff;
        font-size: 18px;
        line-height: 60px;
        float: left;
        user-select: none;
        cursor: default;
    }

    .b-body {
        width: 100%;
        height: 420px;
        overflow: auto;
        margin: 20px 0;
    }

    .rotWord, .mWord {
        width: 100%;
        margin-top: 10px;
        overflow: hidden;
    }

    .rotWord span {
        background: url(../assets/img/rot.png);
        height: 40px;
        width: 40px;
        margin-left: 20px;
        float: left;
    }

    .rotWord p {
        word-break: break-all;
        top: 4px;
        float: left;
        color: #fff;
        font-size: 14px;
        margin-left: 10px;
        padding: 10px;
        line-height: 24px;
        background: rgba(0, 0, 255, .5);
        border-radius: 6px;
        max-width: 220px;
    }

    .mWord span {
        background: url(../assets/img/my.png);
        height: 40px;
        width: 40px;
        float: right;
        margin-right: 20px;
    }

    .mWord p {
        word-break: break-all;
        top: 2px;
        float: right;
        color: #fff;
        font-size: 14px;
        margin-right: 10px;
        padding: 10px;
        line-height: 24px;
        background: #19b955;
        border-radius: 6px;
        max-width: 220px;
    }

    .b-footer {
        width: 760px;
        height: 60px;
        margin: 0 20px;
        font-size: 16px;
        color: #666;
    }

    #f-left {
        padding-left: 20px;
        outline: none;
        overflow: hidden;
        width: 620px;
        height: 60px;
        float: left;
        background: rgba(0, 0, 0, .1);
        font-size: 18px;
        border: none;
        border-radius: 5px;
    }

    #btn {
        width: 100px;
        height: 60px;
        background: #666666;
        float: right;
        cursor: pointer;
        text-align: center;
        line-height: 60px;
        font-size: 18px;
        color: #fff;
        user-select: none;
        border-radius: 10px;
    }

</style>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值