溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

Vue父子模版?zhèn)髦导敖M件傳值的三種方法

發(fā)布時(shí)間:2020-09-11 03:18:07 來(lái)源:腳本之家 閱讀:366 作者:雨歇微涼 欄目:web開(kāi)發(fā)

這里是針對(duì)于vue1.0,如果要學(xué)2.0,建議大家去看官方文檔

vue2.0 http://vuefe.cn/guide/

vue-router2.0https://router.vuejs.org/zh-cn/essentials/getting-started.html

第一種

<div id="example">
  <my-component></my-component>
</div>
<script src="../node_modules/vue/dist/vue.js"></script>
<script>
  //向子組件傳遞數(shù)據(jù)
  //省略extend方法,vue內(nèi)部調(diào)用
  Vue.component('my-component', {
    //模板里不支持駝峰的屬性寫(xiě)法,需要轉(zhuǎn)換為‘-'連接的屬性寫(xiě)法
    data:function(){
      return{
        parentMsg: '雨歇微涼'
      }
    },
    template: '<div>'
        +'<input v-model="parentMsg">'
        +'<br>'
        +'<child-component :my-message="parentMsg"></child-component>'
        +'</div>',
    components: {
      'child-component': {
        props: ['myMessage'],
        template: '<div>{{myMessage}}</div>'
      }
    }
  });
  // 創(chuàng)建根實(shí)例1
  new Vue({
    el: '#example'
  });
</script>

有什么疑惑的,也可以去查官網(wǎng)的文檔,prop傳值,這里也可以直接拷去試,如果你有什么更好的簡(jiǎn)介,還希望能夠拿出來(lái)分享。

第二種

<div id="example">
  <my-component></my-component>
</div>
<script src="../node_modules/vue/dist/vue.js"></script>
<script>
  //向子組件傳遞數(shù)據(jù)
  //省略extend方法,vue內(nèi)部調(diào)用
  Vue.component('my-component', {
    data:function(){
      return {
        name:'xiaoming',
        age:20
      }
    },
    //模板里不支持駝峰的屬性寫(xiě)法,需要轉(zhuǎn)換為‘-'連接的屬性寫(xiě)法
    template: '<div >{{name}}Parent</div><child1-component v-bind:msg-name="name"></child1-component>',
    components: {
      'child1-component': {
        // 聲明 props
        props: ['msgName'],
        template: '<div>A child-111111 component!{{msgName}}</div>'
      }
    }
  });
  // 創(chuàng)建根實(shí)例1
  new Vue({
    el: '#example'
  });
</script>

第三種

<div id="example">
  <my-component></my-component>
</div>
<script src="../node_modules/vue/dist/vue.js"></script>
<script>
  //向子組件傳遞數(shù)據(jù)
  //省略extend方法,vue內(nèi)部調(diào)用
  Vue.component('my-component', {
    data:function(){
      return {
        name:'xiaoming',
        age:20
      }
    },
    //模板里不支持駝峰的屬性寫(xiě)法,需要轉(zhuǎn)換為‘-'連接的屬性寫(xiě)法
    template: '<div >{{name}}Parent</div><child1-component some="1 + 1"></child1-component><child2-component :some="1 + 3"></child2-component>', 
    components: {
      'child1-component': {
        // 聲明 props
        props: ['some'],
        template: '<div>{{some}}</div>',
        ready:function(){
          console.log(this.some)
        }
      },
      'child2-component': {
        // 聲明 props
        props: ['some'],
        template: '<div>{{some}}</div>',
        ready:function(){
          console.log(this.some)
        }
      }
    }
  });
  // 創(chuàng)建根實(shí)例1
  new Vue({
    el: '#example'
  });
</script>

這個(gè)例子主要是說(shuō)明帶冒號(hào)和不帶冒號(hào)的區(qū)別,不帶冒號(hào)就是一個(gè)字符串死值,帶冒號(hào)會(huì)到父模版的data中去尋找值的具體內(nèi)容。

總結(jié)

以上所述是小編給大家介紹的Vue父子模版?zhèn)髦导敖M件傳值的三種方法,希望對(duì)大家有所幫助,如果大家有任何疑問(wèn)請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)億速云網(wǎng)站的支持!

向AI問(wèn)一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI