在h5中使用vant组件 (vue3.x+vant3开发)

24 篇文章 0 订阅

vant官方文档:https://vant-contrib.oschina.io/vant/v3/#/zh-CN/home

1.需要用到的文件

<!-- 引入样式文件 -->
<link
  rel="stylesheet"
  href="https://cdn.jsdelivr.net/npm/vant@next/lib/index.css"
/>

<!-- 引入 Vue 和 Vant 的 JS 文件 -->
<script src="https://cdn.jsdelivr.net/npm/vue@next"></script>
<script src="https://cdn.jsdelivr.net/npm/vant@next/lib/vant.min.js"></script>

2.在h5中使用

  1. 用到的组件标签全部放在#app下
<div id="app">
	// 需要使用的标签
</div>
  1. 创建vue
const appHtml = {
        data(){
            return {
                
            }
        },
        methods: {
            
        },
        mounted() {
            
        },
    }
    // 创建
    const app=Vue.createApp(appHtml);
    app.mount('#app');
  1. 引入vant组件
app.use(vant);
  1. 引入需要使用的vant组件,引入的组件必须加上vant.,如:需要使用vant中的button组件,app.use(vant.Button)
  2. vant组件的使用(演示如何引入使用轮播组件)

有些组件的标签引入时,必须得把单标签设置成双标签,如:

在使用宫格布局时,官方文档的标签是:
<van-grid>
  <van-grid-item icon="photo-o" text="文字" />
  <van-grid-item icon="photo-o" text="文字" />
  <van-grid-item icon="photo-o" text="文字" />
  <van-grid-item icon="photo-o" text="文字" />
</van-grid>

在h5中使用必须把   
<van-grid-item icon="photo-o" text="文字" />
改变为  
<van-grid-item icon="photo-o" text="文字" > </van-grid-item>
否则下面的标签都将不显示

(1) 引入注册组件 app.use(vant.Swipe)
在这里插入图片描述
(2) 引入标签
在这里插入图片描述

在这里插入图片描述

在这里插入图片描述
在这里插入图片描述

完整代码
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="initial-scale=1, maximum-scale=1, user-scalable=no, minimal-ui">
    <title>首页</title>
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/vant@next/lib/index.css" >
</head>
<body>

<div id="app">
    <van-nav-bar title="标题"></van-nav-bar>
    <!-- 轮播图 -->
    <van-swipe :autoplay="3000" lazy-render>
        <van-swipe-item v-for="image in images" :key="image">
            <img :src="image" />
        </van-swipe-item>
    </van-swipe>
    <!-- 宫格布局 -->
    <div class="grid" style="margin-top: 15px;">
        <van-grid :column-num="3">
            <van-grid-item v-for="value in 6" :key="value" icon="photo-o" text="文字"></van-grid-item>
        </van-grid>
    </div>

    <!-- 空状态 -->
    <van-empty description="描述文字"></van-empty>

    <!-- button按钮 -->
    <van-button type="primary" is-link url="binding.html">标签上添加跳转1</van-button>
    <van-button type="primary" @click="toUrl" >点击跳转</van-button>
    
</div>
<script src="https://cdn.jsdelivr.net/npm/vue@next"></script>
<script src="https://cdn.jsdelivr.net/npm/vant@next/lib/vant.min.js"></script>
<script>
    const appHtml = {
        data(){
            return {
                images: [
                    'https://img.yzcdn.cn/vant/apple-1.jpg',
                    'https://img.yzcdn.cn/vant/apple-2.jpg',
                ]
            }
        },
        methods: {
            toUrl(){
                console.log('页面跳转');
                window.location.href="info.html"
            }
        },
        mounted() {
            
        },
    }
    // 创建
    const app=Vue.createApp(appHtml)
    // 引入vant组件
    app.use(vant);
    // 自动注册 Lazyload 组件
    app.use(vant.Lazyload);

    // 引入button
    app.use(vant.Button);
    // 引入宫格布局
    app.use(vant.Grid);
    app.use(vant.GridItem);
    // 空状态
    app.use(vant.Empty);

    app.mount('#app');

</script>
<style>
    /* 定义轮播图 */
    img{
        width: 100%;
        height: 260px;
    }
</style>
</body>
</html>

  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值