props是用来接收参数的 例如父组件向子组件传参 可以放在props中
slot:插槽 slot分发模式主要用于在组件中插入标签或者组件之间的相互嵌套,个人认为如果组件中有需要单独定义的地方可以使用slot
例子:
现在父组件中占个位置
<template id="a">
<div>hello!</div>
<slot>没有内容 就显示这个</slot>
</template>
var a = vue.extend({
template: '#a'
})
子:
<div id="child">
<A></A>
<hr/>
<A>
<h1>内容1</h1>
<h1>内容2</h1>
</A>
</div>
var child = new Vue({
el: '#child',
components: {
"A":A
}
})
结果:
hello!
没有内容 就显示这个
hello!
内容1
内容2
<template id="b">
<slot name="slot1"></slot>
<slot></slot>
<slot name="slot2"></slot>
</template>
var B = Vue.extend({
template: "#b"
});
<div id="child2">
<B>
<div slot="slot1">this is slot01</div>
<div slot="slot2">this is slot02</div>
<div>aaa</div>
<div>bbb</div>
</B>
</div>
var child2 = new Vue({
el: "#child2",
components: {
"B": B
}
});
结果:
this is slot01
aaa
bbb
this is slot02
子=> 父
子:
<slot name="b" :msg="hello"></slot>
父:
<template slot="b" **slot-scope**="props">
<child>
<div class="">
{{props.msg}}
</div>
</child>
</template>
作用域插槽更典型的用例是在列表组件中,允许使用者自定义如何渲染列表的每一项:
<mylist :items="items">
<!-- 作用域插槽也可以是具名的 -->
<li
slot="item"
slot-scope="props"
class="my-fancy-item">
{{ props.text }}
</li>
</mylist>
列表组件的模板:
<ul>
<slot name="item"
v-for="item in items"
:text="item.text">
<!-- 这里写入备用内容 -->
</slot>
</ul>
来源:https://segmentfault.com/a/1190000012314707
插槽用于内容分发,存在于子组件之中。插槽作用域:父级组件作用域为父级,子级组件作用域为子级,在哪定义的作用域就在哪。子组件之间的内容是在父级作用域的,无法直接访问子组件里面的数据。
vue 为我们提供了很多复用性的方式,slot 和 mixins 就是其中两种...下面对这两种方式做一下记录,插槽使用场景- 该组件被多个地方使用- 每个父组件中对该组件的内部有一部分需要特殊定制
最近发布不久的Vue 2.6,使用插槽的语法变得更加简洁。 对插槽的这种改变让我对发现插槽的潜在功能感兴趣,以便为我们基于Vue的项目提供可重用性,新功能和更清晰的可读性。 真正有能力的插槽是什么?
内容以共享、参考、研究为目的,不存在任何商业目的。其版权属原作者所有,如有侵权或违规,请与小编联系!情况属实本人将予以删除!