格式
<text onclick="{{press}}"></text>
可以簡寫為:
<text @click="{{press}}"></text>
事件回調(diào)支持的寫法(其中{{}}可以省略):
<text @click="press"></text>
"fn":fn 為事件回調(diào)函數(shù)名(在?<script>
?中有對應(yīng)的函數(shù)實(shí)現(xiàn)),上例中 press 為事件回調(diào)函數(shù)。
傳參
常量
<template>
<div class="demo-page">
<text for="{{list}}" key="{{$idx}}" onclick="handle($idx,$item)"> {{$item}}</text>
</div>
</template>
<script>
export default {
private: {
list:[1,2,3,4,5]
},
handle(idx,item,$evt){
console.log(idx)
console.log(item)
console.log($evt)
}
}
</script>
示例圖:

變量
?<script>
?中定義的頁面的數(shù)據(jù)變量(前面不用寫 this)
<template>
<div class="demo-page">
<text for="{{list}}" key="{{$idx}}" onclick="handle(total,$item)"> {{$item}}</text>
</div>
</template>
<script>
export default {
private: {
list:[1,2,3,4,5]
total:0,
},
handle(total,num,$evt){
console.log(total)
console.log(num)
console.log($evt)
}
}
</script>
示例圖:

注意:
回調(diào)函數(shù)被調(diào)用時(shí),會在參數(shù)列表末尾自動添加一個(gè) evt 參數(shù),通過 evt 參數(shù)訪問回調(diào)事件相關(guān)上下文數(shù)據(jù).