免费视频淫片aa毛片_日韩高清在线亚洲专区vr_日韩大片免费观看视频播放_亚洲欧美国产精品完整版

打開APP
userphoto
未登錄

開通VIP,暢享免費電子書等14項超值服

開通VIP
04 Vue組件

組件

  • 每一個組件都是一個vue實例
  • 每個組件均具有自身的模板template,根組件的模板就是掛載點
  • 每個組件模板只能擁有一個根標(biāo)簽
  • 子組件的數(shù)據(jù)具有作用域,以達(dá)到組件的復(fù)用

1、根組件

<div id="app">    <h1>{{ msg }}</h1></div><script type="text/javascript">    // 通過new Vue創(chuàng)建的實例就是根組件(實例與組件一一對應(yīng),一個實例就是一個組件)    // 每個組件組件均擁有模板,template    var app = new Vue({        // 根組件的模板就是掛載點        el: "#app",        data : {            msg: "根組件"        },        // 模板: 由""包裹的html代碼塊,出現(xiàn)在組件的內(nèi)部,賦值給組件的$template變量        // 顯式書寫模塊,就會替換掛載點,但根組件必須擁有掛載點        template: "<div>顯式模板</div>"    })    // app.$template</script>

2、局部組件

<div id="app">    <local-tag></local-tag>    <local-tag></local-tag></div><script>    var localTag = {        data () {            return {                count: 0            }        },        template: '<button @click="btnAction">局部{{ count }}</button>',        methods: {            btnAction () {                this.count               }        }    }    new Vue({        el: "#app",        components: {            'local-tag': localTag        }    })</script>

3、全局組件

<div id="app">    <global-tag></global-tag>    <global-tag></global-tag></div><script>    Vue.component('global-tag', {        data () {            return {                count: 0            }        },        template: '<button @click="btnAction">全局{{ count }}</button>',        methods: {            btnAction () {                this.count               }        }    })    new Vue({        el: "#app"    })</script>

4、父組件傳遞數(shù)據(jù)給子組件

  • 通過綁定屬性的方式進(jìn)行數(shù)據(jù)傳遞
<div id="app">    <global-tag :sup_data1='sup_data1' :supData2='sup_data2'></global-tag></div><script type="text/javascript">    Vue.component('global-tag', {        props:['sup_data1', 'supdata2'],        template: '<div>{{ sup_data1 }} {{ supdata2 }}</div>'    })    new Vue({        el: '#app',        data: {            sup_data1: '數(shù)據(jù)1',            sup_data2: '數(shù)據(jù)2'        }    })</script>

5、子組件傳遞數(shù)據(jù)給父組件

  • 通過發(fā)送事件請求的方式進(jìn)行數(shù)據(jù)傳遞
<div id="app">    <global-tag @send_action='receiveAction'></global-tag></div><script type="text/javascript">    Vue.component('global-tag', {        data () {            return {                sub_data1: "數(shù)據(jù)1",                sub_data2: '數(shù)據(jù)2'            }        },        template: '<div @click="clickAction">發(fā)生</div>',        methods: {            clickAction () {                this.$emit('send_action', this.sub_data1, this.sub_data2)            }        }    })    new Vue({        el: '#app',        methods: {            receiveAction (v1, v2) {                console.log(v1, v2)            }        }    })</script>

6、父子組件實現(xiàn)todoList

<div id="app">    <div>        <input type="text" v-model="val">        <button type="button" @click="submitMsg">提交</button>    </div>    <ul>        <!-- <li v-for="(v, i) in list" :key="i" @click="removeMsg(i)">{{ v }}</li> -->        <todo-list v-for="(v, i) in list" :key="i" :v="v" :i="i" @delect_action="delect_action"></todo-list>    </ul></div><script type="text/javascript">    Vue.component("todo-list", {        template: "<li @click='delect_action'><span>第{{ i   1 }}條: </span><span>{{ v }}</span></li>",        props: ['v', 'i'],        methods: {            delect_action () {                this.$emit("delect_action", this.i)            }        }    })        new Vue({        el: "#app",        data: {            val: "",            list: []        },        methods: {            submitMsg () {                // 往list中添加input框中的value                if (this.val) {                    this.list.push(this.val);                    this.val = ""                }            },            delect_action(index) {                this.list.splice(index, 1)            }        }    })</script>
來源:https://www.icode9.com/content-4-560001.html
本站僅提供存儲服務(wù),所有內(nèi)容均由用戶發(fā)布,如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請點擊舉報。
打開APP,閱讀全文并永久保存 查看更多類似文章
猜你喜歡
類似文章
聊聊 VueJs 組件化編程
Vue3 組合式 API | 菜鳥教程
Vue基礎(chǔ)知識總結(jié)(絕對經(jīng)典)
Vue.js 入門:從零開始做一個極簡 To-Do 應(yīng)用
Vue2.x 中注冊自定義組件的3種方式
Vue組件封裝的過程
更多類似文章 >>
生活服務(wù)
分享 收藏 導(dǎo)長圖 關(guān)注 下載文章
綁定賬號成功
后續(xù)可登錄賬號暢享VIP特權(quán)!
如果VIP功能使用有故障,
可點擊這里聯(lián)系客服!

聯(lián)系客服