vant图标失效(不显示)

本文介绍了一个关于vant图标显示失效的问题及解决办法。原因是图标字体引用的在线文件未能正确加载。解决方式是在main.js中引入本地的vant图标文件。

vant图标失效(不显示)

本应该是这样的
在这里插入图片描述
失效:
在这里插入图片描述
原因:
在这里插入图片描述
图标字体引用的在线文件无法请求到

解决:

import Vue from 'vue'
import Vant from 'vant';
import 'vant/lib/index.css';
import 'vant/lib/icon/local.css';//本地
Vue.use(Vant)

import ‘vant/lib/icon/local.css’;//本地

在main.js里面引入本地文件

<think> 我们正在讨论Vant 2中的van-search组件。根据用户要求,我们需要提供搜索功能的使用方法和示例。 注意:用户要求的是Vant 2(对应Vue2)的使用方法,因此我们主要参考引用[1]的内容(因为引用[1]是关于vue2-vant的),同时引用[2]中关于van-search的一些坑也可以作为注意事项。 主要功能点: 1. 绑定搜索关键词(v-model) 2. 触发搜索事件(@search) 3. 自动获取焦点(通过ref操作DOM) 4. 清空事件(@clear) 5. 右侧图标事件(@click-right-icon)等 我们将按照以下结构回答: 1. 基本用法(绑定数据、触发搜索) 2. 自动获取焦点的方法(在Vue2中) 3. 事件处理(搜索事件、清空事件、右侧图标事件)及注意事项(如引用[2]提到的坑) 4. 提供一个综合示例 注意:由于用户要求的是Vant2,所以我们使用Vue2的语法。 </think> 以下是关于 Vant 2 中 `van-search` 组件搜索功能的详细介绍和使用方法,包含关键配置和示例代码: ### 一、核心功能及使用方法 1. **数据绑定** 使用 `v-model` 双向绑定搜索关键词: ```vue <van-search v-model.trim="keyword" placeholder="请输入关键词" /> ``` `v-model.trim` 自动去除输入内容的首尾空格。 2. **事件监听** - `@search`:点击键盘搜索按钮时触发 - `@clear`:点击清空按钮时触发 - `@click-right-icon`:点击右侧图标时触发 ```vue <van-search v-model="keyword" @search="handleSearch" @clear="handleClear" @click-right-icon="handleSearch" /> ``` 3. **触发焦点方法(Vue2 语法)** 通过 `ref` 获取 DOM 后调用 `focus()`: ```vue <template> <van-search ref="searchInput" /> <van-tag @click="focusSearch">点击聚焦</van-tag> </template> <script> export default { methods: { focusSearch() { this.$refs.searchInput.querySelector('input').focus(); } } } </script> ``` ### 二、完整功能示例 ```vue <template> <div> <!-- 搜索组件 --> <van-search v-model.trim="keyword" ref="searchRef" placeholder="搜索商品" show-action clearable shape="round" @search="onSearch" @clear="onClear" > <template #action> <div @click="onSearch">搜索</div> </template> </van-search> <!-- 历史记录 --> <van-tag v-for="(item,index) in history" :key="index" @click="setKeyword(item)" > {{ item }} </van-tag> </div> </template> <script> export default { data() { return { keyword: '', history: ['手机', '电脑', '耳机'] }; }, mounted() { // 页面加载自动聚焦 this.$nextTick(() => { this.$refs.searchRef.querySelector('input').focus(); }); }, methods: { onSearch() { if (!this.keyword) return; console.log('搜索:', this.keyword); // 添加到历史记录 if (!this.history.includes(this.keyword)) { this.history.unshift(this.keyword); } }, onClear() { console.log('输入已清空'); }, setKeyword(item) { this.keyword = item; this.onSearch(); // 历史记录点击后自动聚焦 this.$refs.searchRef.querySelector('input').focus(); } } }; </script> ``` ### 三、注意事项 1. **清空按钮冲突问题** 当同时使用 `clearable` 和 `right-icon` 时,点击清空按钮可能意外触发 `@click-right-icon` 事件(引用[2])。解决方案: ```vue <van-search @clear="handleClear" @click-right-icon="keyword ? handleSearch() : null" /> ``` 2. **自动聚焦失效场景** 在 `Vue.nextTick()` 中调用聚焦确保 DOM 已渲染: ```js this.$nextTick(() => { this.$refs.searchRef.querySelector('input').focus(); }); ``` 3. **移动端键盘控制** 添加 `show-action` 属性可在键盘显示“搜索”按钮,优化移动端体验。 ### 四、常用配置属性 | 属性 | 说明 | 类型 | 默认值 | |------|------|------|--------| | shape | 搜索框形状 (`round`/`square`) | string | `square` | | clearable | 是否显示清空按钮 | boolean | `true` | | clear-trigger | 清空按钮显示时机 (`always`/`focus`) | string | `focus` | | left-icon | 左侧图标名称 | string | `search` | | right-icon | 右侧图标名称 | string | - | | background | 搜索框背景色 | string | `#f2f2f2` | [^1][^2]
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值