vue插槽的理解和用法
2019-02-28 17:30
645 查看
§1.插槽
理解:
插槽是什么东西,其实就vue提供的一个父组件在使用子组件的时候向子组件中传递结构的方法,子组件中的slot就相当于函数的形参,父组件中传递的结构就是实参,调用的结果就是实参代替了形参的内容。、
实例:
<!--父组件--> <template > <div id="app"> <SlotDemo> <template slot="header"> <p>{{ header }}</p> </template> <template slot="body"> <p>{{ body }}</p> </template> <template slot="footer" slot-scope="slotProps"> // 作用域插槽(用于向子组件向父组件传递,再由父组件传回) // 相当于在一个插槽的作用域对象上挂载了一个属性ht <p>{{ slotProps.ht }}</p> </template> </SlotDemo> </div> </template> data () { return { header:'我是脑袋', body:'身体', footer:'我是脚', } }, <!--子组件--> <template> <div> <slot name="header"></slot> <slot name="body"></slot> <slot name="footer" :ht="footer"></slot> </div> </template>
1.基本插槽:
<slot></slot>
2.具名插槽:为插槽指定名称。必须一一对应
<!--父组件--> <template slot="header"> <p>{{ header }}</p> </template> <!--子组件--> <slot name="header"></slot>
3.插槽的默认内容
<slot name="header">{ error }</slot> 当父组件给子组件传递结构时,子组件会使用默认值
4.编译作用域:父组件模板的所有东西都会在父级作用域内编译;子组件模板的所有东西都会在子级作用域内编译。
5.作用域插槽
<!--父组件--> <template slot="footer" slot-scope="slotProps"> // 作用域插槽(用于向子组件向父组件传递,再由父组件传回) // 相当于在一个插槽的作用域对象上挂载了一个属性ht <p>{{ slotProps.ht }}</p> </template> <!--子组件 --> <slot name="footer" :ht="footer"></slot>
相关文章推荐
- 理解vue中的插槽------slot与slot-scope
- Vuex之理解Mutations的用法实例
- 如何理解Vue的作用域插槽的实现原理
- Vue 中$els 的用法和理解
- Vuex之理解mutation的用法
- 深入理解Vue组件七——Vue中的作用域插槽
- vue slot插槽理解
- Vuex之理解action的用法
- vue中$on和$emit用法理解
- Vue插槽原理与用法详解
- 如何理解Vue的render函数的具体用法
- 理解Vuex的辅助函数mapState, mapActions, mapMutations用法
- vue使用插槽分发内容slot的用法
- vue插槽和作用域插槽的理解
- 理解Vue 2.0 的ref属性及简单用法
- Vuex之理解Getters的用法实例
- 详解vue2.6插槽更新v-slot用法总结
- 深入理解vue $refs的基本用法
- Vue框架-基础知识(vue指令、实例生命周期、计算属性与监听属性、插槽理解、组件介绍、数据交互、ES6语法等)
- Vue 组件和插槽的理解与使用