Vue2純前端實現(xiàn)文字流式輸出效果
1、先展示一下效果啦

文字流輸出
2、首先確定接口返回的是字符串,那就先調(diào)取接口
// 由于需求關(guān)系 有很多個文本需要一起逐字輸出 所以這里就采用了Promise.all的形式
async getThreeTextarea() {
let taskArray = [ "重點項目文本","應用領(lǐng)域文本", "用途文本","成果需求文本", "問題描述","建議描述",]
await Promise.all(taskArray.map((task, index) => {
// 這里調(diào)取接口
const promise = this.getContentThree(this.orderId, task, index);
// 這里是為取消行為做準備
this.streamThreePromises.push(promise);
return promise;
}));
},
async getContentThree(id,task,index) {
// 如果取消了 就停止請求接口
if (!this.cancelTokenSource) return
let response = await axios.post(`${home}/model/workflowsIndexThree`, {
id: id,
taskt: task,
}, {
// axios提供的取消請求的示例
cancelToken: this.cancelTokenSource.token
});
if (typeof response.data !== 'string') return
// 將字符串中的\n和\n\n都替換成<br>標簽
let formattedData = response.data.replace(/\n\n/g, '<br>').replace(/\n/g, '<br>');
// 賦值給對應的字段
let contentLabels = ['zdxmycText', 'yylyycText', 'ytycText', 'cgxqycText', 'wtzjText', 'jyzjText']
let contentProperty = contentLabels[index]
if (!contentProperty) return
// 這里就開始調(diào)用 逐字輸出的方法啦
const promise = this.streamTextUpdate(contentProperty, formattedData);
this.streamThreePromises.push(promise);
await promise;
},
async streamTextUpdate(contentProperty, data) {
if (typeof data !== 'string') {
data = JSON.stringify(data) || '';
}
let text = "";
// 遍歷字符串 然后每加一個字符就更新一下視圖
for (let i = 0; i < data.length; i++) {
if (this.streamCancellationToken) {
break;
}
// String.charAt(index)是找到指定索引的字符 類似于Array[index]
text += data.charAt(i);
this.$set(this, contentProperty, text);
await this.$nextTick();
// 每隔50毫秒輸出一個字符
await new Promise(resolve => setTimeout(resolve, 50));
}
},
// 提示取消操作 嫌啰嗦 其實可以不要
handleBeforeUnload(event) {
if (this.cancelTokenSource) {
this.cancelTokenSource.cancel('中斷操作');
}
this.streamCancellationToken = true;
this.streamThreePromises.forEach(promise => {
if (promise.cancel) {
promise.cancel();
}
});
const message = '正在關(guān)閉頁面,未完成的操作將被取消。';
return this.$message.warning(message)
},3、獲取完數(shù)據(jù)就可以將數(shù)據(jù)渲染上去了
<div class="fx-content" style="height: 300px;overflow-y: auto;" v-html="zdxmycText"></div>
4、渲染成功后,如果文字流輸出還沒有遍歷完 就關(guān)閉頁面,這時候就出現(xiàn)一個問題,當你在打開的時候,文字就會像蹦迪一樣(忽閃忽閃的)這時候就用到了 我們剛剛提到取消請求和中斷輸出
頁面初始化時
if (!this.cancelTokenSource) {
// 在頁面初始化的時候 就實例化一下取消請求的實例
this.cancelTokenSource = axios.CancelToken.source();
// 監(jiān)聽是否要取消操作
window.addEventListener('beforeunload', this.handleBeforeUnload.bind(this));
}
this.streamCancellationToken = false;
頁面銷毀時
// 在頁面銷毀的時候 取消監(jiān)聽
window.removeEventListener('beforeunload', this.handleBeforeUnload.bind(this));
if (this.cancelTokenSource) {
this.cancelTokenSource.cancel('中斷操作');
}
this.streamCancellationToken = true;
// 然后取消所有的promise
this.streamPromises.forEach(promise => {
if (promise.cancel) {
promise.cancel();
}
});
到此這篇關(guān)于Vue2純前端實現(xiàn)文字流式輸出效果的文章就介紹到這了,更多相關(guān)Vue2流式輸出內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
詳解vue2.0 不同屏幕適配及px與rem轉(zhuǎn)換問題
這篇文章主要介紹了詳解vue2.0 不同屏幕適配及px與rem轉(zhuǎn)換問題,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-02-02
vue watch監(jiān)聽數(shù)據(jù)變化的案例詳解
監(jiān)聽數(shù)據(jù)變化,在Vue中是通過偵聽器來實現(xiàn)的,你也可以將它理解為監(jiān)聽器,時刻監(jiān)聽某個數(shù)據(jù)的變化,本文將通過代碼示例為大家詳細的介紹一下vue watch如何監(jiān)聽數(shù)據(jù)變化,需要的朋友可以參考下2023-07-07
基于VUE實現(xiàn)判斷設(shè)備是PC還是移動端
這篇文章主要介紹了基于VUE實現(xiàn)判斷設(shè)備是PC還是移動端,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下2020-07-07
vuedraggable拖拽到目標區(qū)域?qū)崿F(xiàn)過程解析
這篇文章主要為大家介紹了vuedraggable拖拽到目標區(qū)域?qū)崿F(xiàn)過程解析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2023-06-06

