wepy.$instance.globalData // 獲取全局變量的方法
setData()不能直接操作數(shù)據(jù)
wx:if 是惰性渲染
wx:for="{{data}}";
wx:key有兩種形式:
保留關鍵字:wx:key="*this"
字符串:wx:key="unique"
渲染層計算:{{a+b}} + {{c}} + {};
字符串拼接:{{'hello' + kobe}}
定義公共模板import 和 include:
<tamplate name='myTamplate'>
<view>{{index}}:{{name}}</view>
</tamplate>
.
.
.
<tamplate is=''myTamplate"></tamplate> //引用
import有作用域概念,不能多重引用
include可以多重引用
bindTap不阻止事件冒泡;catchTab阻止事件冒泡
普通組件引用:(必須在.wpy文件里面)
<tamplate>
<child></child>
</tamplate>
<script>
import wepy from 'wepy'
import Child from '../../components/child';
export default class Index extends wepy.componten{
compontens = {
child:Child //注意組建id問題
}
}
</script>
- 組件渲染:for={{'list'}} 不是使用 wx:for 且使用wepy輔助標簽
<repeat>
<child :item='item'></child>
</repeat>
- 組件傳值:靜態(tài):
<child title='mttitle'></child>
// child.wpy
props={ title:String }
onLoad(){console.log(this.title)}
- 動態(tài):
<child :title="parentTitle" :syncTitle.sync="parentTitle" :twoWayTitle="parentTitle"></child>
data = { parentTitle: 'p-title' };
// child.wpy
props = {
// 靜態(tài)傳值
title: String,
// 父向子單向動態(tài)傳值
syncTitle: {
type: String,
default: 'null'
},
twoWayTitle: {
type: Number,
default: 50,
twoWay: true // 是否子組件回傳數(shù)據(jù)給父組件
}
};
可以通過使用.sync修飾符來達到父組件數(shù)據(jù)綁定至子組件的效果,也可以通過設置子組件props的twoWay: true來達到子組件數(shù)據(jù)綁定至父組件的效果。那如果即使用.sync修飾符,同時子組件props中添加的twoWay: true時,就可以實現(xiàn)數(shù)據(jù)的雙向綁定了。
- 計算屬性:
computed = {
// 是一個有返回值的函數(shù),在<tamplate> 中使用{{name}}來綁定數(shù)據(jù)
name (){
return this.name + 'Byant'
}
}
- watcher監(jiān)聽器:
data={num:1}
watch= {
num (newvalue,oldvalue) {
console.log(`num value: ${oldValue} -> ${newValue}`)
}
}
onLoad () {
setInterval(() => {
this.num++;
this.$apply();
},1000)
}
- 混合:mixins
import wepy from 'wepy';
export default class TestMixin extends wepy.mixin{
data = {
foo: 'foo defined by page',
bar: 'bar defined by testMix'
};
methods: {
tap () {
console.log('mix tap');
}
}
}
import 引用文件
// pages/index.wpy
import wepy from 'wepy';
import TestMixin from './mixins/test';
export default class Index extends wepy.page{
data = { foo: 'foo defined by index' };
mixins = [TestMixin ];
onShow() {
console.log(this.foo); // foo defined by index.
console.log(this.bar); // foo defined by testMix.
}
}
同一事件會先調(diào)用index事件 后調(diào)用mixins
.....未完待續(xù)!