vue的插槽slot的用法

本文介绍了如何在Vue中通过模板和插槽实现父组件向子组件的数据传递。示例展示了在app.vue父组件中,使用`<template>`定义插槽,将标题、内容和作者信息传递给Article子组件。子组件article.vue接收到这些插槽内容并展示在相应的区域,如绿色背景的标题区、深粉背景的内容区和蓝色背景的作者区。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 APP父组件引入子组件 通过卡槽添加数据

app.vue

<template>
  <div id="app">
    <Article>
      <template #title>
        <h1>标题</h1>
      </template>

      <template v-slot:content>
        <h2>内容</h2>
      </template>

      <template #auth>
        <h3>作者</h3>
      </template>
    </Article>
  </div>
</template>

<script>
import Article from "@/components/Article";
export default {
  name: "App",
  components: {
    Article,
  },
};
</script>

<style lang="less">
#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>

article.vue

<template>
  <div>
<div class="title" msg="vue 您好">
      <slot name="title">

      </slot>
</div>
      <hr />
      <div class="content">
      <slot name="content">

      </slot>
</div>
 <hr />
<div class="auth">
      <slot name="auth">

      </slot>
</div>
      




  </div>
</template>

<script>
export default {

}
</script>

<style scoped>
 .title{
     padding:10px;
     background: green;
     color:#fff;
 }
  .content{
     padding:10px;
     background: deeppink;
     color:#fff;
 }
  .auth{
     padding:10px;
     background: blue;
     color:#fff;
 }
</style>

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值