zoukankan      html  css  js  c++  java
  • 在Vue中使用插槽

    在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>
  • 相关阅读:
    常用业务接口界面化 in python flask
    git命令中带有特殊符号如@
    生成唯一标识 字符串跟时间戳的结合
    MD5 in JAVA
    修改(同步)linux时间
    jenkins 从git拉取代码
    Git 默认不区分大小写
    postman也可以使用F12功能
    Session
    WebXML部署服务
  • 原文地址:https://www.cnblogs.com/tengteng0520/p/12070208.html
Copyright © 2011-2022 走看看