国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 語言 > JavaScript > 正文

詳解vue.js2.0父組件點擊觸發子組件方法

2024-05-06 15:17:23
字體:
來源:轉載
供稿:網友

之前關于vue.js2.0父組件的一點學習,最近需要回顧,就順便發到隨筆上了

<body>   <div id="counter-event-example">    <p>{{ total }}</p>    <button-counter v-on:ee="incrementTotal"></button-counter>    <button-counter v-on:ee="incrementTotal"></button-counter>   </div>    <script>     Vue.component('button-counter', {      template: '<button v-on:click="increment">{{ counter }}</button>',      data: function () {       return {        counter: 0       }      },      methods: {       increment: function () {        this.counter += 1        this.$emit('ee', 'cc' )       }      },     })     new Vue({      el: '#counter-event-example',      data: {       total: 'arg'      },      methods: {       incrementTotal: function (b) {        this.total = b + '1';       }      }     })   </script> </body> 

子組件通過$emit觸發父組件的事件,$emit后面的參數是向父組件傳參,注意,父組件的事件處理函數直接寫函數名即可,不要加(),參數直接傳遞到了父組件的methods的事件處理函數了。

另外,寫一個小拾遺。vue子組件用了定義模板組件功能,然后在父組件里定義一個HTML元素綁定這個子組件后才能在父組件通過這個HTML元素使用。

再說一個非常方便的v-ref

<!DOCTYPE html> <html lang="en"> <head>   <meta charset="UTF-8">   <title>Document</title>   <script src="vue.js"></script> </head> <body> <div id="parent">    <input type="text" name="" id="" v-model="msg" />   <input type="button" id="" value="dianji" @click="clickDt" />   <user-profile ref="profile"></user-profile>  </div>    <script>    Vue.component('user-profile', {      template: '<span>{{ msg }}</span>',      data: function () {        return {         msg: 123       };     },      methods: {        greet: function (msg) {          console.log(msg);        }      }      })  //   var parent = new Vue({el: '#parent'});  //   var child = parent.$refs.profile;  //   child.greet();    new Vue({     el:"#parent",     data:{       msg:""     },     methods: {         clickDt(){         this.$refs.profile.greet(this.msg);       }     }   }) </script>  </body> </html> 

Vue2.0組件間數據傳遞

Vue1.0組件間傳遞

    使用$on()監聽事件; 使用$emit()在它上面觸發事件; 使用$dispatch()派發事件,事件沿著父鏈冒泡; 使用$broadcast()廣播事件,事件向下傳導給所有的后代

Vue2.0后$dispatch(),$broadcast()被棄用,見https://cn.vuejs.org/v2/guide/migration.html#dispatch-和-broadcast-替換 

1,父組件向子組件傳遞場景:Father上一個輸入框,根據輸入傳遞到Child組件上。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表

圖片精選

主站蜘蛛池模板: 金湖县| 蚌埠市| 陵川县| 扶风县| 鹿泉市| 大化| 乐都县| 彰武县| 古丈县| 碌曲县| 松滋市| 宁陕县| 清水县| 炎陵县| 科技| 北辰区| 浦东新区| 福安市| 萝北县| 四子王旗| 杨浦区| 策勒县| 万荣县| 丘北县| 柳河县| 资中县| 梁平县| 马关县| 于田县| 海丰县| 叙永县| 昭平县| 马边| 金平| 延庆县| 阳江市| 洱源县| 买车| 钦州市| 宁夏| 洪雅县|