首页 > 其他分享 >前端学习-vue视频学习007-标签的ref属性

前端学习-vue视频学习007-标签的ref属性

时间:2024-03-06 20:34:28浏览次数:18  
标签:vue title 标签 007 组件 let ref

尚硅谷视频教程

给标签增加ref属性,可以通过属性值取到标签内容

<template>
    <div class="person">
        <h1>this</h1>
        <h2 ref="title">that</h2>
        <button @click="showLog">changeTemp</button>
    </div>
</template>

<script lang="ts" setup name="Person">
    import {ref,watch,watchEffect} from 'vue'
    
    let title = ref()

    function showLog(){
        console.log(title.value); 
    }

   
</script>

<style scoped>
    .person {
        background-color: skyblue;
        box-shadow: 0 0 10px;
        border-radius: 10px;
        padding: 20px;
    }
    
    button {
        margin : 0 5px;
    }

    li {
        font-size : 20px;
    }
</style>

data-v-xxxx 局部标识


如果在style中加了scoped,代表为局部样式,使用ref标签时会出现data-v-xxxx 标识

<style scoped>
</style>

在组件标签中使用ref标签

如果根组件想要获取components文件内的数据,需要两步:

  • 在根组件内的组件标签内增加ref标签
  • 对应组件文档中使用defineExpose

App.vue

<template>
    <Person ref="temp"/>
    <button @click="showLog">test</button>   
</template>

<script lang="ts" setup name="App">
    import Person from './components/Person.vue';
    import {ref} from 'vue'

    let temp = ref()

    // 如果components的文件内不使用defineExpose,是无法获取到组件标签内的数据的
    function showLog(){
        console.log(temp.value);
    }
</script>

Person.vue

<template>
    <div class="person">
        <h1>this</h1>
        <h2 ref="title">that</h2>
        <button @click="showLog">changeTemp</button>
    </div>
</template>

<script lang="ts" setup name="Person">
    import {ref,defineExpose} from 'vue'
    
    let title = ref()

    let a = ref(0)
    let b = ref(1)
    let c = ref(2)

    function showLog(){
        console.log(title.value); 
    }

    // 将数据a,b暴露给根组件,隐藏c
    defineExpose({a,b})
   
</script>

<style scoped>
    .person {
        background-color: skyblue;
        box-shadow: 0 0 10px;
        border-radius: 10px;
        padding: 20px;
    }
    
    button {
        margin : 0 5px;
    }

    li {
        font-size : 20px;
    }
</style>

标签:vue,title,标签,007,组件,let,ref
From: https://www.cnblogs.com/ayubene/p/18057400

相关文章

  • Vue学习笔记36--VueComponent构造函数
    VueComponent构造函数<!DOCTYPEhtml><htmllang="en"><head><metacharset="UTF-8"><metaname="viewport"content="width=device-width,initial-scale=1.0"><title>VueComponent&......
  • Vue3.0 所采用的 Composition Api 与 Vue2.x 使用的 Options Api 有什么不同?
    这里给大家分享我在网上总结出来的一些知识,希望对大家有所帮助开始之前CompositionAPI 可以说是Vue3的最大特点,那么为什么要推出CompositionApi,解决了什么问题?通常使用Vue2开发的项目,普遍会存在以下问题:代码的可读性随着组件变大而变差每一种代码复用的方式,都存在缺点T......
  • Vue学习笔记3--组件嵌套
    组件嵌套示例一:<!DOCTYPEhtml><htmllang="en"><head><metacharset="UTF-8"><metaname="viewport"content="width=device-width,initial-scale=1.0"><title>非单文件组件--全局注册</tit......
  • Vue3计算属性与监听
    接上篇文章vue学习笔记 https://www.cnblogs.com/jickma/p/16523795.html在之前中介绍了vue3的特性与响应式数据定义,新的双向绑定,模版等与vue2的差别。在vue2中很重要的两个很重要的东西就是计算属性与监听在vue3中,同样存在只是写法略有不同。5,计算属性......
  • vue中sass的使用
    前言:这里是用vue2开发的,vue3的sass安装方法有部分不一样。一、安装相关包[email protected]@7.3.1--save-devnpmlsnode-sasssass-loader//查看安装情况//如果报错可能是安装版本有问题,可以卸载后安装node版本对应的s......
  • Cesium在vue3中的安装、使用
    1.cesium插件引入,vite-plugin-cesium是一个专门为Vite构建工具定制的插件,用于在Vite项目中轻松使用Cesium库。它简化了在Vite项目中集成Cesium的过程,并提供了一些额外的功能和配置选项。而直接安装Cesium库,你需要手动配置Vite项目以确保正确引入和使用Cesium。这......
  • PbRL Preference Transformer
    论文题目:PreferenceTransformer:ModelingHumanPreferencesusingTransformersforRL,ICLR2023,5668,poster。pdf:https://arxiv.org/pdf/2303.00957.pdfhtml:https://ar5iv.labs.arxiv.org/html/2303.00957openreview:https://openreview.net/forum?id=Peot1SFDX0项......
  • Caused by: org.apache.ibatis.reflection.ReflectionException: Error parsing prope
    问题描述1、编译过程是不会被发现的;2、运行时报错:Causedby:org.apache.ibatis.reflection.ReflectionException:Errorparsingpropertyname'lambda$pages$4d14352e$1'.Didn'tstartwith'is','get'or'set'。问题分析MyBatis-Plus的条件构造器常用......
  • springboot3+vue3(四.2)ThreadLocal优化
    解决痛点:我们在拦截器内已经获取并解析了一遍token数据,如图:然后在获取当前登录用户详细信息时又做了一遍同样的操作,如图:后续如果说需要用到当前登录用户的信息时都要带上token参数,这样是很冗余的。这时就会用到ThreadLocal来进行优化处理。 ThreadLocal工具类/***......
  • VUE通过for循环一行显示多列el-row
    效果图: 代码: <template><el-row:gutter="0"v-for="(item,index)inarrLen":key="item.name"style="height:200px">......