Tag Archives: Vant Applet Before-close

Vant Applet: The Usage of Vant-dialog in Before-close


## Wrong
//index.wxml
<van-dialog 
	use-slot 
	title="title" 
	show="{{ showDialog }}"
	beforeClose="beforeClose">
  <view class="dialog-content"></view>
</van-dialog>
//index.js
page({
	data:{
		showDialog:true
	},
	beforeClose() {
	    return new Promise((resolve) => {
	      setTimeout(() => {
	        if (action === 'confirm') {
	          resolve(true);
	        } else {
	          resolve(false);
	        }
	      }, 1000);
	    });
	})
  },
  ## Right
  //index.wxml
	<van-dialog 
	use-slot 
	title="title" 
	show="{{ showDialog }}"
	before-close="{{ beforeClose }}">
	  <view class="dialog-content">
	  </view>
	</van-dialog>
	//index.js
	Page({
	  data: {
	  	showDialog:true,
	    beforeClose(action) {
         return new Promise((resolve) => {
            setTimeout(() => {
              if (action === 'confirm') {
                resolve(true);
              } else {
                resolve(false);
              }
            }, 1000);
          });
        },
	  }
	})
	If you want to get a property in data beforeClose, you can't use this.data to get the data in data, so you can't get the data
	You can get the data through the WeChat applet - getCurrentPages (get the current page stack)
	const pages = getCurrentPages()
    let page = pages[pages.length-1]
    page is the current page
    At this point you can use page.data to get the properties of the current page