Vue前端框架11 组件事件与v-mode配合使用、组件数据传递(父传子)、插槽Slot、具名插槽、插槽中的数据传递(双向)

文章目录

一、组件事件与v-model配合使用

组件A的数据变化

组件B可以实时显示

javascript 复制代码
<template>
  <h3>Main</h3>
  <p>搜索内容为:{{search}}</p>
  <component-b @searchEvent="getSearch"></component-b>
</template>

<script>
import ComponentB from "@/components/componentB";
export default {
  name: "componentA",
  components: {ComponentB},
  data(){
    return{
      search:""
    }
  },
  methods:{
    getSearch(data){
      this.search=data
    }
  }
}
</script>

<style scoped>

</style>
javascript 复制代码
<template>
  搜索: <input type="text" v-model="search">
</template>

<script>
export default {
  name: "componentB",
  data(){
    return{
      search:""
    }
  },
  //侦听器
  watch:{
    search(newvValue,oldValue){
      console.log(oldValue)
      this.$emit("searchEvent",newvValue)
    }
  }
}
</script>

<style scoped>

</style>

二、组件数据传递(子传父)

利用父组件的函数回调

javascript 复制代码
<template>
  <h3>Main</h3>
  <p>搜索内容为:{{search}}</p>
  <component-b @searchEvent="getSearch" :onEvent="dataFn"></component-b>
  <p>this:{{message}}</p>
</template>

<script>
import ComponentB from "@/components/componentB";
export default {
  name: "componentA",
  components: {ComponentB},
  data(){
    return{
      search:"",
      message:""
    }
  },
  methods:{
    getSearch(data){
      this.search=data
    },
    dataFn(data){
      this.message=data
    }
  }
}
</script>

<style scoped>

</style>
javascript 复制代码
<template>
  搜索: <input type="text" v-model="search">
  <p>{{onEvent(age)}}</p>
</template>

<script>
export default {
  name: "componentB",
  data(){
    return{
      search:"",
      age:22
    }
  },
  //侦听器
  watch:{
    search(newvValue,oldValue){
      console.log(oldValue)
      this.$emit("searchEvent",newvValue)
    }
  },props:{
    onEvent:Function
  }
}
</script>

<style scoped>

</style>

三、插槽Slots

解决的是组件传输一个完整的html结构 也就是一个模板内容

javascript 复制代码
<template>
  <parent-demo></parent-demo>
  <component-a></component-a>
  <slot-demo>
    <div>
      <h3>插槽标题</h3>
<!--      插槽内容不是写死的 而是动态的-->
      <p>{{ message }}</p>
    </div>
  </slot-demo>
</template>

<script>


import ParentDemo from "@/components/parentDemo";
import ComponentA from "@/components/componentA";
import SlotDemo from "@/components/slotDemo";
export default {
  name: 'App',
  components: {SlotDemo, ComponentA, ParentDemo},
  data(){
    return{
      message:"hello"
    }
  }
}
</script>

<style>
#app {
  font-family: Avenir, Helvetica, Arial, sans-serif;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
  text-align: center;
  color: #2c3e50;
  margin-top: 60px;
}
</style>
javascript 复制代码
<template>
  <h3>插槽基础知识</h3>
<!--  slot 是一个插槽的出口 标示了 父元素提供的插槽内容将要在哪里被渲染 -->
<!--  如果没有传插槽 在标签中间的内容 就是默认值-->
  <slot>插槽默认值</slot>
</template>

<script>
export default {
  name: "slotDemo"
}
</script>

<style scoped>

</style>

四、具名插槽

javascript 复制代码
<template>
  <parent-demo></parent-demo>
  <component-a></component-a>
  <slot-demo>
<!--   通过template中的 v-slot属性 和 slot标签的 name对应 -->
<!--    v-slot的简写 可以 简写为# -->
    <template v-slot:header>
      <h3>插槽标题</h3>
    </template>
    <template #main>
      <p>{{ message }}</p>
    </template>

  </slot-demo>
</template>

<script>


import ParentDemo from "@/components/parentDemo";
import ComponentA from "@/components/componentA";
import SlotDemo from "@/components/slotDemo";
export default {
  name: 'App',
  components: {SlotDemo, ComponentA, ParentDemo},
  data(){
    return{
      message:"hello"
    }
  }
}
</script>

<style>
#app {
  font-family: Avenir, Helvetica, Arial, sans-serif;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
  text-align: center;
  color: #2c3e50;
  margin-top: 60px;
}
</style>
javascript 复制代码
<template>
  <h3>插槽基础知识</h3>
<!--  slot 是一个插槽的出口 标示了 父元素提供的插槽内容将要在哪里被渲染 -->
<!--  如果没有传插槽 在标签中间的内容 就是默认值-->
  <slot name="header">插槽默认值</slot>
  <hr>
  <slot name="main"></slot>
</template>

<script>
export default {
  name: "slotDemo"
}
</script>

<style scoped>

</style>

五、插槽中的数据传递

在某些情况下,插槽的内容可能同时需要用到父子两个组件的数据,所以需要方法将子组件在渲染时候将一部分数据提供给插槽

javascript 复制代码
<template>
  <parent-demo></parent-demo>
  <component-a></component-a>
  <slot-demo>
<!--   通过template中的 v-slot属性 和 slot标签的 name对应 -->
<!--    v-slot的简写 可以 简写为# -->
    <template v-slot:header>
      <h3>插槽标题</h3>
    </template>
    <template #main>
      <p>{{ message }}</p>
    </template>
  </slot-demo>

  <SlotAttr>
<!--    建议指明对应的插槽名称-->
    <template v-slot:main="slotProps">
      <p>{{currentTest}}</p>
      <p>{{slotProps.msg}}</p>
    </template>
  </SlotAttr>
</template>

<script>


import ParentDemo from "@/components/parentDemo";
import ComponentA from "@/components/componentA";
import SlotDemo from "@/components/slotDemo";
import SlotAttr from "@/components/slotAttr";
export default {
  name: 'App',
  components: {SlotAttr, SlotDemo, ComponentA, ParentDemo},
  data(){
    return{
      message:"hello",
      currentTest:"测试内容"
    }
  }
}
</script>
javascript 复制代码
<template>
  <h3>插槽plus</h3>
<!--  子元素的数据想要显示 则需要将数据传给父元素
      父元素接收后和父元素原本的内容 一起传给子元素
-->
  <slot :msg="childMessage" name="main">

  </slot>
</template>

<script>
export default {
  name: "slotAttr",
  data(){
    return{
      childMessage:"子组件数据"
    }
  }
}
</script>

<style scoped>

</style>
相关推荐
miss34 分钟前
AI 改完不敢上线?OpenSpec 规约驱动开发实战
前端·代码规范·前端工程化
招风的黑耳37 分钟前
SpringBoot+Vue+MySQL 民宿在线预定平台 完整可运行版(源码+文档)
vue.js·spring boot·mysql
AiMan1 小时前
腾讯ima还是不可以导出左侧所有对话,只能单条导出,方法不对吗
前端
去伪存真1 小时前
并发量一高就装死?Agent 全链路“防猝死”手册
前端·agent
Moment1 小时前
AI 编程时代,聊聊我的 Mac mini 双屏桌面
前端·后端
AiMan1 小时前
先确认能不能一次把Microsofat Copilot所有内容打包一个文件导出
前端
总要学点什么2 小时前
第97篇:三维数据加载-城市建筑白模自定义着色器——让一条光带沿高度扫上去,快慢却由帧率说了算
前端·gis·webgl·cesium·着色器·空间分析·白模
上海懒图科技2 小时前
工厂厂区电子地图定位导航:厂区人车精准定位,安全合规双保障
前端·arcgis·vue
西安小哥2 小时前
端侧跑大模型,真正的门槛不在参数量:一次 596 秒到 10 秒的压缩路径复盘
前端·人工智能
陈随易3 小时前
Cloudflare 出钱,Vite 团队挑人,44 个维护者分 30 万美元
前端·后端·程序员