阅读(2599) (22)

Vite HMR API hot.accept(deps, cb)

2022-03-08 10:02:34 更新

模块也可以接受直接依赖项的更新,而无需重新加载自身:

import { foo } from './foo.js'

foo()

if (import.meta.hot) {
  import.meta.hot.accept('./foo.js', (newFoo) => {
    // 回调函数接收到更新后的'./foo.js' 模块
    newFoo.foo()
  })

  // 也可以接受一个依赖模块的数组:
  import.meta.hot.accept(
    ['./foo.js', './bar.js'],
    ([newFooModule, newBarModule]) => {
      // 回调函数接收一个更新后模块的数组
    }
  )
}