页面和组件之间的传值
1.页面===>组件
例如:在/pages/index/index 中引用了组件中的card 组件,要如何将index中的数据传递到card中去呢?
-
引用组件(之前说过) -
传值
cardData="{{cardItem}}" ----就是传递到组件的数据,写在引用的组件标签中。 cardData :传递的数据变量名 cardItem :传递的数据值
/pages/index/index
<view>我是首页的内容</view>
<button class="card1" bindtap="showCard1">点击显示卡片1</button>
<card cardData="{{cardItem}}" wx:if="{{isShow}}"></card>
const app = getApp()
Page({
data: {
isShow: false,
cardItem: [{
cardTitle: '订单编号',
cardContent: 'D8392789279837'
},
{
cardTitle: '商品名称',
cardContent: '防晒霜'
},
{
cardTitle: '订单金额',
cardContent: '678.00'
},
{
cardTitle: '支付时间',
cardContent: '2022-04-26'
},
]
},
bindViewTap() {
wx.navigateTo({
url: '../logs/logs'
})
},
showCard1() {
console.log('点击事件:显示卡片');
this.setData({
isShow: true
})
},
onLoad() {
},
})
components/card/card.wxml
wx:for="{{cardData}}" :直接使用传递过来的数据(用循环)
<view class="view">
<view class="cardBox">
<view class="card-item" wx:for="{{cardData}}" wx:key="i" wx:for-item="item">
<view class="card-title">{{item.cardTitle}}</view>
<view class="card-con">{{item.cardContent}}</view>
</view>
</view>
</view>
Component({
properties: {
cardData: Object,
},
data: {
},
methods: {
lifetimes: {
ready: function () {
console.log(this.data.cardData);
},
}
}
})
2.组件===>页面
this.triggerEvent('customEventHandler', {
remark2: this.data.remark2
})
<dialog id='dialog' needData="{{needData}}" numTitle="起批量"
catchtouchmove="preventTouchMove" bind:customEventHandler="customEvent"></dialog>
customEvent: function (event) {
let receiveData = event.detail;
console.log(receiveData);
},
|