一、基礎用法vue
經過v-model
控制彈出層是否展現vuex
<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; } } };
二、彈框組件post
若是彈出內容做爲組件的話,配合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 }, } }
子組件this
<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的時候有問題,我這沒有發現,也給各位提個醒。spa
11-18添加code
用以上方法的話,就失去了組件的動畫效果,postion的屬性就不生效了。若是須要過渡效果的話,建議使用一下兩種方式blog
一、在值存放在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裏的存儲值
二、仍是父子傳值
父組件
<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的小夥伴一塊兒探討。