當(dāng)前位置:首頁 > IT技術(shù) > 微信平臺(tái) > 正文

微信小程序-vant-weapp日期選擇器的使用(年月日時(shí)分)
2021-07-28 14:30:56

小程序vant-weapp的日期選擇器的使用(年月日時(shí)分)

話不多說,記錄一下這個(gè)框架的使用~小程序使用輕量、可靠的小程序 UI 組件庫 vant-weapp
Github源碼:https://github.com/youzan/vant-weapp
中文文檔:https://youzan.github.io/vant-weapp/#/intro


1:打開微信開發(fā)者工具,填寫自己的appid和項(xiàng)目名稱,選擇不使用云服務(wù),新建一個(gè)項(xiàng)目。

微信小程序-vant-weapp日期選擇器的使用(年月日時(shí)分)_react
image
微信小程序-vant-weapp日期選擇器的使用(年月日時(shí)分)_python_02
image

2:右擊在選擇在終端打開

微信小程序-vant-weapp日期選擇器的使用(年月日時(shí)分)_小程序_03
image

進(jìn)入項(xiàng)目的根目錄底下,注意,一定要進(jìn)入根目錄哦,使用cd ../返回上一級(jí)目錄~

微信小程序-vant-weapp日期選擇器的使用(年月日時(shí)分)_vue_04
image

3:小程序已經(jīng)支持使用 npm 安裝第三方包,
這里通過 npm 安裝

1、第一步:npm init

   2、第二步:npm install --production

   3、第三步: npm i @vant/weapp -S --production
      或者  npm i vant-weapp -S --production
微信小程序-vant-weapp日期選擇器的使用(年月日時(shí)分)_vue_05
image
微信小程序-vant-weapp日期選擇器的使用(年月日時(shí)分)_小程序_06
image
微信小程序-vant-weapp日期選擇器的使用(年月日時(shí)分)_vue_07
image

這里需要注意一下
npm i vant-weapp -S --production或者npm i @vant/weapp -S --production
引入的區(qū)別

使用npm i vant-weapp安裝的時(shí)候,到時(shí)候在在app.json或index.json中引入組件,需要使用這樣的路徑

{
  "usingComponents": {
    "van-button": "../../miniprogram_npm/vant-weapp/button/index"
  }
}

使用npm i @vant/weapp安裝的時(shí)候,到時(shí)候在在app.json或index.json中引入組件,需要使用這樣的路徑(推薦,因?yàn)檫@個(gè)可以直接抄文檔,不需要改變引入路徑的~)

{
"usingComponents": {
  "van-button": "@vant/weapp/button/index"
}
}

4:在微信開發(fā)工具執(zhí)行npm 構(gòu)建,點(diǎn)擊工具里面,構(gòu)建npm

微信小程序-vant-weapp日期選擇器的使用(年月日時(shí)分)_python_08
image

構(gòu)建過程需要等待一會(huì)兒,不要捉急

微信小程序-vant-weapp日期選擇器的使用(年月日時(shí)分)_vue_09
image

構(gòu)建完會(huì)生成一個(gè)miniprogram_npm文件夾
如果構(gòu)建完如果編譯報(bào)錯(cuò),再構(gòu)建一次就好了

微信小程序-vant-weapp日期選擇器的使用(年月日時(shí)分)_小程序_10
image
話不多說,來看看小程序vant-weapp的日期選擇器的使用

日期選擇器文檔參照一下

https://youzan.github.io/vant-weapp/#/datetime-picker

5:使用DatetimePicker 時(shí)間選擇組件

選擇日期格式如下:

2021-06-25 9:29

參考代碼:
wxml

<view>當(dāng)前選擇:{{currentChoose}}</view>
<button bind:tap="openPicker">打開選擇器</button>
<van-action-sheet show="{{ show }}" bind:close="onClose" bind:getuserinfo="onGetUserInfo">
  <van-datetime-picker type="datetime" value="{{ currentDate }}" min-date="{{ minDate }}" max-date="{{ maxDate }}" bind:cancel="onCancel"
    bind:confirm="onConfirm"  bind:change="onChange" />
</van-action-sheet>

js

const app = getApp()
Page({
  data: {
    minHour: 0,
    maxHour: 24,
    minDate: new Date(1990,1,1).getTime(),
    maxDate: new Date(2099, 12, 31).getTime(),
    currentDate: new Date().getTime(),
    show: false,
    currentChoose: ''
  },
  openPicker() {
    this.setData({ show: true })
  },
  onConfirm(e) {
    this.setData({ show: false, currentChoose: this.formatDate(new Date(e.detail)) })
  },
  onClose() {
    this.setData({ show: false })
  },
  onCancel() {
    this.setData({ show: false })
  },
  formatDate(date) {
    let taskStartTime
    if (date.getMonth() < 9) {
      taskStartTime = date.getFullYear() + "-0" + (date.getMonth() + 1) + "-"
    } else {
      taskStartTime = date.getFullYear() + "-" + (date.getMonth() + 1) + "-"
    }
    if (date.getDate() < 10) {
      taskStartTime += "0" + date.getDate()
    } else {
      taskStartTime += date.getDate()
    }
    taskStartTime += " " + date.getHours() + ":" + date.getMinutes()
    this.setData({
      taskStartTime: taskStartTime,
    })
    return taskStartTime;
  },
})

json

{
  "usingComponents": {
    "van-datetime-picker": "@vant/weapp/datetime-picker/index",
    "van-action-sheet": "@vant/weapp/action-sheet/index"
  }
}

結(jié)果:
點(diǎn)擊打開選擇器的時(shí)候
日期選擇器的組件會(huì)從底部彈框彈出
可以選擇自己想要的時(shí)間,然后將時(shí)間顯示在頁面上
或者傳遞給后端都可以
根據(jù)自己的需求進(jìn)行修改~~~

微信小程序-vant-weapp日期選擇器的使用(年月日時(shí)分)_vue_11
微信小程序-vant-weapp日期選擇器的使用(年月日時(shí)分)_react_12

? 著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者

本文摘自 :https://blog.51cto.com/u

開通會(huì)員,享受整站包年服務(wù)立即開通 >