国产成人精品亚洲777人妖,欧美日韩精品一区视频,最新亚洲国产,国产乱码精品一区二区亚洲

您的位置:首頁技術(shù)文章
文章詳情頁

Vue的自定義組件不能使用click方法的解決

瀏覽:3日期:2022-12-25 13:57:34

先貼代碼

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.intensediesel.com/bcjs/vue.js'></script></head><body> <div id='app'> <a href='http://www.intensediesel.com/bcjs/11206.html#' rel='external nofollow' rel='external nofollow' @click='componentName=’mycom1’'>組件1</a> <a href='http://www.intensediesel.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)。

標(biāo)簽: Vue
相關(guān)文章:
主站蜘蛛池模板: 隆尧县| 旬邑县| 寿光市| 衢州市| 平乡县| 贡山| 阳曲县| 凯里市| 新兴县| 年辖:市辖区| 永修县| 收藏| 正阳县| 云龙县| 太仆寺旗| 平谷区| 长治县| 庆云县| 宁安市| 上思县| 嘉祥县| 法库县| 临汾市| 襄城县| 醴陵市| 廊坊市| 中阳县| 河西区| 福贡县| 灵武市| 汶上县| 凤城市| 博野县| 梅州市| 阿尔山市| 蓬安县| 高台县| 阿图什市| 孟连| 昌乐县| 洛宁县|