vue2.0+基础知识连载(14)--- 局部组件与全局组件

<!DOCTYPE html>
<html>

<head>
    <title>vue2.0+基础知识连载(14)--- 局部组件与全局组件</title>
</head>

<body>

    <h3>vue2.0+基础知识连载(14)--- 局部组件与全局组件</h3>

    <div id='app'>
        <h5>局部组件</h5>
        <fatherlocal></fatherlocal>
        <hr>
        <h5>全局组件</h5>
        <father></father>
        <hr>
        <h5>全局组件</h5>
        <child :fromfather='giveData'></child>
    </div>

    <!-- 局部组件模板 -->
    <template id="father-local">
        <div>
            <h3>这是{{name}}</h1>
            <div>
                <p>这是{{data}}</p>
            </div>
        </div>
    </template>

    <!-- 全局组件模板 -->
    <template id="father">
        <div>
            <h3>这是{{name}}</h1>
            <div>
                <p>这是{{data}}</p>
            </div>
        </div>
    </template>

    <template id="child">
        <div>
            <h3>这是{{name}}</h3>
            <div>
                <p>{{cmsgtwo}}</p>
                <p>{{cmsg}}</p>
                <p>{{fromfather}}</p>
                <p>{{fromfather.fmsg}}</p>
                <p><input type="button" value="按钮" @click="change"></p>
            </div>
        </div>
    </template>

    <script src="https://cdn.bootcss.com/vue/2.5.8/vue.min.js"></script>
    <script type="text/javascript">

        // 定义组件
        var FATHER = {
            template: "#father",
            data: function() {
                return {
                    name: "父组件",
                    data: "父组件里的数据"
                }
            }
        };

        var CHILD = {
            template: "#child",
            data: function() {
                return {
                    name: "子组件",
                    cmsg: "子组件里的第一个数据",
                    cmsgtwo: "子组件里的第二个数据"
                }
            },
            methods: {
                change: function() {
                    this.fromfather.fmsg = "子组件数据被更改了"
                }
            },
            mounted: function() {
                this.cmsg = this.fromfather;
            },
            props: ["fromfather"],
        };

        // 注册组件
        Vue.component('father', FATHER);
        Vue.component("child", CHILD);

        var vm = new Vue({
            data: {
                fmsg: "data里的数据",
                giveData: {
                    fmsg: "这是父组件里的数据"
                }
            },
            methods: {},
            components: {
                'fatherlocal': {
                    template: '#father-local',
                    data: function() {
                        return {
                            name: "局部-父组件",
                            data: "局部-父组件里的数据"
                        }
                    }
                }
            }
        }).$mount('#app');

    </script>
</body>
</html>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值