Vue彈窗的兩種實(shí)現(xiàn)方式實(shí)例詳解
方法一 使用.sync修飾符
element就是使用的這種方式,實(shí)現(xiàn)方式如下:
父組件:
<template> <div id="demo"> <test-model :show.sync="showFlag"></test-model> </div> </template> <script> import testModel from 'src/components/testModel' export default { data(){ return{ showFlag: false } }, components: { testModel, } } </script> <style lang="scss" scoped> </style>
子組件:
<template> <div v-if="show"> <button @click="closeShow"></button> </div> </template> <script> export default { data(){ return{ } }, props: ["show"], methods: { closeShow() { this.$emit("update:show",false); } }, } </script> <style lang="scss" scoped> </style>
子組件通過this.$emit("update:show",false); 可以直接將父組件傳過來的show值改變,從而達(dá)到顯示隱藏的目的。
方法二 使用v-model
父組件
<template> <div id="demo"> <test-model v-model="show"></test-model> </div> </template> <script> import testModel from 'src/components/testModel' export default { data(){ return{ show: false } }, components: { testModel, } } </script> <style lang="scss" scoped> </style>
子組件
<template> <div v-if="value"> <button @click="closeShow"></button> </div> </template> <script> export default { data(){ return{ } }, props: ["value"], model: { prop: 'value', event: 'balabala' }, methods: { changeShow() { this.$emit("balabala",false); } }, } </script> <style lang="scss" scoped> </style>
官方文檔里有寫,一個(gè)組件上的v-model
默認(rèn)會(huì)利用名為value
的 prop 和名為input
的事件,但是像單選框、復(fù)選框等類型的輸入控件可能會(huì)將value
特性用于不同的目的。model
選項(xiàng)可以用來避免這樣的沖突,所以上面的子組件里,我們用balabala來替換掉了原本的input事件,所以在$emit的時(shí)候,里面的事件名應(yīng)為balabala,否則默認(rèn)為input.
到此這篇關(guān)于Vue彈窗的兩種實(shí)現(xiàn)方式的文章就介紹到這了,更多相關(guān)vue彈窗實(shí)現(xiàn)內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
使用vue自定義如何實(shí)現(xiàn)Tree組件和拖拽功能
這篇文章主要介紹了使用vue自定義如何實(shí)現(xiàn)Tree組件和拖拽功能,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-12-12Vue用mixin合并重復(fù)代碼的實(shí)現(xiàn)
這篇文章主要介紹了Vue用mixin合并重復(fù)代碼的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-11-11vue或css動(dòng)畫實(shí)現(xiàn)列表向上無縫滾動(dòng)
這篇文章主要為大家詳細(xì)介紹了vue或css動(dòng)畫實(shí)現(xiàn)列表向上無縫滾動(dòng),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-04-04通過cordova將vue項(xiàng)目打包為webapp的方法
這篇文章主要介紹了通過cordova將vue項(xiàng)目打包為webapp的方法,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2019-02-02新手vue構(gòu)建單頁面應(yīng)用實(shí)例代碼
本篇文章主要介紹了新手vue構(gòu)建單頁面應(yīng)用實(shí)例代碼,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-09-09antd中table展開行默認(rèn)展示,且不需要前邊的加號操作
這篇文章主要介紹了antd中table展開行默認(rèn)展示,且不需要前邊的加號操作,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-11-11