1、基礎用法
通過v-model控制彈出層是否展示
<van-cell is-link @click="showPopup">展示彈出層</van-cell> <van-popup v-model="show">內容</van-popup> export default { data() { return { show: false } }, methods: { showPopup() { this.show = true; } } };
2、彈框組件
如果彈出內容作為組件的話,配合button組件使用
父組件
<van-button type="primary" @click size="large" @click="show()"> 顯示組件 </van-button> <child v-if="show" @closetip="show()" :arr="fathermsg"></child> export defanlt{ data(){ return{ show:false, fathermsg:"" } }, methods(){ show(){ this.show=!this.show }, } }
子組件
<template>
<van-popup v-model="myshow" closeable :duration='0.3'
@click-overlay='close' @click='close'>
<van-list
v-model="loading"
:finished="finished"
finished-text="沒有更多了"
>
<van-cell
v-for="item in dataarr"
/>
</van-list>
</van-popup>
</template>
export default {
name:'getOrder',
props:["arr"],//父組件傳來的值
data(){
return{
myshow:true,//popup的顯示,在組件中,默認是顯示,不用父組件傳值
dataarr:this.arr,
}
},
methods: {
close(){
this.$emit("closeTip",false)//把關閉信息傳遞給父組件
}
}
}
注:父組件的顯示v-if 或v-show看自己需求,有人發現v-if的時候有問題,我這沒有發現,也給各位提個醒。
11-18添加
用以上方法的話,就失去了組件的動畫效果,postion的屬性就不生效了。如果需要過渡效果的話,建議使用一下兩種方式
1、在值存放在store里,如果使用了vuex的話,會很方便的實現。
store里面
import Vue from 'vue' import Vuex from 'vuex' Vue.use(Vuex) const state = { // 會員頁面 isVipPage:false, } const getters = { } const mutations = { setIsVipPage(state){ state.isVipPage = !state.isVipPage; }, } export default new Vuex.Store({ state, mutations, getters })
父組件
<van-button type="primary" size="large" @click="setIsVipPage()"> 顯示組件 </van-button>
<child ></child>
import { mapMutations} from "vuex";
export defanlt{ data(){ return{ } }, methods(){
...mapMutations([ "setIsVipPage" ]),
} }
子組件
<template> <div class="popu_com vip_com"> <van-popup v-model="isVipPage" position="right":style="{ height: '100%'}" closeable> </van-popup> </div> </template> <script> import {mapMutations,mapGetters} from 'vuex'; export default { data(){ return{ } }, computed:{ isVipPage:{ get(){ return this.$store.state.isVipPage; }, set(val){ this.$store.commit("setIsVipPage", val); }, }, }, } </script>
注:關閉彈框的事件可以不寫,computed 直接出發了store里的存儲值
2、還是父子傳值
父組件
<van-button type="primary" size="large" @click="changeshow"> 顯示組件 </van-button> <child @closeTip='changeshow' :showother="show"></child> export defanlt{ data(){ return{ show:false, } }, methods(){ changeshow(obj) { //子組件關閉事件觸發的 if (obj.flg) { this.show = false; } else { //父級點擊按鈕觸發 this.show = !this.show; } }, } }
子組件
<template> <div class="popu_com vip_com"> <van-popup v-model="myshow" position="right":style="{ height: '100%'}" closeable @click-overlay="close()" @close="close()"> </van-popup> </div> </template> <script> export default { props:["showother"], data(){ return{ myshow:this.showother,//映射 } }, methods:{ close(){ this.$emit("closeTip",{"flg":"1"}) }, }, watch:{ showother:{ handler(newval,oldval){ this.myshow= newval; } } } } </script>
這兩種方式經測試都可以實現。歡迎正在使用vant的小伙伴一起探討。
