Vue 全局變量,局部變量

2019-4-17    seo達(dá)人

如果您想訂閱本博客內(nèi)容,每天自動發(fā)到您的郵箱中, 請點(diǎn)這里

全局組件和局部組件
* 1.先定義組件   Vue.component('組件名', { 組件模板對象 })
*   注意: 組件名不要使用原生的標(biāo)簽名, 若組件名定義時(shí)用的是駝峰命名法, 則調(diào)用時(shí)用中劃線分割后小寫
*                       例如: 組件-->mtText   使用時(shí)-->   <my-text></my-text>
* 2.配置組件的模板  注意: 組件的模板內(nèi)容有且只有一個(gè)根元素
* 3.在視圖層里調(diào)用 ,用雙標(biāo)簽
* 4.組件是一個(gè)獨(dú)立的作用域, 也可以看成一個(gè)特殊的vue實(shí)例, 可以有data, methods,computed等等
*   注意: 組件的data是函數(shù), 函數(shù)中需要返回一個(gè)對象作為組件的data
全局組件案例

<body>
<div id="app">
    <my-component></my-component>
</div>
<script src="lib/vue-2.4.0.js"></script>
<script>
//全局組件
    Vue.component('myComponent',{
        //1.組件的內(nèi)容/模板
        template: '<div><div>頭部組件</div><h1 @click="fn">呵呵{{msg}}</h1></div>',
        data(){
            return {
                msg:'hello,組件'
            }
        },
        methods:{
            fn(){
                console.log(this.msg);
            }
        }
    })
    let vm = new Vue({
        el:"#app",
        data:{
        },
        methods:{

        },

    })
</script>
</body>
局部組件案例

<body>
<div id="app">
    <my-component></my-component>
    <my-test></my-test>
</div>
<template id="box1">
    <h1>haha</h1>
</template>
<template id="box2">
    <div>
        <ul>
            <li v-for="item in arr">
                {{ item }}
            </li>
        </ul>
    </div>
</template>
<script src="lib/vue-2.4.0.js"></script>
<script>
let vm = new Vue({
        el:"#app",
        data:{
        },
        methods:{

        },
        //局部子組件
        components:{
            // 組件名: {配置項(xiàng)}
            "myComponent":{
                template:'#box1',
                data(){
                    return {
                        msg:"哈哈"
                    }
                }
            },
            "myTest":{
                template:"#box2",
                data(){
                    return {
                        arr:[1,2,3,4]
                    }
                }
            }
        }
    })
</script>
</body>
組件切換:法一

<body>
<div id="app">
    <a href="" @click.prevent="flag=true">登錄</a>
    <a href="" @click.prevent="flag=false">注冊</a>
    <login v-if="flag"></login>
    <register v-else="flag"></register>
</div>
<script src="lib/vue-2.4.0.js"></script>
<script>
    Vue.component("login",{
        template:"<h1>登錄組件</h1>"
    })
    Vue.component("register",{
        template:"<h1>注冊組件</h1>"
    })
    let vm = new Vue({
        el:"#app",
        data:{
            flag: false
        },
        methods:{
        },
    })
</script>
</body>
組件切換:法二

 <style>
        .red{
            color:red;
        }
        .v-enter{
            opacity:0;
            transform: translateX(150px);
        }
        .v-leave-to{
            opacity:0;
            transform: translateX(-150px);
        }
        .v-enter-active,
        .v-leave-active{
            transition: all 0.5s;
            position: absolute;
        }
    </style>
</head>
<body>
<div id="app">
    <a href="" :class="{red: flag=='login'}" @click.prevent="flag='login'">登錄</a>
    <a href="" :class="{red: flag=='register'}" @click.prevent="flag='register'">注冊</a>
    <!--  vue提供了一個(gè)標(biāo)簽  component標(biāo)簽(理解為一個(gè)占位符), 用來展示對應(yīng)名稱的組件  :is屬性設(shè)置指定的組件名  -->
    <transition>
        <component :is="flag"></component>
    </transition>
</div>
<script src="lib/vue-2.4.0.js"></script>
<script>
    Vue.component("login",{
        template:"<h1>登錄組件</h1>"
    })
    Vue.component("register",{
        template:"<h1>注冊組件</h1>"
    })
    let vm = new Vue({
        el:"#app",
        data:{
            flag: "login"
        },
        methods:{

        },
    })
</script>
</body>
父組件向子組件傳值

<body>
<div id="app">
    <my-component :fromfather="father"></my-component>
</div>
<template id="box1">
    <h1 @click="change">
        {{ fromfather }}
        子組件的數(shù)據(jù)
    </h1>
</template>
<template id="grandSon">
    <h1>孫子組件的數(shù)據(jù)</h1>
</template>
<!--1.子組件不能訪問父組件的數(shù)據(jù)
2. 解決辦法: ①在引用子組件時(shí), 通過屬性綁定 v-bind方法, 把需要傳遞給子組件的數(shù)據(jù)以綁定的形式傳過來
              ② 在子組件配置項(xiàng)里添加 props: ['傳遞過來的數(shù)據(jù)']-->
<script src="lib/vue-2.4.0.js"></script>
<script>
    let vm = new Vue({
        el:"#app",
        data:{
            father:'啊~~這是父組件的數(shù)據(jù)'
        },
        methods:{
        },
        //局部子組件
        components:{
            // 組件名: {配置項(xiàng)}
            "myComponent":{
                template:'#box1',
                data(){
                    return {
                        msg:"哈哈"
                    }
                },
                //在子組件配置項(xiàng)里添加 props: ['傳遞過來的數(shù)據(jù)']
                //注意: 組件中所有的props中的數(shù)據(jù), 都是通過父組件傳遞給子組件的, props中的數(shù)據(jù)是只讀, 無法修改
                props:['fromfather'],
                methods:{
                    change(){
                       // this.fromfather = "被修改了"
                    }
                },
                //局部子子組件
                components:{
                    'grandSon':{
                        template:'#grandSon'
                    }
                }
            }
        }
    })
</script>
</body>
藍(lán)藍(lán)設(shè)計(jì)www.bouu.cn )是一家專注而深入的界面設(shè)計(jì)公司,為期望卓越的國內(nèi)外企業(yè)提供卓越的UI界面設(shè)計(jì)、BS界面設(shè)計(jì) 、 cs界面設(shè)計(jì) 、 ipad界面設(shè)計(jì) 、 包裝設(shè)計(jì) 、 圖標(biāo)定制 、 用戶體驗(yàn) 、交互設(shè)計(jì)、 網(wǎng)站建設(shè) 、平面設(shè)計(jì)服務(wù)

分享本文至:

日歷

鏈接

個(gè)人資料

存檔