vue事件中心(組件間的數據傳遞)


<!DOCTYPE html>
<html>

    <head>
        <meta charset="UTF-8">
        <title></title>
        <!--<script src="https://unpkg.com/vue@2.1.10/dist/vue.js" type="text/javascript" charset="utf-8"></script>-->
        <script src="http://cdnjs.cloudflare.com/ajax/libs/vue/1.0.26/vue.min.js" type="text/javascript" charset="utf-8"></script>
    </head>

    <body>
        <div id="app">
<component-a> </component-a> <component-b /> </div> <script> var bus = new Vue() Vue.component('component-a', { template: '<div>This is App Content {{ info }}</div>', created() { bus.$on('anotherclick', (res) => { //接收 this.info = res; }) }, data: () => { return { info: '' } } }) Vue.component('component-b', { template: '<div @click="emit">This is Another Content </div>', ready: function () { bus.$emit('anotherclick','aaa')//發送 }, // mounted: function () { // this.$nextTick(function () { // bus.$emit('anotherclick','aaa')//發送 // }) // } }) var vm = new Vue({ el: '#app' }) </script> </body> </html>

1.兼容vue1.0和2.0 取代$broadcast  $dispatch

2.vue-bus   配合webpack一起使用

https://github.com/yangmingshan/vue-bus

http://div.io/topic/1864


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM