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>

总结

相关推荐
The_era_achievs_hero3 分钟前
UniappDay04
vue.js·微信小程序·uni-app
猫和老许8 分钟前
Vue 3 拖拽排序功能优化实现:从原理到实战应用
前端·javascript·vue.js
YGY_Webgis糕手之路11 分钟前
Leaflet 综合案例 - 路径规划
前端·gis
sq80011 分钟前
ag-grid-vue3 降级,支持低版本浏览器
前端·javascript·vue.js
前端灵派派12 分钟前
cesium 实现轨迹回放
前端·cesium
水纹12 分钟前
继续研究pdfjs保存和还原批注
前端
兔年鸿运Q小Q13 分钟前
html转word下载
javascript·vue.js·word
程序员海军13 分钟前
使用 Kiro AI IDE 3小时实现全栈应用Admin系统
前端·后端·aigc
你这个年龄怎么睡得着的17 分钟前
玩转vite性能优化
前端·vue.js·vite
YGY_Webgis糕手之路17 分钟前
OpenLayers 综合案例-热力图
前端·gis