vant Popup弹框使用总结

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的小伙伴一起探讨。

原文地址:https://www.cnblogs.com/zhoujuan/p/11751324.html