VUE傳參及父子組件之間的通信

一、Vue中用this.$router傳遞參數(shù)與取值

  方式一:
    傳遞參數(shù)  -- this.$router.push({path: ' 路由 ', query: {key: value}})
    參數(shù)取值  -- this.$route.query.key
    this.$router.push({path:'test',query:{'id':'123456'})
    this.$route.query.id
  方式二:
    傳遞參數(shù)  -- this.$router.push({name: ' 路由的name ', params: {key: value}})
    參數(shù)取值  -- this.$route.params.key
   this.$router.push({name:'test',params:{'id':'123456'})
    this.$route.params.id

二、vue之父子組件間通信實(shí)例講解
1.props
在說如何實(shí)現(xiàn)通信之前,我們先來建兩個組件father.vue和child.vue作為示例的基礎(chǔ)。

  //父組件
<template>
 <div>
  <h1>我是父組件!</h1>
  <child></child>
 </div>
</template>
<script>
    import Child from '../components/child.vue'
      export default {
       components: {Child},
    }
</script>
//子組件
<template>
 <h3>我是子組件!</h3>
</template>

子組件的props選項(xiàng)能夠接收來自父組件數(shù)據(jù)。沒錯,僅僅只能接收,props是單向綁定的,即只能父組件向子組件傳遞,不能反向。而傳遞的方式也分為兩種:
1.1)靜態(tài)傳遞
子組件通過props選項(xiàng)來聲明一個自定義的屬性,然后父組件就可以在嵌套標(biāo)簽的時候,通過這個屬性往子組件傳遞數(shù)據(jù)了。

    <!-- 父組件 -->
    <template>
     <div>
      <h1>我是父組件!</h1>
      <child message="我是子組件一!"></child> //通過自定義屬性傳遞數(shù)據(jù)
     </div>
    </template>
    <script>
    import Child from '../components/child.vue'
    export default {
     components: {Child},
    }
    </script>
    <!-- 子組件 -->

    <template>
   <h3>{{message}}</h3>
    </template>
    <script>
     export default {
      props: ['message']  //聲明一個自定義的屬性
     }
    </script>

1.2)動態(tài)傳遞
我們已經(jīng)知道了可以像上面那樣給 props 傳入一個靜態(tài)的值,但是我們更多的情況需要動態(tài)的數(shù)據(jù)。這時候就可以用 v-bind 來實(shí)現(xiàn)。通過v-bind綁定props的自定義的屬性,傳遞去過的就不是靜態(tài)的字符串了,它可以是一個表達(dá)式、布爾值、對象等等任何類型的值。

    <!-- 父組件 -->

    <template>
     <div>
      <h1>我是父組件!</h1>
      <child message="我是子組件一!"></child>

      <!-- 這是一個 JavaScript 表達(dá)式而不是一個字符串。-->
      <child v-bind:message="a+b"></child>

        <!-- 用一個變量進(jìn)行動態(tài)賦值。-->
      <child v-bind:message="msg"></child>
     </div>
    </template>
    <script>
    import Child from '../components/child.vue'
    export default {
     components: {Child},
      data() {
        return {
         a:'我是子組件二!',
         b:112233,
         msg: '我是子組件三!'+ Math.random()
        }
       }
      }
     </script>
      <!-- 子組件 -->

     <template>
       <h3>{{message}}</h3>
     </template>
    <script>
     export default {
      props: ['message']
     }
    </script>

2.通過$ref 實(shí)現(xiàn)通信

    對于ref官方的解釋是:
    ref 是被用來給元素或子組件注冊引用信息的。引用信息將會注冊在父組件的$ref對象上。
    我的理解:
    如果ref用在子組件上,指向的是組件實(shí)例,可以理解為對子組件的索引,
    通過$ref可能獲取到在子組件里定義的屬性和方法。
    如果ref在普通的 DOM 元素上使用,引用指向的就是 DOM 元素,
    通過$ref可能獲取到該DOM 的屬性集合,輕松訪問到DOM元素,作用與JQ選擇器類似。
    <!-- 父組件 -->

    <template>
     <div>
      <h1>我是父組件!</h1>
        <child ref="msg"></child>
     </div>
    </template>

    <script>
     import Child from '../components/child.vue'
     export default {
      components: {Child},
      mounted: function () {
        console.log( this.$refs.msg);
       this.$refs.msg.getMessage('我是子組件一!')
      }
     }
    </script>
    <!-- 子組件 -->

    <template>
     <h3>{{message}}</h3>
    </template>
    <script>
     export default {
      data(){
       return{
        message:''
       }
      },
      methods:{
       getMessage(m){
        this.message=m;
       }
      }
     }
    </script>

從上面的代碼我們可以發(fā)現(xiàn),通過ref=‘msg'可以將子組件child的實(shí)例指給ref,并且通過.msg.getMessage()調(diào)用到子組件的getMessage方法,將參數(shù)傳遞給子組件。下面是“ console.log( this.refs.msg);”打印出來的內(nèi)容,這可以讓大家更加了解,究竟通過ref我們獲取了什么:

image.png

這里再補(bǔ)充一點(diǎn)就是,prop和$ref之間的區(qū)別:

prop 著重于數(shù)據(jù)的傳遞,它并不能調(diào)用子組件里的屬性和方法。像創(chuàng)建文章組件時,自定義標(biāo)題和內(nèi)容這樣的使用場景,最適合使用prop。
$ref 著重于索引,主要用來調(diào)用子組件里的屬性和方法,其實(shí)并不擅長數(shù)據(jù)傳遞。而且ref用在dom元素的時候,能使到選擇器的作用,這個功能比作為索引更常有用到。

3.通過$emit 實(shí)現(xiàn)通信

上面兩種示例主要都是父組件向子組件通信,而通過$emit 實(shí)現(xiàn)子組件向父組件通信。

      vm.$emit( event, arg )

$emit 綁定一個自定義事件event,當(dāng)這個這個語句被執(zhí)行到的時候,就會將參數(shù)arg傳遞給父組件,父組件通過@event監(jiān)聽并接收參數(shù)

      <template>
       <div>
        <h1>{{title}}</h1>
        <child @getMessage="showMsg"></child>
       </div>
      </template>

      <script>
       import Child from '../components/child.vue'
       export default {
        components: {Child},
        data(){
         return{
          title:''
         }
        },
        methods:{
         showMsg(title){
          this.title=title;
         }
       }
       }
      </script>
      <template>
       <h3>我是子組件!</h3>
      </template>
      <script>
       export default {
        mounted: function () {
          this.$emit('getMessage', '我是父組件!')
        }
       }
      </script>
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

友情鏈接更多精彩內(nèi)容