uniapp 引用 vant 組件
1、下載vant組件
vant的GitHub下載地址下載完成后解壓,然后在項目更目錄下創建文件夾wxcomponents
,注意這里的wxcomponents
目錄級別和pages
在同一級別,然后再該目錄下創建vant
目錄。回到剛才解壓的vant目錄,找到dist
文件夾,把它復制到vant
目錄下
2、引入組件
2.1 首先在app.vue
文件內添加
1 @import "/wxcomponents/vant/dist/common/index.wxss";
2.2 在pages.json文件內添加組件引用
你可以選擇在一個頁面的配置文件里面配置,但是只能在這個頁面內使用,你也可以選擇在globalStyle里面配置,是的所有頁面都可以直接使用
1 "usingComponents":{ 2 "van-button":"/wxcomponents/vant/dist/button/index", 3 "van-notify":"/wxcomponents/vant/dist/notify/index" 4 }
3、使用組件
3.1 注意notify的使用,她還需要在main.js配置一下,不然就會報$notify is undefined 錯誤。
1 import Notify from './wxcomponents/vant/dist/notify/notify'; 2 3 Vue.prototype.$notify = Notify
3.2 最后,在你要使用的頁面內添加你要使用的組件就可以了。
1 <template> 2 <view> 3 <van-button @click="aa()" type="info">信息按鈕</van-button> 4 <van-notify id="van-notify" /> 5 </view> 6 </template> 7 8 <script> 9 export default { 10 data(){ 11 return{ 12 13 } 14 }, 15 methods:{ 16 aa(){ 17 this.$notify({ type: "danger", message: "通知內容" }); 18 } 19 } 20 } 21 </script> 22 23 <style> 24 </style>
效果: