1. 程式人生 > 實用技巧 >vue-父元件向子元件傳遞引數

vue-父元件向子元件傳遞引數

父元件向子元件傳遞引數:

 注意:

  - 父元件傳送的形式是以屬性的形式繫結值到子元件身上。   - 然後子元件用屬性props接收   - 在props中使用駝峰形式,模板中需要使用短橫線的形式字串形式的模板中沒有這個限制
 <!-- 定義根元件 -->
  <div id="app">
    <!-- 在需要動態的資料的時候 需要屬性繫結的形式設定,靜態值不需要繫結形式傳遞 -->
   <a-father :father="father"></a-father>
  </div>

  <script>
    //
定義一個全域性的子元件,a-father是new vue的子元件 Vue.component('a-father',{ // 在需要動態的資料的時候 需要屬性繫結的形式設定,靜態值不需要繫結形式傳遞 template: '<div>{{father}}<hr/><h2><a-son :fistMsg="fistMsg"></a-son></h2></div>', data: function() { return{ fistMsg:'component第一個全域性元件傳遞過來的值
', fathers:'全域性子元件' } }, // 接收new Vue傳過來的引數, props:['father'], components:{ // a-son是a-father的子元件 'a-son':{ template:'<div>{{fistMsg}}</div>', data:function(){ return{ sonMsg:'a-father子元件' } },
// 接收a-first傳遞過來的值 props:['fistMsg'] } } }) const vm = new Vue({ el:'#app', data: { msg: 'hello', father:'new vue中的father', title:'titles' } }) </script>