当前位置:首页 > VUE

vue实现高亮

2026-02-10 01:07:19VUE

Vue 实现高亮的方法

在 Vue 中实现高亮功能可以通过多种方式实现,以下是一些常见的方法:

使用 v-html 指令

通过 v-html 指令可以动态渲染包含 HTML 标签的字符串,从而实现高亮效果。需要确保内容的安全性,避免 XSS 攻击。

<template>
  <div v-html="highlightedText"></div>
</template>

<script>
export default {
  data() {
    return {
      text: 'This is a sample text to highlight',
      searchTerm: 'sample'
    };
  },
  computed: {
    highlightedText() {
      const regex = new RegExp(this.searchTerm, 'gi');
      return this.text.replace(regex, match => `<span class="highlight">${match}</span>`);
    }
  }
};
</script>

<style>
.highlight {
  background-color: yellow;
  font-weight: bold;
}
</style>

使用自定义指令

可以创建一个自定义指令来动态高亮匹配的文本。

<template>
  <div v-highlight="searchTerm">{{ text }}</div>
</template>

<script>
export default {
  data() {
    return {
      text: 'This is a sample text to highlight',
      searchTerm: 'sample'
    };
  },
  directives: {
    highlight: {
      inserted(el, binding) {
        const regex = new RegExp(binding.value, 'gi');
        el.innerHTML = el.textContent.replace(regex, match => `<span class="highlight">${match}</span>`);
      },
      update(el, binding) {
        const regex = new RegExp(binding.value, 'gi');
        el.innerHTML = el.textContent.replace(regex, match => `<span class="highlight">${match}</span>`);
      }
    }
  }
};
</script>

<style>
.highlight {
  background-color: yellow;
  font-weight: bold;
}
</style>

使用第三方库

可以使用第三方库如 mark.js 来实现更复杂的高亮功能。

<template>
  <div ref="content">{{ text }}</div>
</template>

<script>
import Mark from 'mark.js';

export default {
  data() {
    return {
      text: 'This is a sample text to highlight',
      searchTerm: 'sample'
    };
  },
  mounted() {
    this.highlightText();
  },
  watch: {
    searchTerm() {
      this.highlightText();
    }
  },
  methods: {
    highlightText() {
      const markInstance = new Mark(this.$refs.content);
      markInstance.unmark();
      markInstance.mark(this.searchTerm, {
        className: 'highlight'
      });
    }
  }
};
</script>

<style>
.highlight {
  background-color: yellow;
  font-weight: bold;
}
</style>

动态样式绑定

通过动态绑定样式类来实现高亮效果。

vue实现高亮

<template>
  <div>
    <span
      v-for="(word, index) in words"
      :key="index"
      :class="{ highlight: word === searchTerm }"
    >
      {{ word }}
    </span>
  </div>
</template>

<script>
export default {
  data() {
    return {
      text: 'This is a sample text to highlight',
      searchTerm: 'sample'
    };
  },
  computed: {
    words() {
      return this.text.split(' ');
    }
  }
};
</script>

<style>
.highlight {
  background-color: yellow;
  font-weight: bold;
}
</style>

注意事项

  • 使用 v-html 时需注意安全性,避免注入恶意代码。
  • 自定义指令适用于需要复用高亮逻辑的场景。
  • 第三方库如 mark.js 提供了更多高亮选项和灵活性。
  • 动态样式绑定适用于简单的文本高亮需求。

标签: vue高亮
分享给朋友:

相关文章

vue实现一个标签选择

vue实现一个标签选择

Vue 标签选择组件实现 基础实现 使用 Vue 的 v-for 和 v-model 指令可以快速实现标签选择功能: <template> <div class="tag-se…

vue实现一个tab栏

vue实现一个tab栏

Vue 实现 Tab 栏 基础实现(选项式 API) <template> <div class="tab-container"> <div class="t…

vue实现轮询

vue实现轮询

实现轮询的基本方法 在Vue中实现轮询可以通过setInterval或setTimeout配合递归调用完成。轮询通常用于定期向服务器请求数据更新。 使用setInterval的简单示例: data…

vue功能实现

vue功能实现

Vue 功能实现指南 Vue.js 是一个渐进式 JavaScript 框架,广泛用于构建用户界面。以下是 Vue 功能的常见实现方法。 数据绑定 Vue 的核心功能之一是数据绑定,通过 v-mod…

vue 实现过滤

vue 实现过滤

Vue 实现过滤的方法 在 Vue 中实现过滤功能可以通过多种方式完成,包括使用计算属性、自定义过滤器、第三方库等。以下是几种常见的实现方法: 使用计算属性进行过滤 计算属性是 Vue 中最常用的过…

vue实现接口

vue实现接口

在 Vue 中实现接口请求 Vue 本身不直接提供 HTTP 请求功能,通常需要借助第三方库如 axios 或 fetch 来实现接口请求。以下是几种常见方法: 使用 axios 发送请求 安装 a…