script增强特性
计算属性是一个纯函数,利用组合其它数据的方式返回一个新的数据,你可以像绑定普通数据一样在模板中绑定计算属性。
示例:
例如:
<template>
<view>
<view>{{question}}</view>
<view>{{answer}}</view>
</view>
</template>
<script>
import {createComponent} from '@mpxjs/core'
createComponent({
data: {
question: 'old',
answer: 'I cannot give you an answer until you ask a question!',
info: {
name: 'a'
},
arr: [{
age: 1
}]
},
watch: {
// 如果 `question` 发生改变,这个函数就会运行
question: function (newval, oldval) {
},
question: {
handler (newval, oldval) {
console.log(newval, ':', oldval) // test:old
this.answer = 'Waiting for you to stop typing...'
},
imeediate: true // 立即执行一次
// deep: true // 是否深度观察
// sync: true // 数据变化之后是否同步执行,默认是进行异步队列
},
'info.name' (val) {
// 支持路径表达式
console.log(val) // b
},
'arr[0].age' (val) {
// 支持路径表达式
console.log(val) // 100
},
'question, answer' (val, old) {
// 同时观察多个值, val为数组个数, question变化时
console.log(val) // ['test', 'I cannot give you an answer until you ask a question!']
}
},
attached () {
// 3s之后修改数据
setTimeout(() => {
}, 3000)
},
changeData() {
this.question = 'test'
this.info.name = 'b'
this.arr[0].age = 100
}
}
})
</script>
除了 watch 选项之外,您还可以使用命令式的 this.$watch
API。
示例:
<template xmlns="">
<view class="list">
<view wx:for="{{list}}">{{item}}</view>
</view>
</template>
<script>
import {createComponent} from '@mpxjs/core'
import mixin from './mixin'
createComponent({
mixins: [mixin],
ready () {
console.log('component ready:', this.list.tv)
}
})
</script>
输出结果为