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

vue組件中的數(shù)據(jù)傳遞方法

 更新時間:2018年05月14日 14:35:39   作者:晴_eeca  
這篇文章主要介紹了vue組件中的數(shù)據(jù)傳遞方法,非常不錯,具有一定的參考借鑒價值,需要的朋友參考下吧

Vue 的組件作用域都是孤立的,不允許在子組件的模板內直接引用父組件的數(shù)據(jù)。必須使用特定的方法才能實現(xiàn)組件之間的數(shù)據(jù)傳遞。組件之間傳遞數(shù)據(jù)大致分為三種情況:

父組件向子組件傳遞數(shù)據(jù),通過 props 傳遞數(shù)據(jù)。

子組件向父組件傳遞數(shù)據(jù),通過 events 傳遞數(shù)據(jù)。

兩個同級組件之間傳遞數(shù)據(jù),通過 event bus 傳遞數(shù)據(jù)。

一、父組件向子組件傳遞數(shù)據(jù)

子組件部分:

<template>
  <div class="child">
    {{ msg }}
  </div>
</template>
<script>
export default {
 name: 'child',
 data(){
  return {
  
  }
 },
 props: ['msg']
</script>

在child.vue中,msg實在data中定義的變量,使用props:['msg']從父組件中獲取msg的值

父組件部分:

<template>
  <div class="child">
    child
    <child :msg="message"></child>
  </div>
</template>
<script>
import child from './child.vue'
export default {
 name: 'parent',
 components: { child },
 data () {
  return {
   message: 'hello vue'
  }
 }
}
</script>

在調用組件的時候,使用v-bind將msg的值綁定為parent.vue中定義的變量message,這樣就能將parent.vue中的message的值傳給child.vue了。

單項數(shù)據(jù)流

當父組件的 message 發(fā)生改變,子組件也會自動地更新視圖。但是在子組件中,我們不要去修改 prop。如果你必須要修改到這些數(shù)據(jù),你可以使用以下方法:

方法一:把 prop 賦值給一個局部變量,然后需要修改的話就修改這個局部變量,而不影響 prop

export default {
  data(){
    return {
      newMessage: null
    } 
  },
  props: ['message'],
  created(){
    this.newMessage = this.message;
  }
}

方法二:在計算屬性中對 prop 進行處理

export default {
  props: ['message'],
  computed: {
    newMessage(){
      return this.message + ' 哈哈哈';
    }
  }
}

二、子組件向父組件傳遞數(shù)據(jù)

子組件主要通過實踐傳遞數(shù)據(jù)給父組件的

子組件部分:

<template>
  <div class="child">
   <span>用戶名:</span>
   <input v-model="username" @change="sendUser" />
  </div>
</template>

子組件的html中,當input中的值發(fā)生改變時,將username傳遞給parent.vue。

首先聲明了一個sendUser方法,用change事件來調用sendUser。

<script>
 export default {
  name: 'parend',
  data () {
   return {
     username: ''
   }
  },
  methods: {
   sendUser () {
    this.$emit('changeName', this.username)
   }
  }
}
</script>

在sendUser中,使用$emit來遍歷changeName事件,并返回this.name,其中changeName是一個自定義的事件,功能類似于一個中轉,this.name將通過這個事件傳遞給父組件。

父組件部分:

<template>
  <div class="parent">
    <child @changeName="getUser"></child>
    <p>用戶名:{{user}}</p>
  </div>
</template>

在父組件中聲明了一個getUser方法,用changeName事件調用getUser方法,獲取從子組件傳遞過來的參數(shù)username

<script>
import child from './child.vue'
export default {
 name: 'parent',
 components: { child },
 data () {
  return {
   user: ''
  }
 },
 methods: {
  getUser(data) {
   this.user = data
  }
 }
}
</script>

getUser方法中的參數(shù)msg就是從子組件中傳遞過來的參數(shù)uesrname。

三、同級組件間的數(shù)據(jù)傳遞

有時候兩個組件也需要通信(非父子關系)。當然Vue2.0提供了Vuex,但在簡單的場景下,可以使用一個空的Vue實例作為中央事件總線。

<template>
  <div id="app">
    <c1></c1>  //組件1
    <c2></c2> //組件2
  </div>
</template>

組件c1中:

<template>
  <div class="c1">
    page
    <button @click="changeMsg">click</button>
  </div>
</template>
<script>
var Bus = new Vue(); //為了方便將Bus(空vue)定義在一個組件中,在實際的運用中一般會新建一Bus.js
export default {
 name: 'c1',
 data () {
  return {
   message: 'hi'
  }
 },
 methods: {
  changeMsg () {  //點擊按鈕,將this.message傳遞給c2
   bus.$emit('sendMsg', this.message)
  }
 }
}
</script>

組件c2中:

<template>
  <div class="c2">
    {{msg}}
  </div>
</template>

<script>
var Bus = new Vue();

export default {
 name: 'c2',
 data () {
  return {
   msg: ''
  }
 },
 created () {
  bus.$on('sendMsg',(data)=>{  //data即為c1組件中的message
   this.msg = data
  })
 }
}
</script>

在實際運用中,一般將bus抽離出來:

//Bus.js
import Vue from 'vue'
const Bus = new Vue()
expore default Bus

組件調用時引用(import Bus from './Bus.js')

但這種引入方式,經(jīng)過webpack打包后可能會出現(xiàn)Bus局部作用域的情況,即引用的是兩個不同的Bus,導致不能正常通信
實際運用:

將Bus注入到Vue根對象中

import Vue from 'vue'
const Bus = new Vue()
var app= new Vue({
  el:'#app',
   data:{
    Bus
  }  
})

在子組件中通過this.$root.Bus.$on(),this.$root.Bus.$emit()來調用

總結

以上所述是小編給大家介紹的vue組件中的數(shù)據(jù)傳遞方法,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復大家的。在此也非常感謝大家對腳本之家網(wǎng)站的支持!

相關文章

  • vue生命周期和react生命周期對比【推薦】

    vue生命周期和react生命周期對比【推薦】

    本文通過實例代碼給大家介紹了vue生命周期和react生命周期對比 ,非常不錯,具有一定的參考借鑒價值,需要的朋友可以參考下
    2018-09-09
  • 詳解vue項目首頁加載速度優(yōu)化

    詳解vue項目首頁加載速度優(yōu)化

    這篇文章主要介紹了詳解vue項目首頁加載速度優(yōu)化,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2017-10-10
  • vue3中的reactive、readonly和shallowReactive使用詳解

    vue3中的reactive、readonly和shallowReactive使用詳解

    在 Vue3 中,可以使用 shallowReactive 函數(shù)創(chuàng)建一個淺層響應式對象,它接收一個普通對象作為參數(shù),返回一個淺層響應式代理對象,本文給大家介紹vue3中的reactive、readonly和shallowReactive使用,感興趣的朋友跟隨小編一起看看吧
    2024-04-04
  • vue中created、watch和computed的執(zhí)行順序詳解

    vue中created、watch和computed的執(zhí)行順序詳解

    由于vue的雙向數(shù)據(jù)綁定,自動更新數(shù)據(jù)的機制,在數(shù)據(jù)變化后,對此數(shù)據(jù)依賴?的所有數(shù)據(jù),watch事件都會被更新、觸發(fā),下面這篇文章主要給大家介紹了關于vue中created、watch和computed的執(zhí)行順序,需要的朋友可以參考下
    2022-11-11
  • vue項目中引入Sass實例方法

    vue項目中引入Sass實例方法

    在本文里小編給大家整理的是關于vue項目中引入Sass的相關知識點內容,有需要的朋友們可以測試參考下。
    2019-08-08
  • Vue中axios攔截器如何單獨配置token

    Vue中axios攔截器如何單獨配置token

    這篇文章主要介紹了Vue axios攔截器如何單獨配置token及vue axios攔截器的使用,需要的朋友可以參考下
    2019-12-12
  • 對vue中v-on綁定自定事件的實例講解

    對vue中v-on綁定自定事件的實例講解

    今天小編就為大家分享一篇對vue中v-on綁定自定事件的實例講解,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2018-09-09
  • vue-cli的eslint相關用法

    vue-cli的eslint相關用法

    本篇文章主要介紹了vue-cli的eslint相關用法,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2017-09-09
  • 使用vue制作探探滑動堆疊組件的實例代碼

    使用vue制作探探滑動堆疊組件的實例代碼

    探探的堆疊滑動組件起到了關鍵的作用,下面就來看看如何用vue寫一個探探的堆疊組件,感興趣的朋友一起看看吧
    2018-03-03
  • Vue.js 中的 v-show 指令及用法詳解

    Vue.js 中的 v-show 指令及用法詳解

    v-show 指令通過改變元素的 css 屬性(display)來決定元素是顯示還是隱藏。這篇文章主要介紹了Vue.js 中的 v-show 指令及用法詳解,需要的朋友可以參考下
    2018-11-11

最新評論

永福县| 宣威市| 凌源市| 金华市| 泰兴市| 泾源县| 陆丰市| 靖州| 呼伦贝尔市| 台北市| 威海市| 嫩江县| 安康市| 泉州市| 左贡县| 太谷县| 西盟| 沛县| 林州市| 福贡县| 宁化县| 江北区| 吉木萨尔县| 威远县| 精河县| 论坛| 陈巴尔虎旗| 阿拉善右旗| 德钦县| 黑龙江省| 衡阳市| 华安县| 荣昌县| 上林县| 砀山县| 海兴县| 呈贡县| 金秀| 长宁县| 建宁县| 信丰县|