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

話不多說,記錄一下這個(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)目。

image

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

image

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

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

image
image
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

image

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

image

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

話不多說,來看看小程序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ù)時(shí)限</view>
    <input disabled="disabled" placeholder="請選擇任務(wù)時(shí)限" value="{{time}}" data-name="time">
    </input>
  </view>
  <button class='btn1' form-type="submit">確認(rèn)發(fā)布</button>
</form>
<van-cell title="選擇單個(gè)日期" 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"
  }
}
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

友情鏈接更多精彩內(nèi)容