codecamp

Vant4 Highlight 高亮文本

介绍

高亮指定文本内容。请升级 vant 到 >= 4.8.0 版本来使用该组件。

引入

通过以下方式来全局注册组件,更多注册方式请参考组件注册

import { createApp } from 'vue';
import { Highlight } from 'vant';

const app = createApp();
app.use(Highlight);

代码演示

基础用法

你可以通过 keywords 指定需要高亮的关键字,通过 source-string 指定源文本。

<van-highlight :keywords="keywords" :source-string="text" />
export default {
  setup() {
    const text = '慢慢来,不要急,生活给你出了难题,可也终有一天会给出答案。';
    const keywords = '难题';

    return {
      text,
      keywords,
    };
  },
};

多字符匹配

如果需要指定多个关键字,可以以数组的形式传入 keywords

<van-highlight :keywords="keywords" :source-string="text" />
export default {
  setup() {
    const text = '慢慢来,不要急,生活给你出了难题,可也终有一天会给出答案。';
    const keywords = ['难题', '终有一天', '答案'];

    return {
      text,
      keywords,
    };
  },
};

设置高亮标签类名

通过 highlight-class 可以设置高亮标签的类名,以便自定义样式。

<van-highlight
  :keywords="keywords"
  :source-string="text"
  highlight-class="custom-class"
/>
export default {
  setup() {
    const text = '慢慢来,不要急,生活给你出了难题,可也终有一天会给出答案。';
    const keywords = '生活';

    return {
      text,
      keywords,
    };
  },
};
.custom-class {
  color: red;
}

API

Props

参数 说明 类型 默认值
auto-escape 是否自动转义 boolean true
case-sensitive 是否区分大小写 boolean false
highlight-class 高亮元素的类名 string -
highlight-tag 高亮元素对应的 HTML 标签名 string span
keywords 期望高亮的文本 string | string[] -
source-string 源文本 string -
tag 根节点对应的 HTML 标签名 string div
unhighlight-class 非高亮元素的类名 string -
unhighlight-tag 非高亮元素对应的 HTML 标签名 string span

类型定义

组件导出以下类型定义:

import type { HighlightProps, HighlightThemeVars } from 'vant';

主题定制

样式变量

组件提供了下列 CSS 变量,可用于自定义样式,使用方法请参考 ConfigProvider 组件

名称 默认值 描述
--van-highlight-tag-color var(--van-primary-color) 高亮文本颜色
Vant4 Empty 空状态
Vant4 ImagePreview 图片预览
温馨提示
下载编程狮App,免费阅读超1000+编程语言教程
取消
确定
目录

关闭

MIP.setData({ 'pageTheme' : getCookie('pageTheme') || {'day':true, 'night':false}, 'pageFontSize' : getCookie('pageFontSize') || 20 }); MIP.watch('pageTheme', function(newValue){ setCookie('pageTheme', JSON.stringify(newValue)) }); MIP.watch('pageFontSize', function(newValue){ setCookie('pageFontSize', newValue) }); function setCookie(name, value){ var days = 1; var exp = new Date(); exp.setTime(exp.getTime() + days*24*60*60*1000); document.cookie = name + '=' + value + ';expires=' + exp.toUTCString(); } function getCookie(name){ var reg = new RegExp('(^| )' + name + '=([^;]*)(;|$)'); return document.cookie.match(reg) ? JSON.parse(document.cookie.match(reg)[2]) : null; }