uni-app使用Vant組件


前言

vant ui有h5版和微信小程序版。其h5版只能用於h5,其微信小程序版(vant weapp)可用於微信和App,從uni-app 2.4.7起,H5和QQ小程序也支持了微信小程序組件。

使用步驟

下載代碼

  • 在項目根目錄下新建 wxcomponents 目錄 ,此目錄應該與components 目錄同級。

  • 直接通過 git 下載 vant-weapp 最新源代碼,並將dist目錄拷貝到新建的wxcomponents目錄下,並重命名distvant-weapp

  • 在pages.json的globalStyle中 引入所需要的組件

試試是否成功

<template>
  <div>
    <van-picker
      v-if="flag"
      :columns="columns"
      @change="changePicker"
    />
    <van-button @click="show">顯示</van-button>
  </div>

</template>

<script>
export default {
  data() {
    return {
      columns: ["杭州", "寧波", "溫州", "嘉興", "湖州"],
      flag: false
    };
  },
  methods: {
    show() {
      console.log("1 =", "show");
      this.flag = !this.flag;
    },
    changePicker(event) {
      console.log("1 =", event);
    }
  }
};
</script>

注意事項

Vant組件中Notify 消息提示比較特殊

不僅需要在pages.json的globalStyle中 引入還需要再main.js中添加到vue原型上

//main.js

import Notify from './wxcomponents//vant/notify/notify';

Vue.prototype.$notify = Notify

然后在頁面中使用

<template>
    <view>
        <van-button @click="showNotify">彈出提示</van-button>
 	<van-notify id="van-notify" />
    </view>
</template>

<script>
export default {
  methods: {
    showNotify() {
       this.$notify({ type: "danger", message: "通知內容" });
    }
  }
};
</script>


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM