uniapp組件傳值的方法(父傳子,子傳父,對象傳值)實(shí)戰(zhàn)案例
前言
最近看到uniapp組件傳值的方法,這里記錄一下,學(xué)過vue的應(yīng)該都覺得很簡單,傳值的方法基本與vue的寫法差不多
父組件給子組件傳值
創(chuàng)建子組件comp.vue,然后在index.vue父頁面使用該組件:
<template> <view class="content"> <image class="logo" src="/static/logo.png"></image> <comp :name="name" @getMsg="openMsg"></comp> </view> </template> <script> import comp from '../../components/comp.vue' import phone from '../../components/phone.vue' import phoneItem from '../../components/phoneItem.vue' export default { components: { comp, }, data(){ name: 'parent', } } </script>
可以看到在上面的index頁面中有一個(gè)數(shù)據(jù)為name,我們下面使用props將父組件的name值傳給子組件comp.vue。首先在父組件中使用子組件的上面進(jìn)行綁定傳參:
<comp :name="name" ></comp>
然后在子組件comp.vue中使用props接收父組件傳過來的值:
然后設(shè)置一個(gè)點(diǎn)擊事件打印拿到的name的值
<script> export default { // 接收父傳給子的參數(shù) props:{ name: String }, methods: { sendMsg() { console.log(this.name) } } } </script>
打印的值:
子組件給父組件傳值
在子組件中使用this.$emit對父組件進(jìn)行傳值
在comp.vue中:
methods: { sendMsg() { //子傳父 this.$emit('getMsg', "我是子,"+this.name); } }
在index.vue中:
定義openMsg方法綁定給@getMsg
<comp :name="name" @getMsg="openMsg"></comp>
然后寫openMsg方法:打印傳過來的值
methods: {<!--{cke_protected}{C}%3C!%2D%2D%20%2D%2D%3E-->openMsg(msg) {<!--{cke_protected}{C}%3C!%2D%2D%20%2D%2D%3E-->console.log(msg)}}
結(jié)果如下:這樣子組件編寫的值傳到了父組件中打印了出來
父組件給父組件傳對象值
父給子傳值還是使用props方法,只是傳對象的話寫法有些區(qū)別
在父組件中:
<phoneItem v-for="(item,index) in songList" :item="item" :key="index"/>
現(xiàn)需要將song中songList的值傳過去
<script> import phoneItem from '../../components/phoneItem.vue' export default { components: { phoneItem }, data() { return { title: 'Hello', name: 'parent', song: { img: 'http://gw.alicdn.com/bao/uploaded/i3/1917047079/O1CN01VlEDD522AEJzpw3A5_!!2-item_pic.png_360x10000.jpg', title: 'Apple/蘋果 iPhone 11 Pro', price: '8699.00', marketPrice: '¥8699.00', }, songList: [ { img: 'http://gw.alicdn.com/bao/uploaded/i3/1917047079/O1CN01VlEDD522AEJzpw3A5_!!2-item_pic.png_360x10000.jpg', title: 'Apple/蘋果 iPhone 11 Pro', price: '8699.00', marketPrice: '¥8699.00', }, { img: 'http://gw.alicdn.com/bao/uploaded/i3/1917047079/O1CN01VlEDD522AEJzpw3A5_!!2-item_pic.png_360x10000.jpg', title: 'Apple/蘋果 iPhone 11 Pro', price: '8699.00', marketPrice: '¥8699.00', }, { img: 'http://gw.alicdn.com/bao/uploaded/i3/1917047079/O1CN01VlEDD522AEJzpw3A5_!!2-item_pic.png_360x10000.jpg', title: 'Apple/蘋果 iPhone 11 Pro', price: '8699.00', marketPrice: '¥8699.00', }, ] } } } </script>
子組件中進(jìn)行接收:
<template> <view class="phone"> <image :src="item.img" mode="widthFix"></image> <view > {{item.title}} </view> <view class=""> <view class="price"> ¥{{item.price}} </view> <view class="market-price"> {{item.marketPrice}} </view> </view> </view> </template> <script> export default { //父傳給子的參數(shù) props: { item: { type: Object, default() { return {} } } } } </script>
運(yùn)行結(jié)果:
這樣就能把對象的值傳過來并且渲染:
總結(jié)
到此這篇關(guān)于uniapp組件傳值的方法(父傳子,子傳父,對象傳值)的文章就介紹到這了,更多相關(guān)uniapp組件傳值內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Bootstrap中datetimepicker使用小結(jié)
這篇文章主要為大家詳細(xì)介紹了Bootstrap中datetimepicker的使用方法,一款功能強(qiáng)大的日期選擇控件,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-12-12基于JavaScript創(chuàng)建動態(tài)Dom
這篇文章主要介紹了基于JavaScript創(chuàng)建動態(tài)Dom的相關(guān)資料,需要的朋友可以參考下2015-12-12用Javascript實(shí)現(xiàn)發(fā)送短信驗(yàn)證碼間隔功能
這篇文章主要介紹了用Javascript實(shí)現(xiàn)發(fā)送短信驗(yàn)證碼間隔功能,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-02-02使用ImageMagick進(jìn)行圖片縮放、合成與裁剪(js+python)
由于需要在服務(wù)器端處理,使用就研究使用imagemagick來進(jìn)行。同時(shí)準(zhǔn)備封裝了一個(gè)Node.js和Python的方法,主要還是講一下然后使用imagemagick來對圖片進(jìn)行縮放、合成后進(jìn)行裁剪吧2013-09-09Javascript創(chuàng)建自定義對象 創(chuàng)建Object實(shí)例添加屬性和方法
創(chuàng)建自定義對象的最簡單的方式就是創(chuàng)建一個(gè)Object實(shí)例,然后再為它添加屬性和方法2012-06-06JavaScript中的Reflect對象詳解(ES6新特性)
這篇文章主要介紹了JavaScript中的Reflect對象(ES6新特性)的相關(guān)資料,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友可以參考下2016-07-07