Vue的自定義組件不能使用click方法的解決
先貼代碼
var myButton = Vue.extend({//設(shè)置標(biāo)簽 props: [’names’, ’item2’],//names為按鈕名,item2為數(shù)據(jù) template: ’<span><span v-for='obj in item2' v-if='obj.name==names' v-html='obj.code'></span></span>’ }) Vue.component(’my-button’, myButton);//注冊組件
這是上篇博客的自定義按鈕權(quán)限的代碼,然后調(diào)用代碼:
<my-button names='修改' v-bind:item2='btdata'></my-button>
當(dāng)你在這個(gè)標(biāo)簽上加@click事件的時(shí)候報(bào)錯(cuò),原因是因?yàn)闆]有加上native,官網(wǎng)對于native的解釋為:
.native - 監(jiān)聽組件根元素的原生事件。
正確的代碼為:
<my-button @click.native='alert1()' names='刪除' v-bind:item2='btdata'></my-button>
這樣就能成功在自定義標(biāo)簽上綁定事件了
補(bǔ)充知識:Vue中利用component切換組件
我就廢話不多說了,大家還是直接看代碼吧~
<!DOCTYPE html><html lang='en'><head> <meta charset='UTF-8'> <meta name='viewport' content='width=device-width, initial-scale=1.0'> <meta http-equiv='X-UA-Compatible' content='ie=edge'> <title>Document</title> <script src='http://www.gepszalag.com/bcjs/vue.js'></script></head><body> <div id='app'> <a href='http://www.gepszalag.com/bcjs/11206.html#' rel='external nofollow' rel='external nofollow' @click='componentName=’mycom1’'>組件1</a> <a href='http://www.gepszalag.com/bcjs/11206.html#' rel='external nofollow' rel='external nofollow' @click='componentName=’mycom2’'>組件2</a> <component :is='componentName'></component> </div></body><script> Vue.component(’mycom1’,{ //注意無論是哪種方式創(chuàng)建組件,template都只能有一個(gè)唯一的根元素 template: ’<h3>組件1</h3>’,//指定組件要展示的html結(jié)構(gòu) }) Vue.component(’mycom2’,{ //注意無論是哪種方式創(chuàng)建組件,template都只能有一個(gè)唯一的根元素 template: ’<h3>組件2</h3>’,//指定組件要展示的html結(jié)構(gòu) }) //創(chuàng)建一個(gè)vue實(shí)例 //當(dāng)我們導(dǎo)入包之后,在瀏覽器的內(nèi)存中就多了一個(gè)vue構(gòu)造函數(shù) var vm = new Vue({ el: ’#app’,//表示當(dāng)前我們new的這個(gè)vue實(shí)例要控制頁面上的哪個(gè)區(qū)域 data: { //data屬性中存放的是el中要用到的數(shù)據(jù) componentName: ’mycom1’ } }); </script></html>
以上這篇Vue的自定義組件不能使用click方法的解決就是小編分享給大家的全部內(nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持好吧啦網(wǎng)。
相關(guān)文章:
1. 怎樣才能用js生成xmldom對象,并且在firefox中也實(shí)現(xiàn)xml數(shù)據(jù)島?2. 基于javaweb+jsp實(shí)現(xiàn)企業(yè)車輛管理系統(tǒng)3. 利用ajax+php實(shí)現(xiàn)商品價(jià)格計(jì)算4. ASP.Net MVC利用NPOI導(dǎo)入導(dǎo)出Excel的示例代碼5. jstl 字符串處理函數(shù)6. JSP動(dòng)態(tài)網(wǎng)頁開發(fā)原理詳解7. PHP中為什么使用file_get_contents("php://input")接收微信通知8. .Net core Blazor+自定義日志提供器實(shí)現(xiàn)實(shí)時(shí)日志查看器的原理解析9. IOS蘋果AppStore內(nèi)購付款的服務(wù)器端php驗(yàn)證方法(使用thinkphp)10. XML CDATA是什么?
