wepy組件間傳值

普通組件引用函數

當頁面須要引入組件,或組件須要引入子組件時,必須在.wpy文件的<script>部分先import組件文件,而後在components對象中給組件聲明惟一的組件ID;接着在<template>模板部分中,添加以組件ID進行命名的自定義標籤,以插入組件。this

<template>
<!-- 以`<script>`腳本部分中所聲明的組件ID爲名命名自定義標籤,從而在`<template>`模板部分中插入組件 -->
<child></child>
</template>雙向綁定

<script>
import wepy from 'wepy';
import Child from '../components/child'; // 引入組件文件component

export default class Index extends wepy.component {

components = {
child: Child // 聲明組件,分配組件id爲child
};
}
</script>對象

組件ID事件

WePY中的組件都是靜態組件,是以組件ID做爲惟一標識的,每個ID都對應一個組件實例。當頁面引入兩個相同ID的組件時,這兩個組件共用同一個實例與數據,當其中一個組件數據變化時,另一個也會一塊兒變化。
如需避免這個問題,則須要分配多個組件ID和實例。代碼以下:ip

<template>
<view class="child1">
<child></child>
</view>同步

<view class="child2">
<anotherchild></anotherchild>
</view>
</template>it


<script>
import wepy from 'wepy';
import Child from '../components/child';console

export default class Index extends wepy.component {
components = {
//爲兩個相同組件的不一樣實例分配不一樣的組件ID,從而避免數據同步變化的問題
child: Child,
anotherchild: Child
};
}
</script>

組件的循環渲染
當須要循環渲染WePY組件時,須使用WePY定義的輔助標籤<repeat>,示例以下:
<template>
<!-- 注意,使用for屬性,而不是使用wx:for屬性 -->
<repeat for="{{list}}" key="index" index="index" item="item">
<!-- 插入<script>腳本部分所聲明的child組件,同時傳入item -->
<child :item="item"></child>
</repeat>
</template>

<script>
import wepy from 'wepy';
import Child from '../components/child'; // 引入child組件文件

export default class Index extends wepy.component {
components = {
child: Child
}

data = {
list: [{id: 1, title: 'title1'}, {id: 2, title: 'title2'}]
}
}
</script>

組件的computed屬性
computed計算屬性,是一個有返回值的函數,可直接被看成綁定數據來使用。相似於data屬性。示例以下:
data = {
aaa: 1
}

// 計算屬性aPlus,在腳本中可經過this.aPlus來引用,在模板中可經過{{ aPlus }}來插值
computed = {
aPlus () {
return this.aaa + 1
}
}

watcher監聽器
經過監聽器watcher,可以監聽到任何屬性的更新。監聽器在watch對象中聲明,類型爲函數,函數名與須要被監聽的data對象中的屬性同名,每當被監聽的屬性改變一次,監聽器函數就會被自動調用執行一次。

監聽器適用於當屬性改變時須要進行某些額外處理的情形。

示例:
data = {
num: 1
}

// 監聽器中的監聽函數名,必須與其對應的屬性名同樣
// 參數newValue爲屬性改變後的新值,oldValue爲改變前的舊值
watch = {
num (newValue, oldValue) {
console.log(`num value: ${oldValue} -> ${newValue}`)
}
}

props傳值
在WePY中屬於父子組件之間傳值的一種機制,包括靜態傳值與動態傳值。
一、靜態傳值
父組件向子組件傳遞常量數據,只能傳遞String。
作法:在父組件template模板部分的組件標籤中,使用子組件props對象中所聲明的屬性名,做爲其屬性名來接收父組件傳遞的值。
示例:
//* 父組件 template模板部分
<childCom title="mytitle"></childCom >

//*子組件 childCom.wpy中
props = {
title: String
};

onLoad () {
console.log(this.title); // 輸出 「mytitle」
}

二、動態傳值
指父組件向子組件傳遞動態數據內容,父子組件數據徹底獨立互不干擾。

-能夠經過.sync修飾符,來達到父組件數據綁定至子組件的效果;
-也能夠經過設置子組件props的twoWay: true,來達到子組件數據綁定至父組件的效果;
-若是既使用.sync修飾符,同時子組件props中添加的twoWay: true時,就能夠實現數據的雙向綁定了。
-在父組件template模板部分所插入的子組件標籤中,使用:prop屬性,來進行動態傳值。

// parent.wpy

<child :title="parentTitle" :syncTitle.sync="parentTitle" :twoWayTitle="parentTitle"></child>

data = {
parentTitle: 'p-title'
};


// child.wpy

props = {
// 靜態傳值
title: String,

// 父向子單向動態傳值
syncTitle: {
type: String,
default: 'null'
},

twoWayTitle: {
type: Number,
default: 'nothing',
twoWay: true
}
};

組件通訊與交互
wepy.component基類提供$broadcast、$emit、$invoke三個方法,用於組件之間的通訊和交互。有關的事件處理函數,須要寫在組件和頁面的events對象中聲明。

$broadcast
事件是由父組件發起,全部子組件都會收到此廣播事件,除非事件被手動取消。


$emit
由某個子組件發起,其全部祖先組件會依次接收到$emit事件。

示例
this.$emit('some-event', 1, 2, 3, 4);
import wepy from 'wepy'

export default class Com extends wepy.component {
components = {};

data = {};

// 聲明
events = {
'some-event': (p1, p2, p3, $event) => {
console.log(`${this.$name} receive ${$event.name} from ${$event.source.$name}`);
}
};
// Other properties
}


$invoke
是一個頁面或組件,直接調用另外一個組件的方法。經過傳入組件路徑找到相應的組件,而後再調用其方法。

好比:
-想在頁面Page_Index中,調用組件ComA的某個方法:
this.$invoke('ComA', 'someMethod', 'someArgs');
-想在組件ComA中,調用組件ComG的某個方法:
this.$invoke('./../ComB/ComG', 'someMethod', 'someArgs');
組件自定義本身的事件處理函數
使用.user修飾符,爲自定義組件 綁定事件。

如:@customEvent.user="myFn"
其中,@表示事件修飾符,customEvent 表示事件名稱,.user表示事件後綴。

示例:
// page.wpy

<template>
<child @childFn.user="parentFn"></child>
</template>

<script>
import wepy from 'wepy'
import Child from '../components/child'

export default class Index extends wepy.page {
components = {
child: Child
}

methods = {
parentFn (num, evt) {
console.log('parent received emit event, number is: ' + num)
}
}
}
</script>

// child.wpy

<template>
<view @tap="tap">Click me</view>
</template>

<script>
import wepy from 'wepy'

export default class Child extends wepy.component {
methods = {
tap () {
console.log('child is clicked')
this.$emit('childFn', 100)
}
}
}
</script>

slot 組件內容分發插槽

做者:Arthur凌連接:https://www.jianshu.com/p/e30436ba773f來源:簡書簡書著做權歸做者全部,任何形式的轉載都請聯繫做者得到受權並註明出處。

相關文章
相關標籤/搜索