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>
相关推荐
风清扬_jd2 分钟前
Chromium 中JavaScript Fetch API接口c++代码实现(二)
javascript·c++·chrome
It'sMyGo27 分钟前
Javascript数组研究09_Array.prototype[Symbol.unscopables]
开发语言·javascript·原型模式
李是啥也不会43 分钟前
数组的概念
javascript
道爷我悟了1 小时前
Vue入门-指令学习-v-html
vue.js·学习·html
无咎.lsy1 小时前
vue之vuex的使用及举例
前端·javascript·vue.js
fishmemory7sec1 小时前
Electron 主进程与渲染进程、预加载preload.js
前端·javascript·electron
fishmemory7sec1 小时前
Electron 使⽤ electron-builder 打包应用
前端·javascript·electron
工业互联网专业2 小时前
毕业设计选题:基于ssm+vue+uniapp的校园水电费管理小程序
vue.js·小程序·uni-app·毕业设计·ssm·源码·课程设计
计算机学姐2 小时前
基于SpringBoot+Vue的在线投票系统
java·vue.js·spring boot·后端·学习·intellij-idea·mybatis
JUNAI_Strive_ving2 小时前
番茄小说逆向爬取
javascript·python