Vue3学习-day4

父子通信

父传子

App.vue

复制代码
<script setup>
// 局部组件(直接导入就能用,不用注册)
 import sonFirst from './components/son-First.vue'
// 父传子
// 1. 给子组件,添加属性的方式传值
// 2. 在子组件,通过 props 接收
import {ref} from 'vue'
const sum = ref(200) // 响应式值的传递
const getmore = () => {
  sum.value++
}
</script>

<template>
  <div>
    <h3>
      父组件 {{ sum }}
      <button @click="getmore">+1</button>
    </h3>

    <!-- 给子组件以添加属性的方式传值 -->
    <son-First fruit="苹果" :sum="sum"></son-First>
  </div>
</template>

son-First.vue

复制代码
<script setup>
// 注意:由于写了 setup,所以无法直接配置 props 选项
// 因此,此处需要借助于"编译器宏"函数接收子组件传递的值
const props = defineProps({
  fruit: String,
  sum: Number
})
console.log(props.fruit)
console.log(props.sum)
// 子组件
// import son from './components/son.vue'
// 父传子
// 1. 给子组件,添加属性的方式传值
// 2. 在子组件,通过 props 接收

</script>

<template>
<!-- 对于 props 传递过来的数据,模板中可以直接使用 -->
  <div class="son">子组件 - {{ fruit }}+ {{ sum }}</div>
</template>

<style scoped>S
  .son{
    border: 2px solid red;
    padding: 25px;
  }
</style>

defineProps原理

子传父

App.vue

复制代码
<script setup>
// 局部组件(直接导入就能用,不用注册)
 import sonFirst from './components/son-First.vue'
// 父传子
// 1. 给子组件,添加属性的方式传值
// 2. 在子组件,通过 props 接收

// 子传父
// 1. 在子组件中,emit 触发事件 (编译器宏获取)
// 2. 在父组件,通过 @ 监听
import {ref} from 'vue'
const sum = ref(200) // 响应式值的传递
const getmore = () => {
  sum.value++
}
const changefn = (newsum) => {
  sum.value = newsum
}
</script>

<template>
  <div>
    <h3>
      父组件 {{ sum }}
      <button @click="getmore">+1</button>
    </h3>

    <!-- 给子组件以添加属性的方式传值 -->
    <son-First
    fruit="苹果"
    :sum="sum"
    @changenum="changefn"></son-First>
  </div>
</template>

son-First.vue

复制代码
<script setup>
// 注意:由于写了 setup,所以无法直接配置 props 选项
// 因此,此处需要借助于"编译器宏"函数接收子组件传递的值
const props = defineProps({
  fruit: String,
  sum: Number
})
console.log(props.fruit)
console.log(props.sum)
// 子组件
// import son from './components/son.vue'
// 父传子
// 1. 给子组件,添加属性的方式传值
// 2. 在子组件,通过 props 接收

// 子传父
// 1. 在子组件中,emit 触发事件 (编译器宏获取)
// 2. 在父组件,通过 @ 监听
const emit = defineEmits(['changenum'])

const subnum = () => {
  // 需要 emit 触发事件
  emit('changenum', 5)
}
</script>

<template>
<!-- 对于 props 传递过来的数据,模板中可以直接使用 -->
  <div class="son">
    子组件 - {{ fruit }}+ {{ sum }}
    <button @click="subnum">-1</button>
    </div>
</template>

<style scoped>S
  .son{
    border: 2px solid red;
    padding: 25px;
  }
</style>

总结

模板引用

模板引用的概念

如何使用(以dom为例)

App.vue

复制代码
<script setup>
import TextCom from '@/components/textCom.vue'
import {onMounted, ref} from 'vue'
// 模板引用(可以获取dom,也可以获取组件)
//  1. 调用ref函数,生成一个对象
//  2. 通过ref标识,进行绑定
//  3. 通过ref对象.value即可访问到绑定的元素(必需渲染完成后才能拿到)
const inp = ref(null)
// console.log(inp.value)
onMounted(() => {
  // console.log(inp.value)
  // inp.value.focus() // 聚焦
})
const clickFn = () => {
  inp.value.focus()
}

// ----------------------------------------------
const textRef =  ref(null)
const getCom = () => {
  console.log(textRef.value)
}
</script>
<template>
  <div>
    <input ref="inp" type="text">
    <button @click="clickFn">通过点击按钮聚焦</button>
    <TextCom ref="textRef"></TextCom>
    <button @click="getCom">获取组件</button>
  </div>
</template>
<style scoped>

</style>

defineExpose()

textCom.vue

复制代码
<script setup>

const count = 99999
const hello = () => {
  console.log('你好啊,世界')
}

// defineExpose 编译宏,指定哪些属性和方法允许访问
defineExpose({
  count,
  hello
})
</script>

<template>
  <div>
    我是测试组件 - {{ count }}
  </div>
</template>

App.vue

复制代码
<script setup>
import TextCom from '@/components/textCom.vue'
import {onMounted, ref} from 'vue'
// 模板引用(可以获取dom,也可以获取组件)
//  1. 调用ref函数,生成一个对象
//  2. 通过ref标识,进行绑定
//  3. 通过ref对象.value即可访问到绑定的元素(必需渲染完成后才能拿到)
const inp = ref(null)
// console.log(inp.value)
onMounted(() => {
  // console.log(inp.value)
  // inp.value.focus() // 聚焦
})
const clickFn = () => {
  inp.value.focus()
}

// ----------------------------------------------
const textRef =  ref(null)
const getCom = () => {
  console.log(textRef.value.count)
  textRef.value.hello()
}
</script>
<template>
  <div>
    <input ref="inp" type="text">
    <button @click="clickFn">通过点击按钮聚焦</button>
    <TextCom ref="textRef"></TextCom>
    <button @click="getCom">获取组件</button>
  </div>
</template>
<style scoped>

</style>

总结

相关推荐
久爱@勿忘几秒前
vue下载项目内静态文件
前端·javascript·vue.js
前端炒粉1 分钟前
21.搜索二维矩阵 II
前端·javascript·算法·矩阵
合作小小程序员小小店31 分钟前
web网页开发,在线%台球俱乐部管理%系统,基于Idea,html,css,jQuery,jsp,java,ssm,mysql。
java·前端·jdk·intellij-idea·jquery·web
搞机械的假程序猿33 分钟前
普中51单片机学习笔记-流水灯
笔记·学习·51单片机
不爱吃糖的程序媛39 分钟前
Electron 应用中的系统检测方案对比
前端·javascript·electron
泷羽Sec-静安43 分钟前
Less-9 GET-Blind-Time based-Single Quotes
服务器·前端·数据库·sql·web安全·less
pe7er1 小时前
用高阶函数实现递归:从匿名函数到通用递归生成器
前端·javascript
IT古董1 小时前
全面理解 Corepack:Node.js 的包管理新时代
前端·node.js·corepack
学习3人组1 小时前
清晰地说明 NVM、NPM 和 NRM 在 Node.js 开发过程中的作用
前端·npm·node.js
立志成为大牛的小牛1 小时前
数据结构——三十九、顺序查找(王道408)
数据结构·学习·程序人生·考研·算法