Vue組件
組件 (Component) 是 Vue.js 最強大的功能之一。html
組件能夠擴展 HTML 元素,封裝可重用的代碼。是可複用的Vue實例。app
組件的註冊
// html 代碼 <div id="app"> <my-component></my-component> </div> // js 代碼 Vue.component('my-component', { template: '<div>A component!</div>' }) var app = new Vue({ el: '#app', data: { } });
// html 代碼 <div id="app"> <my-component></my-component> </div> // js 代碼 // 組件中的data必須是個函數 var Child = { template: '<div>A component!</div>', data: function() { return { name: "gao", } }}; new Vue({ // ... components: { // <my-component> 將只在父組件模板中可用 'my-component': Child } })
// js 代碼 Vue.component('child', { template: `<div><button @click="on_click()">{{msg}}</button></div>`, data: function () { return { msg: "點我", } }, methods: { on_click(){ alert(123) } } }); new Vue({ el: "#app", })
<script> var my_component = { template: `<div><h1>{{msg}}</h1></div>`, data(){ return{ msg: "這是子組件" } } }; var global_component = { template: `<div> <h1>{{msg}}</h1> <button @click="on_click">點我</button> <my_component></my_component> </div>`, data(){ return { msg: "全局組件" } }, methods: { on_click() { alert("123") } }, components:{ my_component:my_component, } }; const app = new Vue({ el: "#app", data: { }, components: { global_component: global_component, // my_component: my_component, } }); </script>
組件之間的通訊
咱們的組件在任何地方用的時候都要是一個樣子麼~ide
可不能夠咱們給組件傳個參數~讓組件在不一樣的地方表現不一樣的狀態~函數
咱們以前說過博客評論@某某某,點擊用戶名能夠跳轉到該用戶站點。post
這樣一個小功能,咱們每次@的時候都要寫,咱們能夠封裝成組件,傳值便可~~this
// html 代碼 <div id="app"> <child username="gaoxin"></child> </div> // js 代碼 Vue.component('child', { template: `<a :href="'/user/'+ username">{{username}}</a>`, props: ["username"], }); var app = new Vue({ el: "#app", data:{ name: "@gaoxin" } });
app.$on(event, callback) 監聽當前實例上的自定義事件,事件由$emit觸發,回調函數接收事件觸發器額外參數。spa
app.$emit(event, [args....]) 觸發當前實例上的事件,額外參數傳給監聽器的callback回調函數。code
// html 代碼 <div id="app"> <parent></parent> </div> // js 代碼 Vue.component('parent',{ template: ` <div> <child @show_balance="show"></child> <p v-if="active">您的餘額998</p> </div> `, data: function () { return { active: false, } }, methods: { show: function(data){ this.active=true; console.log(data) } } }); Vue.component('child', { template: `<div><button @click="on_click()">{{msg}}</button></div>`, data: function () { return { msg: "顯示餘額", } }, methods: { on_click(){ // alert(123) this.$emit('show_balance', {q:1,b:2}) } } });
平行組件之間的通訊,喊話須要一箇中間調度器,在組件加載完成以後去監聽調度器事件,回調函數接收數據。component
// html 代碼 <div id="app"> <whh></whh> <shh></shh> </div> // js 代碼 var Event = new Vue() Vue.component('whh',{ template: ` <div> 我說: <input @keyup="on_change" v-model="i_said"> </div> `, data: function () { return { i_said: '', } }, methods: { on_change: function () { Event.$emit("whh_said_something", this.i_said) } } }); Vue.component('shh', { template: ` <div> 花花說:{{whh_said}} </div> `, data: function () { return { whh_said: '', } }, mounted: function () { var me = this Event.$on('whh_said_something', function (data) { me.whh_said = data }) } });
混合Mixins
重複功能和數據的儲存器,能夠覆蓋Mixins的內容。htm
// 點擊顯示和隱藏 提示框的顯示和隱藏 // html 代碼 <div id="app"> <PopUp></PopUp> <ToolTip></ToolTip> </div> // js 代碼 var base = { data: function () { return { visible: false, } }, methods: { show: function () { this.visible = true }, hide: function () { this.visible = false } } } Vue.component('popup', { template:` <div> <button @click="show">PopUp show</button> <button @click="hide">PopUp hide</button> <div v-if="visible"><p>hello everybody</p></div> </div> `, mixins: [base], data: function () { return { visible: true, } } }); Vue.component('tooltip', { template: ` <div> <div @mouseenter="show" @mouseleave="hide">ToolTip</div> <div v-if="visible"><p>ToolTip</p></div> </div> `, mixins: [base] }); new Vue({ el: "#app", })
插槽 Slot
插槽是一套內容分發的API,在組件中,<slot>做爲內容承載分發的出口
// html 代碼 <div id="app"> <panel> <div slot="title"> HELLO</div> <div slot="content">hello</div> </panel> <panel></panel> <panel></panel> </div> <template id="panel-tpl"> <div class="panel"> <div class="title"> <slot name="title"></slot> </div> <div class="content"> <slot name="content"></slot> </div> <!--<div class="content">Failure is probably the fortification in your pole. It is like a peek your wallet as the thief, when you are thinking how to spend several hard-won lepta,</div>--> <div class="footer"> <slot name="footer">更多信息</slot> </div> </div> </template> // js 代碼 Vue.component('panel', { template: '#panel-tpl', }); new Vue({ el: "#app", })