Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

【vue】组件的使用(4)使用插槽 Slot ,以及slot-scope作用域插槽 #20

Open
Kelichao opened this issue Feb 3, 2017 · 0 comments

Comments

@Kelichao
Copy link
Owner

Kelichao commented Feb 3, 2017

Slot插槽

使用特殊的 元素作为原始内容的插槽。

Slot的使用

  • 如果子组件是如下代码片段
<div>
  <h2>我是子组件的标题</h2>
  <slot>
   我是默认内容
  </slot>
  <!--name为对应标志位-->
  <slot name="head">
   我是默认head内容
  </slot>

</div>
  • 子组件在父组件写法如下
<!-- 键值对中间没有内容,则slot显示为 “我是默认内容。 ”-->
<keyboard :total.sync="vinInput" v-on:sure="getCarInfo"></keyboard>

<!-- 键值对中间有内容,则slot显示为 “<div>123321</div><div>222222222222</div> ”-->
<keyboard :total.sync="vinInput" v-on:sure="getCarInfo">
    <div>123321</div>
    <div slot="head">222222222222</div>
</keyboard>

作用域插槽slot-scope的使用

子组件

<div>
	<h3>这里是子组件</h3>
	<slot :data="data2"></slot>
</div>
<script>
data() {
	return {
	    data2: ['zhangsan', 'lisi', 'wanwu', 'zhaoliu', 'tianqi', 'xiaoba']
},
</script>

父组件处

<keyboard :total.sync="vinInput" v-on:sure="getCarInfo">
       <!-- 使用处只需添加 slot-scope="props"-->
	<template slot-scope="user">
		<div class="tmpl3">
			 <!-- <span>{{user}}</span> -->
			<span v-for="item in user.data">{{item}}</span>
		</div>
	</template>
</keyboard>

template循环引用报错以及解决办法

  • Duplicate presence of slot "up" found in the same render tree - this will likely cause render errors.
  • 解决父组件用到子组件的时候出现循环引用所导致的报错解决办法添加slot-scope="{}"
  • 注意需要添加在父组件
  • <template slot="up" slot-scope="{}">
@Kelichao Kelichao added vue and removed vue labels Feb 3, 2017
@Kelichao Kelichao changed the title 【vue】组件的使用(4)使用 Slot 分发内容 【vue】组件的使用(4)使用插槽 Slot 分发内容 Apr 16, 2018
@Kelichao Kelichao changed the title 【vue】组件的使用(4)使用插槽 Slot 分发内容 【vue】组件的使用(4)使用插槽 Slot ,以及slot-scope作用域插槽 Apr 16, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant