Grid 宫格

Grid 宫格

介绍

宫格可以在水平方向上把页面分隔成等宽度的区块,用于展示内容或进行页面导航。

引入

在app.json或index.json中引入组件,详细介绍见快速上手。

"usingComponents": {  "van-grid": "@vant/weapp/grid/index",  "van-grid-item": "@vant/weapp/grid-item/index"}

Vant Weapp 1.0 版本开始支持此组件,升级方式参见快速上手

代码演示

基本用法

通过icon属性设置格子内的图标,text属性设置文字内容。

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

自定义列数

默认一行展示四个格子,可以通过column-num自定义列数。

<​van-grid column-num="3">  <​van-grid-item icon="photo-o" text="文字" wx:for="{{ 6 }}" /><​/van-grid>

自定义内容

通过插槽可以自定义格子展示的内容。

<​van-grid column-num="3" border="{{ false }}">  <​van-grid-item use-slot wx:for="{{ 3 }}" wx:for-item="index">    <​image      style="width: 100%; height: 90px;"      src="https://img.yzcdn.cn/vant/apple-{{ index + 1 }}.jpg"    />  <​/van-grid-item><​/van-grid>

正方形格子

设置square属性后,格子的高度会和宽度保持一致。

<​van-grid square>  <​van-grid-item icon="photo-o" text="文字" wx:for="{{ 8 }}" /><​/van-grid>

格子间距

通过gutter属性设置格子之间的距离。

<​van-grid gutter="{{ 10 }}">  <​van-grid-item icon="photo-o" text="文字" wx:for="{{ 8 }}" /><​/van-grid>

内容横排

将direction属性设置为horizontal,可以让宫格的内容呈横向排列。

<​van-grid direction="horizontal" column-num="2">  <​van-grid-item icon="photo-o" text="文字" />  <​van-grid-item icon="photo-o" text="文字" />  <​van-grid-item icon="photo-o" text="文字" /><​/van-grid>

页面跳转

可以通过url属性进行页面跳转,通过link-type属性控制跳转类型。

<​van-grid clickable column-num="2">  <​van-grid-item    icon="home-o"    link-type="navigateTo"    url="/pages/dashboard/index"    text="Navigate 跳转"  />  <​van-grid-item    icon="search"    link-type="reLaunch"    url="/pages/dashboard/index"    text="ReLaunch 跳转"  /><​/van-grid>

提示信息

设置dot属性后,会在图标右上角展示一个小红点。设置badge属性后,会在图标右上角展示相应的徽标。

<​van-grid column-num="2">  <​van-grid-item icon="home-o" text="文字" dot />  <​van-grid-item icon="search" text="文字" badge="99+" /><​/van-grid>

API

Grid Props

参数 说明 类型 默认值 版本
column-num 列数 number 4 -
icon-size v1.3.2 图标大小,默认单位为px string 28px

| gutter | 格子之间的间距,默认单位为px | string | number | 0 | - | |---|---|---|---|---|

border 是否显示边框 boolean true -
center 是否将格子内容居中显示 boolean true -
square 是否将格子固定为正方形 boolean false -
clickable 是否开启格子点击反馈 boolean false -
direction 格子内容排列的方向,可选值为 horizontal string vertical -
reverse v1.7.0 是否调换图标和文本的位置 boolean false
use-slot 是否使用自定义内容的插槽 boolean false

Grid 外部样式类

类名 说明
custom-class 根节点样式类

GridItem Props

参数 说明 类型 默认值 版本
text 文字 string - -
icon 图标名称或图片链接,可选值见 Icon 组件 string - -
icon-color 图标颜色 string - -
icon-prefix v1.7.0 第三方图标前缀 string van-icon -
dot 是否显示图标右上角小红点 boolean false -

| badge | 图标右上角徽标的内容 | string | number | - | - | |---|---|---|---|---|

url 点击后跳转的链接地址 string - -
link-type 链接跳转类型,可选值为 redirectTo switchTab reLaunch string navigateTo -

GridItem Events

事件名 说明 回调参数
bind:click 点击格子时触发 -

GridItem Slots

名称 说明
- 自定义宫格的所有内容,需要设置use-slot属性
icon 自定义图标,如果设置了use-slot或者icon属性则不生效
text 自定义文字,如果设置了use-slot或者text属性则不生效

GridItem 外部样式类

类名 说明
custom-class 根节点样式类
content-class 内容样式类
icon-class 图标样式类
text-class 文本样式类