教你巧用?import.meta?實(shí)現(xiàn)熱更新的問題
import.meta
import.meta 是一個(gè)給 JavaScript 模塊暴露特定上下文的元數(shù)據(jù)屬性的對象,它包含了這個(gè)模塊的信息。
import.meta 對象是由 ECMAScript 實(shí)現(xiàn)的,它帶有一個(gè) null 的原型對象。這個(gè)對象可以擴(kuò)展,并且它的屬性都是可寫,可配置和可枚舉的。
<script type="module">
console.log(import.meta) // {url: 'http://127.0.0.1:5500/dist/index.html?a=1'}
</script>它返回一個(gè)帶有 url 屬性的對象,指明模塊的基本 URL。也可以是外部腳本的 URL,還可以是內(nèi)聯(lián)腳本所屬文檔的 URL。注意,url 也可能包含參數(shù)或者哈希(比如后綴?或#)

應(yīng)用場景
import.meta.hot
Pinia 是 vuex 新替代方案。Pinia 中熱更新實(shí)現(xiàn),借助 import.meta。
import { defineStore, acceptHMRUpdate } from 'pinia'
const useAuth = defineStore('auth', {
// options...
})
// make sure to pass the right store definition, `useAuth` in this case.
if (import.meta.hot) {
import.meta.hot.accept(acceptHMRUpdate(useAuth, import.meta.hot))
}
Vite 通過特殊的 import.meta.hot 對象暴露手動 HMR API。
https://github.com/vitejs/vite/blob/main/packages/vite/src/client/client.ts#L412
interface ImportMeta {
readonly hot?: {
readonly data: any
accept(): void
accept(cb: (mod: any) => void): void
accept(dep: string, cb: (mod: any) => void): void
accept(deps: string[], cb: (mods: any[]) => void): void
prune(cb: () => void): void
dispose(cb: (data: any) => void): void
decline(): void
invalidate(): void
on(event: string, cb: (...args: any[]) => void): void
}
}
vite HMR API:https://cn.vitejs.dev/guide/api-hmr.html
import.meta.webpackHot
module.hot 的一個(gè)別名,strict ESM 中可以使用 import.meta.webpackHot 但是不能使用 module.hot。
在 package.json 中設(shè)置
"type": "module"會強(qiáng)制 package.json 下的所有文件使用 ECMAScript 模塊
vuex 借助 webpack 模塊熱替換:https://webpack.docschina.org/guides/hot-module-replacement/
vuex 提供 hotUpdate 方法:https://github.com/vuejs/vuex/blob/HEAD/src/store.js#L242-L243
if (import.meta.webpackHot) {
// 使 action 和 mutation 成為可熱重載模塊
import.meta.webpackHot.accept(['./mutations', './modules'], () => {
// 獲取更新后的模塊
// 因?yàn)?babel 6 的模塊編譯格式問題,這里需要加上 `.default`
const newMutations = require('./mutations').default
const newModules = require('./modules').default
// 加載新模塊
store.hotUpdate({
mutations: newMutations,
modules: {
...newModules
}
})
})
}
URL()
URL() 構(gòu)造函數(shù)返回一個(gè)新創(chuàng)建的 URL 對象,表示由一組參數(shù)定義的 URL。
const url = new URL(url [, base])
url是一個(gè)表示絕對或相對 URL 的 DOMString。如果url是相對 URL,則會將base用作基準(zhǔn) URL。如果url是絕對URL,則無論參數(shù)base是否存在,都將被忽略。base可選。是一個(gè)表示基準(zhǔn) URL 的 DOMString,在 url 是相對 URL 時(shí),它才會起效。如果未指定,則默認(rèn)為''。
二者結(jié)合使用
new URL('./relative-path', import.meta.url)
示例:獲取圖片
function loadImg (relativePath) {
const url = new URL(relativePath, import.meta.url)
const image = new Image()
image.src = url
return image
}
loadImg('../../images/1.jpg')
無需關(guān)心路徑問題,自動根據(jù)當(dāng)前 URL 進(jìn)行轉(zhuǎn)換。
到此這篇關(guān)于巧用 import.meta 實(shí)現(xiàn)熱更新的文章就介紹到這了,更多相關(guān)import.meta熱更新內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
原生JavaScript實(shí)現(xiàn)滾動條效果
這篇文章主要介紹了原生JavaScript實(shí)現(xiàn)滾動條效果的相關(guān)資料,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-01-01
JavaScript html5 canvas繪制時(shí)鐘效果(二)
這篇文章主要介紹了JavaScript html5繪制時(shí)鐘效果的相關(guān)資料,使用HTML5的canvas標(biāo)簽和Javascript腳本,模擬顯示了一個(gè)時(shí)鐘,感興趣的小伙伴們可以參考一下2016-03-03
基于JavaScript實(shí)現(xiàn)報(bào)警器提示音效果
這篇文章給大家分享分享一段代碼基于JavaScript實(shí)現(xiàn)報(bào)警器提示音效果,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友參考下吧2017-10-10
bootstrap日期插件daterangepicker使用詳解
這篇文章主要為大家詳細(xì)介紹了bootstrap日期插件daterangepicker的使用方法,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-10-10
基于JavaScript代碼實(shí)現(xiàn)pc與手機(jī)之間的跳轉(zhuǎn)
本文通過一段代碼實(shí)例給大家介紹pc跳轉(zhuǎn)手機(jī)代碼,手機(jī)跳轉(zhuǎn)pc網(wǎng)站代碼的相關(guān)知識,對js跳轉(zhuǎn)代碼相關(guān)知識感興趣的朋友一起通過本篇文章學(xué)習(xí)吧2015-12-12

