vue3深入學(xué)習(xí)?nextTick和historyApiFallback
1、nextTick
官方解釋:將回調(diào)推遲到下一個 DOM 更新周期之后執(zhí)行。在更改了一些數(shù)據(jù)以等待 DOM 更新后立即使用它。
比如我們有下面的需求:
- 點(diǎn)擊一個按鈕,我們會修改在h2中顯示的message;
- message被修改后,獲取h2的高度;
<template>
<div>
<h3 class="title" ref="title">{{ message }}</h3>
<button @click="handleClick">添加內(nèi)容</button>
</div>
</template>
<script setup>
import { ref } from 'vue'
const message = ref('')
const title = ref(null)
const handleClick = () => {
message.value += 'NextTick!'
console.log(title.value.offsetHeight)
}
</script>
<style scoped>
.title {
width: 100px;
word-break: break-all;
}
</style>
可以看到,上面每次打印的都是上一次元素內(nèi)容的高度
實(shí)現(xiàn)上面的案例我們有三種方式:
- 方式一:在點(diǎn)擊按鈕后立即獲取到h2的高度(錯誤的做法)
- 方式二:在updated生命周期函數(shù)中獲取h2的高度(但是其他數(shù)據(jù)更新,也會執(zhí)行該操作)
<template>
<div>
<h3 class="title" ref="title">{{ message }}</h3>
<button @click="handleClick">添加內(nèi)容</button>
</div>
</template>
<script setup>
import { ref, onUpdated } from 'vue'
const message = ref('')
const title = ref(null)
const handleClick = () => {
message.value += 'NextTick!'
}
onUpdated(() => {
console.log(title.value.offsetHeight)
})
</script>
<style scoped>
.title {
width: 100px;
word-break: break-all;
}
</style>
方式三: 使用nextTick函數(shù);
nextTick是如何做到的呢?
<template>
<div>
<h3 class="title" ref="title">{{ message }}</h3>
<button @click="handleClick">添加內(nèi)容</button>
</div>
</template>
<script setup>
import { ref, nextTick } from 'vue'
const message = ref('')
const title = ref(null)
const handleClick = () => {
message.value += 'NextTick!'
// 更新 DOM
nextTick(() => {
console.log(title.value.offsetHeight)
})
}
</script>
<style scoped>
.title {
width: 100px;
word-break: break-all;
}
</style>
2、historyApiFallback
1.historyApiFallback是開發(fā)中一個非常常見的屬性,它主要的作用是解決SPA頁面在路由跳轉(zhuǎn)之后,進(jìn)行頁面刷新時,返回404的錯誤。
2.boolean值:默認(rèn)是false
- 如果設(shè)置為true,那么在刷新時,返回404錯誤時,會自動返回 index.html 的內(nèi)容;
3.object類型的值,可以配置rewrites屬性:
- 可以配置from來匹配路徑,決定要跳轉(zhuǎn)到哪一個頁面;
4.事實(shí)上devServer中實(shí)現(xiàn)historyApiFallback功能是通過connect-history-api-fallback庫的:
可以查看文檔

代碼在vue-cli腳手架項(xiàng)目的node_modules/@vue/cli-service/lib/commands/serve.js中:
const server = new WebpackDevServer(compiler, Object.assign({
historyApiFallback: {
disableDotRule: true
}
}))現(xiàn)在已經(jīng)是vite打包工具了,上面是webpack的配置
自己配置可以在項(xiàng)目根目錄下創(chuàng)建一個vue.config.js文件,在這個文件中進(jìn)行配置:
module.exports = {
configureWebpack: {
devServer: {
historyApiFallback: true
}
}
}到此這篇關(guān)于vue3深入學(xué)習(xí) nextTick和historyApiFallback的文章就介紹到這了,更多相關(guān)vue3 nextTick和historyApiFallback內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Vue項(xiàng)目中input框focus時不調(diào)出鍵盤問題的解決
這篇文章主要介紹了Vue項(xiàng)目中input框focus時不調(diào)出鍵盤問題的解決,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-04-04
何時/使用 Vue3 render 函數(shù)的教程詳解
這篇文章主要介紹了何時/使用 Vue3 render 函數(shù)的教程詳解,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-07-07
vue實(shí)現(xiàn)簡單loading進(jìn)度條
這篇文章主要為大家詳細(xì)介紹了vue實(shí)現(xiàn)簡單loading進(jìn)度條,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下2018-06-06
解決IE11 vue +webpack 項(xiàng)目中數(shù)據(jù)更新后頁面沒有刷新的問題
今天小編就為大家分享一篇解決IE11 vue +webpack 項(xiàng)目中數(shù)據(jù)更新后頁面沒有刷新的問題,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-09-09
Vue使用echarts散點(diǎn)圖在區(qū)域內(nèi)標(biāo)點(diǎn)
這篇文章主要為大家詳細(xì)介紹了Vue使用echarts散點(diǎn)圖在區(qū)域內(nèi)標(biāo)點(diǎn),文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下2022-03-03

