Vue.js-----轻量高效的MVVM框架(使用slot分发内容)

#单个slot

html:

复制代码
<h3>#单个slot</h3>
<!-- 定义child01模板 -->
<template id="child01">
    <div>this is temp01 component!</div>
    <slot>
        如果没有分发内容,这里将会被显示
    </slot>
</template>
<div id="dr01">
    <child01></child01>
    <br /><br />
    <child01>
        <div>这里有新的内容01</div>
        <div>这里有新的内容02</div>
    </child01>
</div>
复制代码

js:

复制代码
//*********************************************************
//单个slot
var child01 = Vue.extend({
    template: "#child01",
});
var dr01 = new Vue({
    el: "#dr01",
    components: {
        "child01": child01
    }
});
复制代码

 结果展示:

  

 

#具名Slot(有名称的slot)

html:

复制代码
<h3>#具名Slot(有名称的slot)</h3>
<!-- 定义模板child02 -->
<template id="child02">
    <slot name="s1"></slot>
    <slot></slot>
    <slot name="s2"></slot>
</template>
<div id="dr02">
    <child02>
        <div slot="s1">this is slot01</div>
        <div slot="s2">this is slot02</div>
        <div>this is a simple div01</div>
        <div>this is a simple div02</div>
    </child02>
</div>
复制代码

 

js:

复制代码
//*********************************************************
//具名slot
var child02 = Vue.extend({
    template: "#child02"
});
var dr02 = new Vue({
    el: "#dr02",
    components: {
        "child02": child02
    }
});
复制代码

 

结果展示:

  

 

#编译作用域

html:

复制代码
<h3>编译作用域</h3>
<template id="child03">
    <div>the two items following is child msg:</div>
    <div>{{cmsg_01}}</div>
    <div>{{cmsg_02}}</div>
    <br />
    <div>the three items following is parent msg:</div>
    <slot name="s1"></slot>
    <slot></slot>
    <slot name="s2"></slot>
</template>
<div id="dr03">
    <child03>
        <div slot="s1">{{msg01}}</div>
        <div slot="s2">{{msg02}}</div>
        <div>{{msg03}}</div>
    </child03>
</div>
复制代码

 

js:

复制代码
//*********************************************************
//编译作用域
var child03 = Vue.extend({
    data: function() {
        return {
            cmsg_01: "this is child msg_01",
            cmsg_02: "this is child msg_02",
        }
    },
    template: "#child03",
})
var dr03 = new Vue({
    el: "#dr03",
    data: {
        msg01: "this is parent msg01",
        msg02: "this is parent msg02",
        msg03: "this is parent msg03",
    },
    components: {
        "child03": child03
    }
});
复制代码

 

结果展示:

  

分类:  Vue.js
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值