首页 > 其他 > 详细

在Vue中使用插槽

时间:2019-12-19 23:01:12      阅读:89      评论:0      收藏:0      [点我收藏+]

在Vue中使用插槽

组件的template里可以在任意位置添加<slot></slot>,slot为标签,称为插槽,像是一个借口,接受html数据。

具名插槽,slot标签可以添加name属性,用与区分组件中不同插槽

<slot name="header"></slot>

<slot name="header">默认值</slot>

这样在调用组件时就可以在innerHTML里写入对应的html数据

<div slot="header"></div>这样就会把这个标签下的,包括这个标签,所有html元素替换到<slot name="header"></slot>的位置,本质和v-html没什么区别。

<body>
  <div id="root">
    <child content="Dell"></child>
    <child content="Lee"></child>
  </div>
  <script>
    Vue.prototype.bus=new Vue()
    Vue.component(child,{
      data:function(){
        return{
          selfContent:this.content
        }
        
      },
      props:{
        content:String,
      },
      template:<div @click="handleClick">{{ selfContent}}</div>,
      methods:{
        handleClick:function(){
          //alert(this.content)
          this.bus.$emit(change,this.selfContent)

        }
      },
      mounted:function(){
        var this_=this
        this.bus.$on(change,function(msg){
         this_. selfContent=msg
        })
      }
    })
    var vm=new Vue({
      el:#root
    })
  </script>
 
</body>

在Vue中使用插槽

原文:https://www.cnblogs.com/tengteng0520/p/12070208.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!