vue插槽<作用域插槽>

作者: zz77zz | 来源:发表于2019-05-29 07:56 被阅读2次

2019年5月29日07:51:57
插槽 - 这里之前没怎么注意 这次算是懂了个大概

作用域插槽 应用场景 当子组件循环或者dom结构需要外部父子间传递的时候用

必须使用 template标签 -> slot-scope接收 ->slot定义属性传递值给template 就这三部其实

代码如下

<!DOCTYPE html>
<html>
<head>
    <title>test for vue</title>
    <script src="https://cdn.jsdelivr.net/npm/vue@2.6.10/dist/vue.js"></script>
</head>
<body>
<div id="app">
    <!-- 4-5 -->
    <!-- <h3>非父子组件间传值(Bus/总线/发布订阅模式/观察者模式)</h3>
    <child content="Dad gives u"></child>
    <child content="This money"></child> -->
    <!-- 4-6/4-7 -->
    <h3>插槽 slot/作用域插槽</h3>
    <child>
        <!-- <h4>这里是子组件需要插槽的part</h4> -->
        <div class="header" slot="header">Header 网页头</div>
        <div class="footer" slot="footer">Footer 网页脚</div>
    </child>

    <test_for_slot>
        <template slot-scope="SlotData">
        <h2>{{SlotData.thisis_shuxing}}</h2>
        </template>
    </test_for_slot>

</div>
<script type="text/javascript">

// 4-5
// // 非父子组件 用总线机制 其实就是给vue挂了个全局的属性
// Vue.prototype.bus = new Vue()

//  Vue.component('child',{
//      props:{
//          content:String
//      },
//      data(){
//          return {
//              selfContent:this.content
//          }
//      },
//      template:'<div @click="handleClick">{{selfContent}}</div>',
//      methods:{
//          handleClick(){
//              // alert(this.content)
//              this.bus.$emit('explode_func',this.selfContent)
//          }
//      },
//      mounted:function(){
//          var _that = this
//          this.bus.$on('explode_func',function(msg){
//              alert(msg) //这里会跳出两次 以为组件有两个 都监听了方法
//              _that.selfContent = msg
//          })
//      }
//  })

4-6

    Vue.component('child',{
        props:['content'],
        template:`
                    <div>
                        <slot name="header">
                            <h4>如果插没有传值 默认显示内容
                            </h4>
                        </slot>

                        <div>中间内容</div>

                        <slot name="footer"></slot>
                    </div>
                 `
    })

    Vue.component('test_for_slot',{
        data:function(){
            return {
                list:['a','b','c','d']
            }
        },
        template:`<div>
                    <ul>
                    <slot v-for="item of list"
                            :thisis_shuxing=item
                    >
                         {{item}}
                    </slot>
                    </ul>
                </div>`
    })

    var app = new Vue({
        el:"#app"
    })

</script>
</body>
</html>

Slot肯定还有别的使用场景 只是没有讲到 以后项目遇到了再做个总结

下一节是动态组件跟v-once指令

相关文章

  • vue插槽<作用域插槽>

    2019年5月29日07:51:57插槽 - 这里之前没怎么注意 这次算是懂了个大概 作用域插槽 应用场景 当子组...

  • Vue之深入理解插槽—slot, slot-scope, v-s

    Vue 2.6.0 以前Vue 2.6.0 以后具名插槽 slot具名插槽 v-slot作用域插槽 slot-sc...

  • 18、Vue3 作用域插槽

    作用域插槽:让插槽内容能够访问子组件中,vue2中作用域插槽使用slot-scope,vue3中使用v-slot ...

  • vue----slot插槽

    插槽分类 匿名插槽 具名插槽 作用域插槽

  • 2.插槽

    匿名插槽 具名插槽 作用域插槽

  • vue 插槽

    插槽语法是Vue实现的内容分发API,用于复合组件开发。 匿名插槽 具名插槽 作用域插槽 将内容分发到子组件指定位置

  • vue3中的插槽

    插槽 默认插槽 具名插槽,v-slot可以简写为# 动态插槽 #[dynamicSlotName] 作用域插槽(...

  • slot 用法以及使用场景

    Vue的插槽slot,分为3种 匿名插槽 具名插槽 作用域插槽 前两种很好理解,无非就是子组件里定义一个slot占...

  • vue中slot插槽的使用

    插槽的种类:1、默认插槽、具名插槽、作用域插槽、解构插槽、动态插槽几种。转原文:https://www.jians...

  • vue插槽slot

    -具名插槽 子组件Demo.vue 引用子组件的页面 -作用域插槽 子组件Demo.vue 引用子组件的页面

网友评论

    本文标题:vue插槽<作用域插槽>

    本文链接:https://www.haomeiwen.com/subject/kbnstctx.html