uni-app导入vant(上干货)

本文介绍了如何在uni-app项目中集成 vant UI 库,包括创建项目、安装 vant、全局导入组件和样式,以及如何使用 vant 官网的组件。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

  • 一、在Hbulder中创建一个项目,如:uni-vant

  • 二、安装vant:

1.运行命令cmd安装vant到uni-app项目中:

npm i vant@latest-v2 -S

2.安装成功后可以在package.json中看到已经加入该依赖

3.我们可以在根目录中看到下面的文件结构(之后我们使用的将是这个红色框中的目录中),node_modules没有的可以运行命令cmd中安装:npm i

  • 三、进行全局导入vant组件

在main.js中进行挂载Vant组件,主看带有编号的两步

import App from './App'
// 1.导入Vant组件,这里的‘vant’就是我们上面新导入的vant的目录
import Vant from './node_modules/vant/lib/vant';

// #ifndef VUE3
import Vue from 'vue'
Vue.config.productionTip = false
App.mpType = 'app'
const app = new Vue({
    ...App
})
app.$mount()
// #endif
// 2. 将Vant组件挂载到Vue上
Vue.use(Vant);
app.$mount()

// #ifdef VUE3
import { createSSRApp } from 'vue'
export function createApp() {
  const app = createSSRApp(App)
  return {
    app
  }
}
// #endif
  • 四、导入vant全局样式,在App.vue中进行导入

复制这段代码到style中:@import '@/node_modules/vant/lib/index.css';

  • 五、在官网中找到想要的组件粘贴复制到你要用的位置。

<template>
    <view class="content">
        <image class="logo" src="/static/logo.png"></image>
        <view class="text-area">
            <text class="title">{{title}}</text>
        </view>
        <van-button type="primary">主要按钮</van-button>
        <van-button type="info">信息按钮</van-button>
        <van-button type="default">默认按钮</van-button>
        <van-button type="warning">警告按钮</van-button>
        <van-button type="danger">危险按钮</van-button>
    </view>
</template>

<script>
    export default {
        data() {
            return {
                title: 'Hello'
            }
        },
        onLoad() {

        },
        methods: {

        }
    }
</script>

<style>
    .content {
        display: flex;
        flex-direction: column;
        align-items: center;
        justify-content: center;
    }

    .logo {
        height: 200rpx;
        width: 200rpx;
        margin-top: 200rpx;
        margin-left: auto;
        margin-right: auto;
        margin-bottom: 50rpx;
    }

    .text-area {
        display: flex;
        justify-content: center;
    }

    .title {
        font-size: 36rpx;
        color: #8f8f94;
    }
</style>
  • 效果图

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

WeChat624

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值