当前位置:首页 > VUE

vue实现ai逐字输出

2026-02-20 18:33:16VUE

实现思路

通过Vue的响应式数据和定时器,结合字符串分割与逐字拼接,模拟AI逐字输出的效果。核心是利用setInterval逐步更新显示文本,并通过v-text{{}}绑定到模板。

基础实现代码

<template>
  <div>
    <p>{{ displayedText }}</p>
  </div>
</template>

<script>
export default {
  data() {
    return {
      fullText: "这里是需要逐字输出的AI回答内容",
      displayedText: "",
      currentIndex: 0,
      typingSpeed: 100 // 控制输出速度(毫秒)
    };
  },
  mounted() {
    this.startTyping();
  },
  methods: {
    startTyping() {
      const timer = setInterval(() => {
        if (this.currentIndex < this.fullText.length) {
          this.displayedText += this.fullText.charAt(this.currentIndex);
          this.currentIndex++;
        } else {
          clearInterval(timer);
        }
      }, this.typingSpeed);
    }
  }
};
</script>

进阶优化版本

添加光标动画和中断逻辑,提升交互体验:

<template>
  <div>
    <p>
      {{ displayedText }}
      <span class="cursor" :class="{ blinking: isTyping }">|</span>
    </p>
    <button @click="stopTyping">停止输出</button>
  </div>
</template>

<script>
export default {
  data() {
    return {
      fullText: "进阶版:支持中断的逐字输出效果",
      displayedText: "",
      currentIndex: 0,
      typingSpeed: 80,
      isTyping: false,
      timer: null
    };
  },
  mounted() {
    this.startTyping();
  },
  methods: {
    startTyping() {
      this.isTyping = true;
      this.timer = setInterval(() => {
        if (this.currentIndex < this.fullText.length) {
          this.displayedText += this.fullText.charAt(this.currentIndex);
          this.currentIndex++;
        } else {
          this.stopTyping();
        }
      }, this.typingSpeed);
    },
    stopTyping() {
      clearInterval(this.timer);
      this.isTyping = false;
    }
  }
};
</script>

<style>
.cursor {
  color: #42b983;
  font-weight: bold;
}
.blinking {
  animation: blink 1s infinite;
}
@keyframes blink {
  0% { opacity: 1; }
  50% { opacity: 0; }
  100% { opacity: 1; }
}
</style>

动态内容处理

通过watch监听外部传入的内容变化,实现动态响应:

vue实现ai逐字输出

props: {
  aiResponse: {
    type: String,
    required: true
  }
},
watch: {
  aiResponse(newVal) {
    this.fullText = newVal;
    this.displayedText = "";
    this.currentIndex = 0;
    this.stopTyping();
    this.startTyping();
  }
}

性能注意事项

  1. 组件销毁前清除定时器
    beforeDestroy() {
    clearInterval(this.timer);
    }
  2. 长文本分段处理避免界面卡顿
  3. 使用requestAnimationFrame替代setInterval可获得更平滑的动画效果

标签: vueai
分享给朋友:

相关文章

vue实现iframe

vue实现iframe

Vue 中实现 iframe 的方法 在 Vue 项目中,可以通过多种方式实现 iframe 的嵌入。以下是几种常见的方法: 使用原生 HTML iframe 标签 直接在 Vue 组件的模板中使用…

vue实现xss

vue实现xss

XSS 攻击简介 XSS(跨站脚本攻击)是一种常见的安全漏洞,攻击者通过注入恶意脚本到网页中,当其他用户访问该页面时,脚本会在其浏览器中执行,可能导致数据泄露或会话劫持。 Vue 中的 XSS…

vue的实现

vue的实现

Vue 的实现原理 Vue 是一个渐进式 JavaScript 框架,其核心实现主要包括响应式系统、虚拟 DOM、模板编译和组件化机制。以下是 Vue 实现的主要技术细节。 响应式系统 Vue 的响…

vue实现水印

vue实现水印

Vue 实现水印的方法 使用 CSS 背景图 通过 CSS 的 background-image 和 background-repeat 属性实现水印效果。创建一个包含水印文本的 Canvas,将其转…

vue实现tree

vue实现tree

Vue 实现 Tree 组件 使用 Vue 实现 Tree 组件可以通过递归组件的方式来实现层级结构展示。以下是一个完整的实现方法: 基础递归组件实现 创建 Tree 组件文件 Tree.vue,使…

vue最佳实现

vue最佳实现

Vue 最佳实践 组件设计 组件应遵循单一职责原则,每个组件只负责一个功能模块。命名采用 PascalCase(如 UserProfile.vue),模板中使用 kebab-case(如 <us…