vue.js 如何动态的给一个按钮添加点击事件

  1. vue.js开发中,需要动态的给一个弹出框的确定按钮添加点击事件,来执行不同的方法,这个怎么做?


幕布斯6054654
浏览 7913回答 1
1回答

烙印99

Vue.js 2.x里面可以这样写,用@click.asyc绑定,代码如下:<div id="app" >&nbsp; <div>{{message}}</div>&nbsp; <button @click.sync="onClick">Click Me</button></div><script>&nbsp; &nbsp; var vue = new Vue({&nbsp; &nbsp; &nbsp; &nbsp; el: "#app",&nbsp; &nbsp; &nbsp; &nbsp; data:{&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; message:"Hello,world!"&nbsp; &nbsp; &nbsp; &nbsp; },&nbsp; &nbsp; &nbsp; &nbsp; methods: {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;onClick: function () {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; this.message = "You click me!";&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; this.onClick = function () {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;this.message = "Hello,i changed!";&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;}&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; });</script>
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript