当前位置:首页 > 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按钮实现截屏

vue按钮实现截屏

Vue 按钮实现截屏的方法 在 Vue 项目中实现截屏功能可以通过多种方式完成,以下是几种常见的方法: 使用 html2canvas 库 安装 html2canvas 库: npm ins…

vue实现展开与收起

vue实现展开与收起

Vue 实现展开与收起功能 在 Vue 中实现展开与收起功能可以通过多种方式完成,以下是一些常见的方法: 使用 v-show 或 v-if 控制显示 通过 Vue 的指令 v-show 或 v-if…

vue  select实现

vue select实现

Vue Select 实现方法 在 Vue 中实现下拉选择功能可以使用原生 <select> 标签或第三方库如 vue-select。以下是两种方法的详细说明: 原生 HTML Sele…

vue实现triger

vue实现triger

在Vue中实现触发器(trigger)功能通常涉及自定义事件或DOM事件触发。以下是几种常见场景的实现方法: 自定义事件触发 通过$emit方法触发父组件中监听的自定义事件: // 子组件 thi…

vue 实现递归

vue 实现递归

Vue 实现递归组件的方法 在 Vue 中实现递归组件通常用于处理树形结构数据,例如菜单、评论列表或文件目录。以下是实现递归组件的关键步骤: 定义递归组件 递归组件需要设置 name 属性,以便在模…

vue实现评分

vue实现评分

Vue 实现评分功能 使用组件库实现 安装 element-ui 或 ant-design-vue 这类 UI 库,它们已经内置了评分组件。 以 element-ui 为例: <templa…