HMR (热模块更新)

Pinia支持热模块更新,因此您可以直接在您的应用程序中修改您的stores并与它们进行交互,而无需重新加载页面,从而允许您保留现有的状态,添加,甚至删除stateactions,和getters

目前,官方只支持Vite,但任何执行import.meta.hot规范的打包器都应该能生效(webpack似乎使用import.meta.webpackHot而不是import.meta.hot)。您需要在任何store声明旁边添加这段代码。假设您有三个storesauth.js, cart.js, 和chat.js, 您必须在创建store定义后添加(并调整)它:

  1. // auth.js
  2. import { defineStore, acceptHMRUpdate } from 'pinia'
  3. const useAuth = defineStore('auth', {
  4. // options...
  5. })
  6. // make sure to pass the right store definition, `useAuth` in this case.
  7. if (import.meta.hot) {
  8. import.meta.hot.accept(acceptHMRUpdate(useAuth, import.meta.hot))
  9. }