當前位置:首頁 > IT技術(shù) > 微信平臺 > 正文

微信小程序-vant-weapp日歷組件的使用(年月日)
2021-07-28 14:42:39

小程序vant-weapp的日歷組件的使用

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


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

微信小程序-vant-weapp日歷組件的使用(年月日)_java
image
微信小程序-vant-weapp日歷組件的使用(年月日)_python_02
image

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

微信小程序-vant-weapp日歷組件的使用(年月日)_vue_03
image

進入項目的根目錄底下,注意,一定要進入根目錄哦,使用cd ../返回上一級目錄~

微信小程序-vant-weapp日歷組件的使用(年月日)_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日歷組件的使用(年月日)_小程序_05
image
微信小程序-vant-weapp日歷組件的使用(年月日)_vue_06
image
微信小程序-vant-weapp日歷組件的使用(年月日)_react_07
image

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

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

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

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

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

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

微信小程序-vant-weapp日歷組件的使用(年月日)_vue_08
image

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

微信小程序-vant-weapp日歷組件的使用(年月日)_小程序_09
image

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

微信小程序-vant-weapp日歷組件的使用(年月日)_小程序_10
image
話不多說,來看看小程序vant-weapp的日歷組件的使用

日歷文檔參照一下

https://youzan.github.io/vant-weapp/#/calendar

5:使用vant-weapp日歷組件
我這里對日期的處理,是需要這樣的格式Y(jié)YYY-MM-dd
所以在對選中的日期做了一些處理和判斷~

2020-10-26

wxml

<form catchsubmit="confirmPublish">
  <view class="cu-form-group " bindtap="showCalendar">
    <view>任務(wù)時限</view>
    <input disabled="disabled" placeholder="請選擇任務(wù)時限" value="{{time}}" data-name="time">
    </input>
  </view>
  <button class='btn1' form-type="submit">確認發(fā)布</button>
</form>
<van-cell title="選擇單個日期" value="{{ date }}" bind:click="onDisplay" />
<van-calendar show="{{ show }}" bind:close="onClose" bind:confirm="onConfirm" />

js

const app = getApp()
Page({
  data: {
    maxDate: new Date().setFullYear(new Date().getFullYear() + 2),
    show: false,
    taskStartTime: '',
    time: '',
  },
  showCalendar() {
    this.setData({ show: true })
  },
  onClose() {
    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()
    }
    this.setData({
      taskStartTime: taskStartTime,
    })
    return taskStartTime;
  },
  onConfirm(e) {
    this.setData({
      time: this.formatDate(e.detail),
      show: false
    })
  },
  onLoad: function (options) {
  },
  confirmPublish: function () {
    const data = {}
    data.taskStartTime = this.data.taskStartTime
    console.log(JSON.stringify(data))
  },
})

json

{
  "usingComponents": {
    "van-calendar": "@vant/weapp/calendar/index"
  }
}
微信小程序-vant-weapp日歷組件的使用(年月日)_vue_11
image
微信小程序-vant-weapp日歷組件的使用(年月日)_java_12
image

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

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

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