我正在使用VUE 3、VUE路由和Pinia构建一个面包屑组件.我在具有labelparent属性的路由上使用breadcrumbs元密钥.该组件通过使用router.resolve()递归地解析父路由来生成一个数组,然后从该数组创建组件标记.我使用Piniastore ,并且希望使用存储在Piniastore 中的属性在我的routes文件上动态生成meta.label属性.

如果我在routes文件中导入Piniastore ,如下所示:

// routes file
import { useRentalStore } from "@/stores/rentalStore";

const rentalStore = useRentalStore();

const routes = [
  // extract from routes array
  {
    path: ":equipment",
    name: "rental-show",
    component: ShowRentalView,
    meta: {
      breadcrumbs: {
        label: () => rentalStore.rental.label,
        parent: "rental-index",
      },
    },
  },
];

我得到以下错误:"getActivePinia()" was called but there was no active Pinia. Did you forget to install pinia? const pinia = createPinia()

我try 从主入口点main.js传递Pinia实例

// main.js
const pinia = createPinia()
const app = createApp(App)

app.use(pinia)
app.use(router)
//...
export { pinia }

// routes file
import {pinia} from '@/main.js'

const rentalStore = useRentalStore(pinia);

但这仍然行不通.我在Vue 2中对Vuex使用了类似的方法,上面的方法对Vuex有效,但不适用于Pinia.上面的代码返回以下错误消息:Cannot access 'pinia' before initialization.

你知道怎么从Routes文件访问Piniastore 吗?Pinia文档建议延迟useStore()的调用,方法是将其放置在安装Pinia后将始终运行的函数中,但当子组件导航处于活动状态时,父路由组件将不会运行.

推荐答案

Pinia创建存储函数而不是存储实例,以避免早期实例化.在组件外部使用时,应在使用存储区的位置调用useRentalStore:

...
label: () => useRentalStore().rental.label,
...

Javascript相关问答推荐

JavaScript .Click()函数不起作用

在JavaScript中检索一些文本

React:未调用useState变量在调试器的事件处理程序中不可用

按钮未放置在html dis位置

react/redux中的formData在expressjs中返回未定义的req.params.id

如何在Connect 4游戏中 for each 玩家使用位板寻找7形状?

将现场录音发送到后端

在拖放时阻止文件打开

html + java script!需要帮助来了解为什么我得到(无效的用户名或密码)

如何根据当前打开的BottomTab Screeb动态加载React组件?

我怎么在JS里连续加2个骰子的和呢?

Next.js服务器端组件请求,如何发送我的cookie token?

如何在使用rhandsontable生成表时扩展数字输入验证?

如何在Svelte中从一个codec函数中调用error()?

如何在DYGRAPS中更改鼠标事件和键盘输入

无法设置RazorPay订阅API项目价格

react -原生向量-图标笔划宽度

react 路由如何使用从加载器返回的数据

Chart.js Hover线条在鼠标离开时不会消失

更改管线时不渲染组件的react