1、基础用法
通过v-model控制弹出层是否展示
1<van-cell is-link @click="showPopup">展示弹出层</van-cell> 2 3<van-popup v-model="show">内容</van-popup> 4export default { 5 data() { 6 return { 7 show: false 8 } 9 }, 10 11 methods: { 12 showPopup() { 13 this.show = true; 14 } 15 } 16};
2、弹框组件
如果弹出内容作为组件的话,配合button组件使用
父组件
1<van-button type="primary" @click size="large" @click="show()"> 2 显示组件 3 </van-button> 4 5<child v-if="show" @closetip="show()" :arr="fathermsg"></child> 6 7export defanlt{ 8data(){ 9return{ 10show:false, 11fathermsg:"" 12} 13 14}, 15methods(){ 16show(){ 17this.show=!this.show 18}, 19 20} 21}
子组件
1<template> 2 <van-popup v-model="myshow" closeable :duration='0.3' 3 @click-overlay='close' @click='close'> 4<van-list 5 v-model="loading" 6 :finished="finished" 7 finished-text="没有更多了" 8 9> 10 <van-cell 11 v-for="item in dataarr" 12 /> 13</van-list> 14 15 </van-popup> 16</template> 17 18export default { 19 name:'getOrder', 20 props:["arr"],//父组件传来的值 21 data(){ 22 return{ 23 myshow:true,//popup的显示,在组件中,默认是显示,不用父组件传值 24 dataarr:this.arr, 25 } 26 }, 27 methods: { 28 close(){ 29 this.$emit("closeTip",false)//把关闭信息传递给父组件 30 } 31 } 32}
注:父组件的显示v-if 或v-show看自己需求,有人发现v-if的时候有问题,我这没有发现,也给各位提个醒。
11-18添加
用以上方法的话,就失去了组件的动画效果,postion的属性就不生效了。如果需要过渡效果的话,建议使用一下两种方式
1、在值存放在store里,如果使用了vuex的话,会很方便的实现。
store里面
1import Vue from 'vue' 2import Vuex from 'vuex' 3 4Vue.use(Vuex) 5const state = { 6 // 会员页面 7 isVipPage:false, 8 9} 10const getters = { 11 12} 13const mutations = { 14 setIsVipPage(state){ 15 state.isVipPage = !state.isVipPage; 16}, 17 18} 19export default new Vuex.Store({ 20 state, 21 mutations, 22 getters 23})
父组件
1<van-button type="primary" size="large" @click="setIsVipPage()"> 显示组件 </van-button> 2 3<child ></child>
import { mapMutations} from "vuex";
1export defanlt{ 2data(){ 3return{ 4 5} 6 7}, 8methods(){
...mapMutations([ "setIsVipPage" ]),
1} 2}
子组件
1<template> 2<div class="popu_com vip_com"> 3 <van-popup v-model="isVipPage" position="right":style="{ height: '100%'}" closeable> 4 </van-popup> 5</div> 6</template> 7<script> 8import {mapMutations,mapGetters} from 'vuex'; 9export default { 10 11 12 data(){ 13 return{ 14 } 15 }, 16 computed:{ 17 isVipPage:{ 18 get(){ 19 return this.$store.state.isVipPage; 20 }, 21 set(val){ 22 this.$store.commit("setIsVipPage", val); 23 }, 24 }, 25 26 }, 27} 28</script>
注:关闭弹框的事件可以不写,computed 直接出发了store里的存储值
2、还是父子传值
父组件
1<van-button type="primary" size="large" @click="changeshow"> 显示组件 </van-button> 2<child @closeTip='changeshow' :showother="show"></child> 3export defanlt{ 4data(){ 5return{ 6show:false, 7} 8 9}, 10methods(){ 11 changeshow(obj) { 12 //子组件关闭事件触发的 13 if (obj.flg) { 14 this.show = false; 15 } else { 16 //父级点击按钮触发 17 this.show = !this.show; 18 } 19 }, 20 21} 22}
子组件
1<template> 2<div class="popu_com vip_com"> 3 <van-popup v-model="myshow" position="right":style="{ height: '100%'}" closeable @click-overlay="close()" @close="close()"> 4 </van-popup> 5</div> 6</template> 7<script> 8export default { 9 props:["showother"], 10 data(){ 11 return{ 12 myshow:this.showother,//映射 13 } 14 }, 15 methods:{ 16 close(){ 17 this.$emit("closeTip",{"flg":"1"}) 18 }, 19 }, 20 watch:{ 21 showother:{ 22 handler(newval,oldval){ 23 this.myshow= newval; 24 } 25 } 26 } 27} 28</script>
这两种方式经测试都可以实现。欢迎正在使用vant的小伙伴一起探讨。