【VUE基础】VUE3第三节—核心语法之ref标签、props

ref标签

作用:用于注册模板引用。

  • 用在普通DOM标签上,获取的是DOM节点。

  • 用在组件标签上,获取的是组件实例对象。

用在普通DOM标签上:

typescript 复制代码
<template>
  <div class="person">
    <h1 ref="title1">小C学安全</h1>
    <h2 ref="title2">前端</h2>
    <h3 ref="title3">Vue</h3>
    <input type="text" ref="inpt"> <br><br>
    <button @click="showLog">点我打印内容</button>
  </div>
</template>

<script lang="ts" setup name="Person">
  import {ref} from 'vue'
	
  let title1 = ref()
  let title2 = ref()
  let title3 = ref()

  function showLog(){
    // 通过id获取元素  在dom标签上需要设置id=title1
    // const t1 = document.getElementById('title1')
    // 打印内容
    // console.log((t1 as HTMLElement).innerText)
    // console.log((<HTMLElement>t1).innerText)
    // console.log(t1?.innerText)
    
		/************************************/
		
    // 通过ref获取元素
    console.log(title1.value)
    console.log(title2.value)
    console.log(title3.value)
  }
</script>

用在组件标签上:

在父组件中设置ref标签,获取到的是子组件对象,还没有获取到真正数据

例如

typescript 复制代码
//APP.vue
<template>
  <Person ref="xiaoc123"/>
  <button @click="test">测试</button>
</template>

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

  let xiaoc123  = ref()

  function test(){
    console.log(xiaoc123.value);
    
  }
</script>
//Person.vue
<template>
  <div class="person">
    <h1 ref="title1">小C学安全</h1>
    <h2 ref="title2">前端</h2>
    <h3 ref="title3">Vue</h3>
    <input type="text" ref="inpt"> <br><br>
    <!-- <button @click="showLog">点我打印内容</button> -->
  </div>
</template>

<script lang="ts" setup name="Person">
import { ref } from 'vue'

let name = ref('xiaoc')
let age = ref(20)
</script>

以上代码并没有获取到Person.vue中具体的name,age值

需要使用defineExpose将组件中的数据交给外部,就可以获取到name,age值

例如:

typescript 复制代码
//APP.vue
<template>
  <Person ref="xiaoc123"/>
  <button @click="test">测试</button>
</template>

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

  let xiaoc123  = ref()

  function test(){
    console.log(xiaoc123.value);
    
  }
</script>
//Person.vue
<template>
  <div class="person">
    <h1 ref="title1">小C学安全</h1>
    <h2 ref="title2">前端</h2>
    <h3 ref="title3">Vue</h3>
    <input type="text" ref="inpt"> <br><br>
    <!-- <button @click="showLog">点我打印内容</button> -->
  </div>
</template>

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

let name = ref('xiaoc')
let age = ref(20)

defineExpose({name,age})

Props

在使用

typescript 复制代码
<script setup>
const props = defineProps(['foo'])

console.log(props.foo)
</script>

除了使用字符串数组来声明 props 外,还可以使用对象的形式:

typescript 复制代码
// 使用 <script setup>
defineProps({
  title: String,
  likes: Number
})

如果你正在搭配 TypeScript 使用

typescript 复制代码
<script setup lang="ts">
defineProps<{
  title?: string
  likes?: number
}>()

</script>

传递不同的值类型

在上述的两个例子中,我们只传入了字符串值,但实际上任何类型的值都可以作为 props 的值被传递。

Number​

typescript 复制代码
<!-- 虽然 `42` 是个常量,我们还是需要使用 v-bind -->
<!-- 因为这是一个 JavaScript 表达式而不是一个字符串 -->
<BlogPost :likes="42" />

<!-- 根据一个变量的值动态传入 -->
<BlogPost :likes="post.likes" />

Boolean​

typescript 复制代码
<!-- 仅写上 prop 但不传值,会隐式转换为 `true` -->
<BlogPost is-published />

<!-- 虽然 `false` 是静态的值,我们还是需要使用 v-bind -->
<!-- 因为这是一个 JavaScript 表达式而不是一个字符串 -->
<BlogPost :is-published="false" />

<!-- 根据一个变量的值动态传入 -->
<BlogPost :is-published="post.isPublished" />

Array​

typescript 复制代码
<!-- 虽然这个数组是个常量,我们还是需要使用 v-bind -->
<!-- 因为这是一个 JavaScript 表达式而不是一个字符串 -->
<BlogPost :comment-ids="[234, 266, 273]" />

<!-- 根据一个变量的值动态传入 -->
<BlogPost :comment-ids="post.commentIds" />

Object​

typescript 复制代码
<!-- 虽然这个对象字面量是个常量,我们还是需要使用 v-bind -->
<!-- 因为这是一个 JavaScript 表达式而不是一个字符串 -->
<BlogPost
  :author="{
    name: 'Veronica',
    company: 'Veridian Dynamics'
  }"
 />

<!-- 根据一个变量的值动态传入 -->
<BlogPost :author="post.author" />

代码演示:

指定固定数据类型

typescript 复制代码
// 定义一个接口,限制每个Person对象的格式
export interface PersonInter {
id:string,
name:string,
 age:number
}

// 定义一个自定义类型Persons
export type Persons = Array<PersonInter>

App.vue中代码:

typescript 复制代码
<template>
	<Person :list="persons"/>
</template>

<script lang="ts" setup name="App">
import Person from './components/Person.vue'
import {reactive} from 'vue'
 import {type Persons} from './types'

 let persons = reactive<Persons>([
  {id:'e98219e12',name:'张三',age:18},
   {id:'e98219e13',name:'李四',age:19},
    {id:'e98219e14',name:'王五',age:20}
  ])
</script>

Person.vue中代码:

typescript 复制代码
<template>
<div class="person">
<ul>
  <li v-for="item in list" :key="item.id">
     {{item.name}}--{{item.age}}
   </li>
 </ul>
</div>
</template>

<script lang="ts" setup name="Person">
import {defineProps} from 'vue'
import {type PersonInter} from '@/types'

// 第一种写法:仅接收
// const props = defineProps(['list'])

// 第二种写法:接收+限制类型
// defineProps<{list:Persons}>()

// 第三种写法:接收+限制类型+指定默认值+限制必要性
let props = withDefaults(defineProps<{list?:Persons}>(),{
  list:()=>[{id:'asdasg01',name:'小猪佩奇',age:18}]
})
console.log(props)
</script>
相关推荐
Web4Browser1 小时前
指纹浏览器 API 自动化怎么接:启动 Profile、获取 CDP 端点并连接自动化框架
前端·网络·typescript·自动化
Patrick_Wilson1 小时前
从 React 到 Flutter:写给前端的一张跨端知识地图
前端·flutter·react.js
颜酱1 小时前
06 | 把 meta_config 同步进 MySQL(生成阶段)
前端·人工智能·后端
OpenTiny社区2 小时前
Loop Engineering:让 AI Agent 自己跑起来的工程方法
前端·github
IT_陈寒2 小时前
SpringBoot自动配置坑了我一周,原来问题这么蠢!
前端·人工智能·后端
kyriewen2 小时前
我review了一份Vibe Coding写的前端代码——能跑,但5个地方迟早要命
前端·javascript·ai编程
REDcker3 小时前
Cesium三维WebGIS入门详解
前端·gis·web·cesium·webgis
0暗影流光03 小时前
十倍效能提升——Web 基础研发体系的建立
前端
前端一课3 小时前
用Agent Skill重构代码:300行缩至30行的实战教程
前端
腻害兔3 小时前
【若依项目-产品经理视角】RuoYi-Vue-Pro 源码拆解:IM 即时通讯模块,一个被低估的「全功能聊天系统」
java·前端·vue.js·产品经理·ai编程