vue2 插槽基本使用

父级模板里的所有内容都是在父级作用域中编译的;子模板里的所有内容都是在子作用域中编译的。
1.基本使用

A组件  //navigation-link  
    <div>  <slot></slot>  </div>
B组件   <navigation-link>传 html, 组件 , 模板代码</navigation-link>
  1. 插槽具有默认值
A组件  //navigation-link 
    <button type="submit">  <slot>Submit</slot>  </butto>
B组件
    <navigation-link></navigation-link>  // 不传值 默认显示Submit

3.具名插槽

//A组件 // base-layout
    <div class="container">
  		<header>
    			<!-- 我们希望把页头放这里 -->
  		</header>
  		<main>
    			<!-- 我们希望把主要内容放这里 -->
  		</main>
  		<footer>
    			<!-- 我们希望把页脚放这里 -->
  		</footer>
	</div>  
//B组件  v-slot:xxx 只能写在template里面 / slot = xxx 可以直接写在html标签里面或者 template
   <base-layout>
  		<template v-slot:header>
    			<h1>Here might be a page title</h1>
  		</template>

  			<p>A paragraph for the main content.</p>
  			<p>And another one.</p>

  		<template v-slot:footer>
    			<p>Here's some contact info</p>
  		</template>
	</base-layout>
  // 或者
   <template slot="header">
    		<h1>Here might be a page title</h1>
  	</template>  
   // 或者  直接在标签里面
    <h1 slot="header">Here might be a page title</h1>
    

4 作用域插槽

//  子组件 的属性 能被父组件使用
子组件  //使用v-bind
    <span>
  		<slot v-bind:user="user">
    			{{ user.lastName }}
  		</slot>
	</span>
父组件 //使用  //slotProps 的命名 随意  :default 可以省略
    <current-user>
  		<template v-slot:default="slotProps">
    			{{ slotProps.user.firstName }}
  		</template>
	</current-user>
--------------------------------------------------------
// 类似  element 表格数据 自动排序$index 是 element表格的一个属性
  <el-table-column label="序号">
        <template v-slot="scope">
        	{{(page - 1) * size + scope.$index + 1}}
        </template>
  </el-table-column>
--------------------------------------------------------
// 多个插槽需使用完整语法
<current-user>
  	<template v-slot:default="slotProps">
    		{{ slotProps.user.firstName }}
  	</template>

  	<template v-slot:other="otherSlotProps">
    ...
  	</template>
     </current-user>
// 可以使用 解构语法
<current-user v-slot="{ user }">
  	{{ user.firstName }}
     </current-user>
<current-user v-slot="{ user: person }">
  	{{ person.firstName }}
      </current-user>
<current-user v-slot="{ user = { firstName: 'Guest' } }">   // 插槽有默认值
  	{{ user.firstName }}
     </current-user>

跟 v-on 和 v-bind 一样,v-slot 也有缩写,即把参数之前的所有内容 (v-slot:) 替换为字符 #。例如 v-slot:header 可以被重写为 #header:

<base-layout>
  <template #header>
    <h1>Here might be a page title</h1>
  </template>

  <p>A paragraph for the main content.</p>
  <p>And another one.</p>

  <template #footer>
    <p>Here's some contact info</p>
  </template>
</base-layout>

然而,和其它指令一样,该缩写只在其有参数的时候才可用。这意味着以下语法是无效的:

<!-- 这样会触发一个警告 -->
<current-user #="{ user }">
  {{ user.firstName }}
</current-user>
如果你希望使用缩写的话,你必须始终以明确插槽名取而代之:
<current-user #default="{ user }">
  {{ user.firstName }}
</current-user>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Vue 2 中的具名插槽是一种在父组件中定义并在子组件中使用的方式。以下是具名插槽使用方法: 1. 在父组件模板中,使用 `<slot>` 标签来定义具名插槽。你可以给 `<slot>` 标签添加一个 `name` 属性来指定插槽的名称。例如: ```html <template> <div> <slot name="header"></slot> <slot></slot> <slot name="footer"></slot> </div> </template> ``` 这里定义了三个具名插槽:`header`、默认插槽和 `footer`。 2. 在子组件中,使用 `<template>` 标签的 `v-slot` 指令来指定插槽对应的内容。你可以使用 `v-slot` 的参数来指定具名插槽的名称。例如: ```html <template> <div> <slot name="header"> <!-- 默认的插槽内容 --> <h1>This is the default header</h1> </slot> <p>Some default content...</p> <slot name="footer"></slot> </div> </template> ``` 在这个例子中,如果父组件没有提供名为 `header` 和 `footer` 的插槽内容,那么默认的内容将会显示出来。 3. 在父组件中使用子组件,并在父组件的模板中提供具名插槽的内容。你可以使用 `<template>` 标签的 `v-slot` 指令来提供插槽的内容。例如: ```html <template> <div> <my-component> <template v-slot:header> <!-- 插槽内容 --> <h1>This is a custom header</h1> </template> <p>This is some content...</p> <template v-slot:footer> <!-- 插槽内容 --> <footer>Footer content goes here</footer> </template> </my-component> </div> </template> ``` 在这个例子中,我们为 `header` 和 `footer` 插槽提供了自定义的内容。 这就是 Vue 2 中具名插槽基本用法。你可以在父组件中定义多个具名插槽,并在子组件中使用和提供这些插槽的内容。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值