chatgpt的打字机的效果实现

<template>
  <div class="content1">
    <div v-html="searchStr" class="content"></div>
    <el-input v-model="searchs" @blur="search"></el-input>
  </div>
</template>

<script lang="ts" setup>
import { ref, onMounted, nextTick } from "vue";

import { marked } from "marked";
import Prism from "prismjs";
import hljs from "highlight.js";
import "highlight.js/styles/default.css"; // 选择你喜欢的样式
// import { log } from "console";
let searchs = ref("JavaScript写个拼图游戏");
let searchStr = ref();
let codeElement = ref(null);

onMounted(async () => {
  // await nextTick(); // 确保 DOM 更新完成
  getData(searchs.value);
});
/**
 * 
 * @param str 
 */
let getData = async (str) => {
  let res = await fetch(
    "你的后端的接口地址",
    {
      method: "POST",
      body: JSON.stringify("你后端的需要的参数"),
      headers: {
        "Content-Type": "application/json",
      },
    }
  );
  const reader = res.body.getReader();
  const decoder = new TextDecoder();
  let b: any = "";
  let readStream = () => {
    return reader.read().then(({ done, value }) => {
      if (done) {
        console.log("Stream finished");
        return;
      }
      const chunk: any = decoder.decode(value, { stream: true });
      console.log("Received chunk:", chunk);
      b = b + chunk;
      searchStr.value = marked(b);
      return readStream();
    });
  };
  readStream();
};

let search = (e) => {
  // console.log(e.target.value);
  getData(e.target.value);
};
</script>
<style>
.content1 {
  width: 100vw;
  height: 100vh;
  display: flex;
  flex-direction: column;
  align-items: center;

  /* justify-content: center; */
}
* {
  padding: 0;
  margin: 0;
}
.content {
  height: 800px;
  width: 800px;

  /* color: palevioletred; */
  /* background: black; */
  overflow: auto;
}
pre {
  background: radial-gradient(circle, #000000, #151414) !important;
}
code[class*="language-"] {
  /* background-color: black !important; */
  color: #dbdfe3 !important;
}
</style>


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值