小编Jos*_*osh的帖子

Vue:无法在输入 vue-router 之前访问 Pinia Store

我正在使用 Vue 3,包括 Composition API,另外还使用 Pinia 作为状态管理。

在选项 API 中,有一个方法 beforeRouteEnter,该方法内置于组件本身中。不幸的是,这个方法在组合 API 中不存在。这里,本来应该在 beforeRouteEnter 方法中的代码被直接写入 setup 方法中。但是,这意味着首先加载并显示组件,然后执行代码,如果检查失败,则组件将被重定向到错误页面等。

我的想法是直接在路由的 beforeEnter 方法中的路由配置中进行检查。但是,我无法访问 Pinia Store,尽管它之前在 main.js 中调用过,但它似乎尚未初始化。

控制台日志

Uncaught Error: []: getActivePinia was called with no active Pinia. Did you forget to install pinia?
    const pinia = createPinia()
    app.use(pinia)
This will fail in production.
Run Code Online (Sandbox Code Playgroud)

路由器.js

import { useProcessStore } from "@/store/process";

const routes: Array<RouteRecordRaw> = [
{
    path: "/processes/:id",
    name: "ProcessView",
    component: loadView("ProcessView", "processes/"),
    beforeEnter: () => {
      const processStore = useProcessStore();
      console.log(processStore); …
Run Code Online (Sandbox Code Playgroud)

vue.js vue-router vuejs3 pinia

23
推荐指数
2
解决办法
3万
查看次数

标签 统计

pinia ×1

vue-router ×1

vue.js ×1

vuejs3 ×1