当前位置:首页 > 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监听外部传入的内容变化,实现动态响应:

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可获得更平滑的动画效果

vue实现ai逐字输出

标签: vueai
分享给朋友:

相关文章

vue实现缩放

vue实现缩放

Vue 实现缩放的方法 在 Vue 中实现缩放功能可以通过多种方式完成,以下是一些常见的方法: 使用 CSS transform 属性 通过 CSS 的 transform: scale() 属性可…

vue实现订单

vue实现订单

Vue 实现订单功能 订单数据结构设计 订单数据通常包含以下字段: order: { id: String, // 订单ID userId: String,…

实现vue table

实现vue table

Vue 表格实现方法 基础表格实现 使用 Vue 的 v-for 指令可以快速渲染表格数据。在模板中定义表格结构,通过数据绑定动态生成行和列。 <template> <tab…

vue 实现href

vue 实现href

Vue 实现 href 的方法 在 Vue 中实现 href 功能可以通过多种方式,具体取决于需求场景。以下是几种常见的方法: 使用 <a> 标签 直接使用 HTML 的 <a&g…

vue实现123456

vue实现123456

Vue 实现数字 123456 的显示 在 Vue 中显示数字 123456 非常简单,可以通过以下方法实现: 方法一:直接在模板中显示 <template> <div&…

vue实现日历

vue实现日历

Vue 实现日历的基本方法 使用 Vue 实现日历可以通过自定义组件或借助第三方库完成。以下是两种常见实现方式: 自定义日历组件 创建基于 Vue 的日历组件需要处理日期计算和渲染逻辑。核心是生成…