Vue:nextTick作用 - 附带案例讲解

news/2024/7/10 0:02:01 标签: Vue, nextTick

文章目录

VuenextTick__DOM_1">1. Vue.$nextTick - 获取最新渲染的DOM数据

参考:https://segmentfault.com/a/1190000012861862

通俗解释:Vue 在修改数据后,视图不会立刻更新,而是等同一事件循环中的所有数据变化完成之后,再统一进行视图更新 - 即你修改下图变量message是同步任务,但你把数据渲染到页面是异步任务(Vue的视图渲染内部用虚拟DOM组成),只要虚拟DOM更新了数据,那么你就可以获取到最新的渲染数据

请添加图片描述

请添加图片描述



<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <script src="G:\VsCode\开源\vue.global.js"></script>
</head>
<body>
    <div id="main-box">
        <div style="margin-top:50px;border: 5px solid red;padding:30px">
            <p ref="sourceContent">{{msg}}</p>
            <p>不使用nextTick获取的sourceContent节点的内容:{{msg2}}</p>
            <p>使用nextTick获取的sourceContent节点的内容:{{msg3}}</p>
            <button @click="test">测试$nextTick</button>
        </div>
       
        <div style="margin-top:50px;border: 5px solid red;padding:30px">
            <div>
                <button @click="switchShow">隐藏/显示Input框, 显示时此Input获得焦点(不使用nextTick</button>
                <br>
                <span>不使用nextTick</span><input ref="input" v-if="show"></input>
            </div>
   
            <div style="margin-top:50px">
                <button @click="switchShow2">隐藏/显示Input框, 显示时此Input获得焦点(使用nextTick</button>
                <br>
                <span>使用nextTick</span><input ref="input2" v-if="show2"></input>
            </div>
        </div>        
    </div>
</body>
<script>
    var mainPage = {
        data: function () {
            return {
                msg: "原始信息",
                msg2: "",
                msg3: "",
                num: 1,
                show: false,
                show2: false
            }
        },
        methods: {
            test: function () {
                this.msg = "新消息" + this.num++
                //还没等sourceContent节点渲染完就去拿到节点HTML内容
                this.msg2 = this.$refs['sourceContent'].innerHTML
                //等到sourceContent节点渲染完才去拿节点HTML内容
                this.$nextTick(() => {
                    this.msg3 = this.$refs['sourceContent'].innerHTML
                })
            },
            //此input不会获得焦点,因为input还没渲染出来已经去执行focus方法
            switchShow: function () {
                this.show = !this.show;
                if (this.show) {
                    this.$refs['input'].focus();
                }
            },
            //此input会获得焦点,因为input渲染出来才去执行focus方法
            switchShow2: function () {
                this.show2 = !this.show2;
                if (this.show2) {
                    this.$nextTick(() => {
                        this.$refs['input2'].focus();
                    })
                }
            },
        }
    }
    Vue.createApp(mainPage)
        .mount("#main-box")
</script>
</html>

请添加图片描述


http://www.niftyadmin.cn/n/1657714.html

相关文章

废旧电脑改装个人服务器

需要的材料&#xff1a; 旧电脑一个&#xff08;我用的是一个十几年的笔记本配置是2128&#xff09;&#xff0c;U盘一个8G就好&#xff08;做启动U盘&#xff09;。 公网ip&#xff08;现在大部分家庭应该都有公网ip&#xff0c;如果没有可以去运营商申请&#xff09; 系统…

MBD开发 STM32 UASRT

目录 轮询 ptintf 中断方式 DMA方式 轮询 串口要加入这两个文件 bug在于接到10个后会一直发送 ptintf function buffPtr convert(buff)if coder.target(Sfun)%固定句式%Executing in MATLAB, Buff is nullbuffPtr uint32(0); elsecoder.cinclude(getBuffPtr.h);%加入头…

Jsoup解析(京东搜索)

Bean封装 package top.linruchang;import lombok.AllArgsConstructor; import lombok.Builder; import lombok.Data; import lombok.NoArgsConstructor; import lombok.experimental.Accessors;import java.util.List;/*** 作用&#xff1a;** author LinRuChang* version 1.0…

IT项目管理之----(转)

序言... 7 1 前言... 8 2 如何做业务调研&#xff1f;... 8 2.1 调研工作如何组织&#xff1f;... 8 2.2调研准备阶段容易犯哪些错误&#xff1f;(上) 10 2.2.1 第一个容易犯的错误&#xff1a;不清楚调研的的目的... 10 2.2.2 第二个容易犯的错误&#xff1a;计划不够细致... …

ElasticSearch学习1

文章目录1. 其他工具2. 概述概念倒排索引3. 使用3.1 ElasticSearch-HelloWorld3.1.1 配置文件-关闭安全认证、开启跨域3.1.2 启动3.1.3 访问9200端口3.1.4 Rest访问约束规定概念测试获取 - ElasticSearch所有的索引库信息获取 - ElasticSearch健康信息创建库&#xff08;索引&a…

ElasticSearch学习2 - 查询、修改

文章目录0. 修改索引大文本字段支持排序1. 查询1.1 字段检索-match1.2 查询结果仅显示需要的字段-_source1.3 结果集进行排序-sort1.4 分页查询 - from从0开始、size每页行数1.5 多条件查询and-must1.6 多条件查询or-should1.7 否定-must_not1.8 结果集过滤-filter1.9 数组模糊…

《managed DirectX9 kick start:Graohics and Game Programming》

请问谁有电子的。呵呵 转载于:https://www.cnblogs.com/nanshouyong326/archive/2007/04/16/714975.html

Ubuntu Feisty怎么还不见Release啊!

从昨晚等到现在了&#xff0c;啊&#xff01; 我要抓狂了&#xff01; 我要抓狂了&#xff01; 我要抓狂了&#xff01; 我要抓狂了&#xff01; 我要抓狂了&#xff01; 我要抓狂了&#xff01; 我要抓狂了&#xff01; 我要抓狂了&#xff01; 我要抓狂了&#xff01; 我要抓…