vant Popup弹框使用总结

一、基础用法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的小伙伴一块儿探讨。

相关文章
相关标签/搜索