vue3添加文字追加文字段的时候看到滚动条 但是并没有自己去滚到最下面

在 vue中 做个类似聊天框的功能

看图 vue3添加文字追加文字段的时候看到滚动条 但是并没有自己去滚到最下面,很多聊天工具呢 点击send 默认都会到最新消息那 就是滚动条最下方

Vue 更i性能dom是异步的 数据更新是同步的

解决办法:1.回调函数模式 2.async await 写法

第一种解决办法:

javascript 复制代码
<template>
    <div style="padding: 180px">
        <div
            ref="box"
            style="
                height: 500px;
                width: 500px;
                border: 1px solid red;
                overflow: auto;
            "
        >
            <div
                v-for="item in chatList"
                style="
                    background-color: aquamarine;
                    border: 1px solid;
                    display: flex;
                "
            >
                <p>{{ item.name }}</p>
                <p>{{ item.message }}</p>
            </div>
        </div>
        <div>
            <textarea style="width: 500px" v-model="ipt" type="txt"></textarea>
        </div>
        <el-button @click="send">send</el-button>
    </div>
</template>
<script setup lang="ts">
import { nextTick } from "vue"
import { ref, reactive } from "vue"
let chatList = reactive([{ name: "张三", message: "xxxxxx" }])
const ipt = ref("")
const box = ref<HTMLDivElement>()
// Vue 更i性能dom是异步的 数据更新是同步的
const send = () => {
    chatList.push({ name: "小六", message: ipt.value })
    // ipt.value=''
    nextTick(() => {
        box.value!.scrollTop = 99999
    })
}
</script>
<style module lang="scss"></style>

第二种解决办法:

javascript 复制代码
<template>
    <div style="padding: 180px">
        <div
            ref="box"
            style="
                height: 500px;
                width: 500px;
                border: 1px solid red;
                overflow: auto;
            "
        >
            <div
                v-for="item in chatList"
                style="
                    background-color: aquamarine;
                    border: 1px solid;
                    display: flex;
                "
            >
                <p>{{ item.name }}</p>
                <p>{{ item.message }}</p>
            </div>
        </div>
        <div>
            <textarea style="width: 500px" v-model="ipt" type="txt"></textarea>
        </div>
        <el-button @click="send">send</el-button>
    </div>
</template>
<script setup lang="ts">
import { nextTick } from "vue"
import { ref, reactive } from "vue"
let chatList = reactive([{ name: "张三", message: "xxxxxx" }])
const ipt = ref("")
const box = ref<HTMLDivElement>()
// Vue 更i性能dom是异步的 数据更新是同步的
// 解决办法:1.回调函数模式 2.async await 写法
const send = async () => {
    chatList.push({ name: "小六", message: ipt.value })
    ipt.value = ""
    await nextTick()
    box.value!.scrollTop = 99999
}
</script>
<style module lang="scss"></style>
相关推荐
漫天转悠5 小时前
Vue3白屏排查实录-Transition与多根模板
vue.js·1024程序员节
技术小甜甜5 小时前
【Cursor】Skill 被标黄色警告,怎么改成合格的 SKILL.md
开发语言·javascript·ai编程·cursor·1024程序员节·ai agent·skills
qetfw6 小时前
Bash 函数与流程控制:read、if、for、case 验证
linux·前端·javascript·bash
jason.zeng@15022076 小时前
Nginx 配置详解(静态 HTML、下载目录、API 反向代理三套场景)
java·vue.js·1024程序员节
鬼手点金6 小时前
从 API 消息到模型 Token:Chat Template
java·前端·javascript·数据库·人工智能·深度学习·1024程序员节
enjoy编程8 小时前
playwright学习-I
typescript·vue·nodejs·1024程序员节·playwright
Mr.Pascal8 小时前
Javascript之什么是作用域?
javascript·1024程序员节
microrain9 小时前
设备报文怎么变,SagooIoT 只约定三个函数名
javascript·物联网·golang·sagooiot
Bug收容所9 小时前
Vue+JS Day3
前端·javascript·vue.js·前端框架·1024程序员节
iDao技术魔方9 小时前
Bun v1.4.4 实战:用 bun fmt 替掉 prettier 的 5 个步骤
typescript·1024程序员节