vue2[初级] solt插槽使用

回顾上节

– 上节内容 通过PubSub组件实现 组件的订阅与发布
– 父组件中订阅 通过PubSub.subcribe(‘pubName’,(msg,data)=>{…})
– 子组件中发布 通过PubSub.publish(‘pubName’,valueObject);

本节内容

  • 前面都是传递至或传递方法到子组件中,solt则是传递标签到子组件中
  • 主要作用 实现局部刷新
    – 子组件中的某个区域先占位置 然后动态搞个标签进去 方便切换该位置的内容 无需在做多个页面

实现方式

– 新增一个组件 Foot.vue
– Foot.vue的template中添加slot插槽

// sName 是slot的名称 用来区分不同的slot插槽
// 插槽可以设置默认内容 如果父组件传递slot过来则会被替换 不传递则使用默认内容
<slot name="sName">默认插槽内容</slot>

– 父组件中引入Foot组件
– import Foot from ‘./components/Foot’
– 引入之后就可以映射Foot标签了 映射Foot标签后可以使用Foot标签了

// 使用Foot标签
<Foot>
    <div slot="sName">
      版权信息 :{
  { side }}
    </div>
  </Foot>
  // 引入Foot组件
<script>
import Foot from './components/Foot'
export default {
// 映射标签
components:{
Menu22
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Vue 2 中,(slot)是一种用于在组件中入内容的机制。它允许你在组件的模板中定义带有特定名称的,并在使用该组件时传递内容到这些中。 以下是 Vue 2 中的基本使用方法: 1. 在组件模板中定义: ```html <template> <div> <slot></slot> </div> </template> ``` 2. 在使用组件时,将内容传递给: ```html <template> <div> <my-component> <p>This content will be inserted into the slot.</p> </my-component> </div> </template> ``` 在上面的示例中,`<slot></slot>`是一个匿名,可以接受任意传递进来的内容。当使用 `<my-component>` 组件时,传递给组件的内容将被入到这个匿名中。 除了匿名外,你还可以为指定名称。这样,在使用组件时,你可以根据的名称将内容分发到不同的中。 例如,假设你的组件有两个具名:`header` 和 `footer`,你可以按照以下方式使用它们: ```html <template> <div> <header> <slot name="header"></slot> </header> <main> <slot></slot> </main> <footer> <slot name="footer"></slot> </footer> </div> </template> ``` ```html <template> <div> <my-component> <template v-slot:header> <h1>This content will go into the header slot.</h1> </template> <p>This content will go into the main slot.</p> <template v-slot:footer> <p>This content will go into the footer slot.</p> </template> </my-component> </div> </template> ``` 在这个示例中,`<slot name="header"></slot>` 和 `<slot name="footer"></slot>` 是具名使用 `v-slot` 指令(或简写的 `#`)可以指定内容要分发到哪个具名中。 希望这个回答能够帮助你理解 Vue 2 中的基本使用。如有任何疑问,请随时提问!

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值