鍍金池/ 問答/HTML5  HTML/ 關(guān)于vue和react的移動端的那些事件都是怎么解決的

關(guān)于vue和react的移動端的那些事件都是怎么解決的

有什么推薦的插件以及使用指南 各位大神推薦一下

回答
編輯回答
不將就

Vue
Vue是通過vue的自定義指令v-on:click來綁定事件的
v-on可以簡寫為@click
事件對應(yīng)的函數(shù)下載Vue實例里面的
methods里面
例:
<div id="box">

<button @click="clickMe">點我</button>

</div>
let vm = new Vue({

el:'#box',
methods:{
    clickMe(){
        alert(1)
    }
}

})

react
react是通過標(biāo)簽內(nèi)部的onClick來實現(xiàn)的,在事件后面直接寫要執(zhí)行的函數(shù)就行了,
但是一般來說都是吧事件的函數(shù)掛載到react的class里面,但是這樣會導(dǎo)致一個this指向的問題
所以要在constructor函數(shù)內(nèi)部進行重新綁定一下this
例:
<div id="app"></div>

export default class App entends Component{

constructor(){
    this.clickMe = this.clickMe.bind(this);
}
clickMe(){
    alert(1)
}
render(){
    return(
        <button onClick={clickMe}>點我</button>
    )
}

}

ReactDOM.render(

<App/>,
document.getElementById('app')

2018年4月11日 17:17