vue組件父與子通訊詳解(一),vue組件父與子詳解

來源:互聯網
上載者:User

vue組件父與子通訊詳解(一),vue組件父與子詳解

本文執行個體為大家分享了vue組件父與子通訊的具體代碼,供大家參考,具體內容如下

一、組件間通訊(父組件    -->  子組件)

步驟:

①父組件在調用子組件 傳值

<child-component myValue="123"> </child-component>

②在子組件中 擷取父組件傳來的值

Vue.component('child-component',{  props:['myValue'],  template:''})

代碼1:

<!doctype html><html> <head> <meta charset="UTF-8"> <title>父傳子</title> <script src="js/vue.js"></script> </head> <body> <div id="container"> <p>{{msg}}</p> <parent-component></parent-component> </div> <script> // 在vue中一切都是組件 //父傳子 Vue.component("parent-component",{  data:function(){  return {   gift:"傳家寶"  }  },  template:`  <div>   <h1>這是父組件</h1>   <hr>   <child-component v-bind:myValue="gift"></child-component>  </div>  ` }) Vue.component("child-component",{  props:["myValue"],  template:`  <div>   <h1>這是子組件</h1>   <p>{{"父傳遞的值:"+myValue}}</p>  </div>  ` }) new Vue({  el:"#container",  data:{  msg:"Hello VueJs"  } }) </script> </body></html>

myValue是屬性名稱,必須都一樣……拿data中的用v-bind:或者:
props是property屬性,是個數組

代碼2:

<!doctype html><html> <head> <meta charset="UTF-8"> <title>父子之間通訊練習</title> <script src="js/vue.js"></script> </head> <body> <div id="container"> <p>{{msg}}</p> <my-login></my-login> </div> <script>/* 登入視窗 建立4個組件,分別是my-label my-input my-button my-login(複合組件)*/ Vue.component("my-label",{  props:["myLabel"],  template:`  <div>   <label>{{myLabel}}</label>  </div>  ` }) Vue.component("my-input",{  template:`  <div>   <input type="text"/>  </div>  ` }) Vue.component("my-button",{  props:["myButton"],  template:`  <div>   <button>{{myButton}}</button>  </div>  ` }) //複合組件 Vue.component("my-login",{  data:function(){  return {   uname:"使用者名稱",   upwd:"密碼",   login:"登入",   register:"註冊"  }  },  template:`  <div>  <my-label v-bind:myLabel="uname"></my-label>  <my-input></my-input>  <my-label v-bind:myLabel="upwd"></my-label>  <my-input></my-input>  <my-button v-bind:myButton="login"></my-button>   <my-button v-bind:myButton="register"></my-button>  </div>  ` }) new Vue({  el:"#container",  data:{  msg:"Hello VueJs"  } }) </script> </body></html>

代碼3:

<!DOCTYPE html><html><head lang="en"> <meta charset="UTF-8"> <script src="js/vue.js"></script> <title></title></head><body><div id="container"> <my-login></my-login></div><script> Vue.component('my-label',{ props:['labelName'], template:'<label>{{labelName}}</label>' }) Vue.component('my-input',{ props:['tips'], template:'<input type="text" :placeholder="tips"/>' }) Vue.component('my-button',{ props:['btnName'], template:'<button>{{btnName}}</button>' }) Vue.component('my-login',{ template:` <form>  <my-label labelName="使用者名稱"></my-label>  <my-input tips="請輸入使用者名稱"></my-input>  <br/>  <my-label labelName="密碼"></my-label>  <my-input tips="請輸入密碼"></my-input>  <br/>  <my-button btnName="登入"></my-button>  <my-button btnName="註冊"></my-button> </form> ` }) new Vue({ el: '#container', data: {  msg: 'Hello Vue' } })</script></body></html>

要拿到data中的資料就要v-bind,否則就不用。

以上就是本文的全部內容,希望對大家的學習有所協助,也希望大家多多支援幫客之家。

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.