最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

詳解vue.js數(shù)據(jù)傳遞以及數(shù)據(jù)分發(fā)slot

 更新時(shí)間:2018年01月20日 14:44:38   投稿:laozhang  
本篇文章給大家通過代碼實(shí)例分析了vue.js數(shù)據(jù)傳遞以及數(shù)據(jù)分發(fā)slot的相關(guān)知識(shí),有這方面興趣的朋友參考下吧。

一、組件間的數(shù)據(jù)傳遞

1.父組件獲取子組件的數(shù)據(jù)  

*子組件把自己的數(shù)據(jù),發(fā)送到父級(jí)

*vm.$emit(事件名,數(shù)據(jù));

*v-on: @

示例用法:當(dāng)點(diǎn)擊send按鈕的時(shí)候,“111”變成“我是子組件的數(shù)據(jù)”

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>父級(jí)獲取子級(jí)的數(shù)據(jù)</title>
  <script src="bower_components/vue/dist/vue.js"></script>
  <style>
  </style>
</head>
<body>
<div>
  <aaa>
  </aaa>
</div>
<template>
  <span>我是父級(jí) -> {{msg}}</span>
  //自動(dòng)調(diào)用get方法,@child-msg和下面的this.$emit('child-msg',this.a)相對(duì)應(yīng)
  <bbb @child-msg="get"></bbb>
</template>
<template>
  <h3>子組件-</h3>
  <input type="button" value="send" @click="send">
</template>
<script>
  var vm=new Vue({
    el:'#box',
    data:{
      a:'aaa'
    },
    components:{
      'aaa':{
        data:function(){
          return {
            msg:111,
            msg2:'我是父組件的數(shù)據(jù)'
          }
        },
        template:'#aaa',
        methods:{
          //這里的msg實(shí)際上就是子組件傳遞給父組件的數(shù)據(jù)
          get:function(msg){
            this.msg=msg;
          }
        },
        components:{
          'bbb':{
            data:function(){
              return {
                a:'我是子組件的數(shù)據(jù)'
              }
            },
            template:'#bbb',
            methods:{
              send:function(){
                this.$emit('child-msg',this.a);
              }
            }
          }
        }
      }
    }
  });
</script>
</body>
</html>

2、子組件獲取父組件的數(shù)據(jù)

在調(diào)用子組件:

<bbb :m="數(shù)據(jù)"></bbb>

子組件之內(nèi):

props:['m','myMsg']
props:{
'm':String,
'myMsg':Number
        }

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>自己獲取父級(jí)的數(shù)據(jù)</title>
  <script src="bower_components/vue/dist/vue.js"></script>
  <style>
  </style>
</head>
<body>
<div>
  <div>{{a}}</div>
  <aaa>
    {{msg}}
  </aaa>
</div>

<template>
  <h1>11111</h1>
  <bbb :mmm="msg2" :my-msg="msg"></bbb>
</template>
<script>
  var vm=new Vue({
    el:'#box',
    data:{
      a:'a'
    },
    components:{
      'aaa':{
        data:function(){
          return {
            msg:111,
            msg2:'我是父組件的數(shù)據(jù)'
          }
        },
        template:'#aa',
        components:{
          'bbb':{
            props:{
              'mmm':String,
              'myMsg':Number
            },
            template:'<h3>我是bbb組件->{{mmm}} <br> {{myMsg}}</h3>'
          }
        }
      }
    }
  });

</script>
</body>
</html>

運(yùn)行結(jié)果:

二、內(nèi)容分發(fā):

Vue.js提供了一種混合父組件內(nèi)容與子組件自己模版的方式:slot,用來占一個(gè)位置

1、基本用法 

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>slot保留原來的位置</title>
  <script src="bower_components/vue/dist/vue.js"></script>
  <style>
  </style>

</head>
<body>
<div>
  <aaa>
    <ul>
      <li>1111</li>
      <li>2222</li>
      <li>3333</li>
    </ul>
  </aaa>
  <hr>
  <aaa>
  </aaa>
</div>
<template>
  <h1>xxxx</h1>
  <slot>這是默認(rèn)的情況</slot>
  <p>welcome vue</p>
</template>

<script>
  var vm=new Vue({
    el:'#box',
    data:{
      a:'aaa'
    },
    components:{
      'aaa':{
        template:'#aaa'
      }
    }
  });

</script>
</body>
</html>

運(yùn)行結(jié)果:ul標(biāo)簽里面的內(nèi)容沒有被覆蓋,如果不使用slot,ul標(biāo)簽里的內(nèi)容將會(huì)被覆蓋

2、slot的name屬性 

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>slot中name屬性的使用</title>
  <script src="bower_components/vue/dist/vue.js"></script>
  <style>
  </style>
</head>
<body>
<div>
  <aaa>
    <ul slot="ul-slot">    //這里slot的名字要與下面slot中name屬性相對(duì)應(yīng)
      <li>1111</li>
      <li>2222</li>
      <li>3333</li>
    </ul>
    <ol slot="ol-slot">    //用法同上
      <li>111</li>
      <li>222</li>
      <li>333</li>
    </ol>
  </aaa>
  <hr>
  <aaa>
  </aaa>
</div>
<template>  
  <h1>xxxx</h1>
  <slot name="ol-slot">這是默認(rèn)的情況</slot>      //設(shè)置name屬性,給slot命名
  <p>welcome vue</p>
  <slot name="ul-slot">這是默認(rèn)的情況2</slot>
</template>

<script>
  var vm=new Vue({
    el:'#box',
    data:{
      a:'aaa'
    },
    components:{
      'aaa':{
        template:'#aaa'
      }
    }
  });

</script>
</body>
</html>

 運(yùn)行結(jié)果:

相關(guān)文章

最新評(píng)論

宝坻区| 栾城县| 道孚县| 达拉特旗| 延吉市| 东乌珠穆沁旗| 焉耆| 青神县| 吴江市| 吴忠市| 湖口县| 建水县| 宽城| 寻乌县| 甘谷县| 敦煌市| 阳谷县| 枣强县| 佛学| 新河县| 叙永县| 西平县| 龙里县| 南召县| 固原市| 牡丹江市| 邯郸县| 黄平县| 丰镇市| 大邑县| 津南区| 金山区| 南丹县| 永川市| 临安市| 承德市| 馆陶县| 哈巴河县| 沂源县| 清流县| 宝丰县|