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

vue2.0中組件傳值的幾種方式總結(jié)

 更新時(shí)間:2022年12月07日 09:38:51   作者:鍵.  
這篇文章主要介紹了vue2.0中組件傳值的幾種方式總結(jié),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教

搭建好測(cè)試環(huán)境

app.vue

<template>
  <div id="app">
    <img alt="Vue logo" src="./assets/logo.png">
    <HelloWorld msg="Welcome to Your Vue.js App"/>
    
    <child></child>
  </div>
</template>

<script>
import HelloWorld from './components/HelloWorld.vue'
import Child from './components/Child.vue'

export default {
  name: 'App',
  components: {
    HelloWorld,
    Child
  }
}
</script>

<style>
#app {
  font-family: Avenir, Helvetica, Arial, sans-serif;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
  text-align: center;
  color: #2c3e50;
  margin-top: 60px;
}
</style>
Child.vue
````html
!<template>
  <div class="Child">
      <h1>這是子組件</h1>
  </div>
</template>

<script>
export default {
    name:'Child',
    data() {
        return {
            
        }
    },
}
</script>

<style>

</style>

1.方法一

父?jìng)髯?/h3>

父組件向子組件使用props傳值

<template>
  <div id="app">
    <img alt="Vue logo" src="./assets/logo.png">
    <HelloWorld msg="Welcome to Your Vue.js App"/>

    <child :sendParam="send"></child>
  </div>
</template>

<script>
import HelloWorld from './components/HelloWorld.vue'
import Child from './components/Child.vue'

export default {
  name: 'App',
  components: {
    HelloWorld,
    Child
  },
  data() {
    return {
      send:'父組件傳給子組件的值'
    }
  },
}
</script>
<template>
  <div class="Child">
      <h1>這是子組件</h1>
      <div>{{sendParam}}</div>
  </div>
</template>

<script>
export default {
    name:'Child',
    props:['sendParam'],
    data() {
        return {
            
        }
    },
}
</script>

這里的props除了寫(xiě)成數(shù)組還可以寫(xiě)成對(duì)象的方式:

<script>
export default {
    name:'Child',
    data() {
        return {
            
        }
    },
        //方法一:用數(shù)組獲取值
    //    , props:['sendParam'],

    //方法二:用對(duì)象獲取值
    props:{
        sendParam:String,
    }
}
</script>

所以說(shuō)在父組件給子組件傳值的時(shí)候是可以傳對(duì)象,布爾值,函數(shù)等,在子組件接收值的時(shí)候也可以做相應(yīng)的限制或設(shè)置默認(rèn)值。以對(duì)象接收時(shí)有type,default,require等參數(shù)可以設(shè)置,詳細(xì)的內(nèi)容可參考官網(wǎng)的文檔。

Vue.component('my-component', {
  props: {
    // 基礎(chǔ)的類型檢查 (`null` 和 `undefined` 會(huì)通過(guò)任何類型驗(yàn)證)
    propA: Number,
    // 多個(gè)可能的類型
    propB: [String, Number],
    // 必填的字符串
    propC: {
      type: String,
      required: true
    },
    // 帶有默認(rèn)值的數(shù)字
    propD: {
      type: Number,
      default: 100
    },
    // 帶有默認(rèn)值的對(duì)象
    propE: {
      type: Object,
      // 對(duì)象或數(shù)組默認(rèn)值必須從一個(gè)工廠函數(shù)獲取
      default: function () {
        return { message: 'hello' }
      }
    },
    // 自定義驗(yàn)證函數(shù)
    propF: {
      validator: function (value) {
        // 這個(gè)值必須匹配下列字符串中的一個(gè)
        return ['success', 'warning', 'danger'].indexOf(value) !== -1
      }
    }
  }
})

子傳父

子組件向父組件傳值需要使用自定義事件

<template>
  <div class="Child">
      <h1>這是子組件</h1>
      <div>{{sendParam}}</div>
      <button @click="run">子傳父</button>
  </div>
</template>

<script>
export default {
    name:'Child',
    data() {
        return {
            childata:'這是子傳父的值'
        }
    },
    props:['sendParam'],
    methods: {
        run(){
            this.$emit('event',this.childata)
        }
    },
}
</script>
<template>
  <div id="app">
    <img alt="Vue logo" src="./assets/logo.png">
    <HelloWorld msg="Welcome to Your Vue.js App"/>

    <child :sendParam="send" @event="reviceChild"></child>

    <div>{{revice}}</div>
  </div>
</template>

<script>
import HelloWorld from './components/HelloWorld.vue'
import Child from './components/Child.vue'

export default {
  name: 'App',
  components: {
    HelloWorld,
    Child
  },
  data() {
    return {
      send:'父組件傳給子組件的值',
      revice:''
    }
  },
  methods: {
    reviceChild(val){
      this.revice=val
    }
  },
}
</script>

子組件中的事件不一定要用click點(diǎn)擊事件,還可以是各種可觸發(fā)的事件,比如mouseover,dbclick等。

2.方法二

在父組件中加上一個(gè)ref屬性并打印其結(jié)果

<child :sendParam="send" @event="reviceChild" ref="child"></child>
console.log(this.$refs.child);

在結(jié)果中我們發(fā)現(xiàn)了很多子組件Child中有的方法,數(shù)據(jù)。

結(jié)果表明,我們打印的this.$refs.child就是整個(gè)子組件,也就是說(shuō),我們可以在這里直接拿到子組件的值。父組件拿子組件的值同理。

父?jìng)髯?/h3>
<button @click="getParent">獲取父組件的值</button>
getParent(){
   console.log(this.$parent.send)
}

子傳父

<child :sendParam="send" @event="reviceChild" ref="child"></child>

<button @click="go">獲取ref</button>
go(){
  console.log(this.$refs.child.childata);
}

奇怪的傳值

父組件中添加:that="this"

<child :sendParam="send" @event="reviceChild" ref="child" :that="this"></child>

子組件中props接收

props:{
     sendParam:{
         type:String,
         default:'111'
     },
     that:{}
 },

在生命周期中輸出

mounted() {
    console.log(this.that)
},

這里也可以拿到整個(gè)父組件

mounted() {
    console.log(this.that.send)//子組件拿到父組件的值
},

3.方法三

vue提供了provide,inject來(lái)實(shí)現(xiàn)如下場(chǎng)景的組件傳值,父組件向子組件跨級(jí)傳值

父組件:

export default {
	  name: 'App',
	  components: {
	    HelloWorld,
	    Child
	  },
	  provide:{
	    psend:'123456'
	  },
}

子組件:

export default {
    name:'Child',
    data() {
        return {
        }
    },
    inject:['psend'],
    mounted() {
       console.log(this.psend);
   },
 }

4.兄弟組件之間傳值

這里vue提供了vuex來(lái)解決該需求,這里可以查看我之前寫(xiě)的一篇筆記

vuex使用指南

總結(jié)

以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。

相關(guān)文章

最新評(píng)論

上蔡县| 洛扎县| 兰西县| 社会| 南丰县| 永福县| 德钦县| 濮阳市| 贺兰县| 芦溪县| 福鼎市| 娱乐| 拜泉县| 盘山县| 龙井市| 沧州市| 集安市| 乌兰县| 固原市| 永昌县| 泸溪县| 铁岭市| 若羌县| 武清区| 彝良县| 洪雅县| 闸北区| 宁波市| 临潭县| 辉县市| 连城县| 会泽县| 阿巴嘎旗| 密云县| 上林县| 延边| 遂平县| 疏勒县| 枞阳县| 江永县| 洛南县|