将现有类星体应用的状态管理系统从vuex改为pinia



尝试了这个链接,并使用Pinia在Quasar创建了我的第一个商店,我还需要手动更改.quasar/app.js以添加Pinia商店并使Pinia功能。

import { Quasar } from 'quasar'
import { markRaw } from 'vue'
import RootComponent from 'app/src/App.vue'
import createStore from 'app/src/stores/index'
import createRouter from 'app/src/router/index'
export default async function (createAppFn, quasarUserOptions) {
// Create the app instance.
// Here we inject into it the Quasar UI, the router & possibly the store.
const app = createAppFn(RootComponent)
app.config.devtools = true

app.use(Quasar, quasarUserOptions)
const store = typeof createStore === 'function'
? await createStore({})
: createStore

app.use(store)
const router = markRaw(
typeof createRouter === 'function'
? await createRouter({store})
: createRouter
)
// make router instance available in store

store.use(({ store }) => { store.router = router })
// Expose the app, the router and the store.
// Note that we are not mounting the app here, since bootstrapping will be
// different depending on whether we are in a browser or on the server.
return {
app,
store,
router
}
}

但问题是.quasar/app.js被重写默认内容,只要quasar dev被执行,我再次无法访问Pinia商店了。

如我所说,这个应用程序以前是基于vuex的。

请确保您有pinia的索引文件。

在">

src/商店/index.js"

import { store } from 'quasar/wrappers'
import { createPinia } from 'pinia'
/*
* If not building with SSR mode, you can
* directly export the Store instantiation;
*
* The function below can be async too; either use
* async/await or return a Promise which resolves
* with the Store instance.
*/
export default store((/* { ssrContext } */) => {
const pinia = createPinia()
// You can add Pinia plugins here
// pinia.use(SomePiniaPlugin)
return pinia
})

尝试查看类星体信息

quasar info

注意@quasar/app-webpack和vuex.

如果你正在使用@quasar/app,试着升级到@quasar/app-webpack。

quasar upgrade -i

如果你在你的类星体信息输出中安装了vuex,试着删除它。

npm uninstall vuex

在您的包锁中。Json中,查找"node_modules/vuex"并删除键和值。

删除你的"node_modules"文件夹并运行npm i

之后,执行quasar clean

你可以尝试通过quasar命令创建一个Pinia store来验证它。

quasar new store <store_name>

它应该生成一个pinia store而不是vuex store。

问题是旧版本的@quasar/app-webpack包。它从v3.4.0开始支持Pinia。点击这里查看发行说明。所以基本上升级这个包。

先执行quasar upgrade -i,再执行quasar new store <store_name> [--format ts]它将用pinia创建一个stores/目录。

我的解决方案是删除并重新安装node_modules

在我的情况下,我不需要编辑任何特殊文件,只需替换stores文件夹中的index.js。为了让类星体CLI在运行quasar new store时使用pinia,我必须使用quasar clean,就像我已经完全过渡了。

最新更新