我正在使用VUE 3、VUE路由和Pinia构建一个面包屑组件.我在具有label
和parent
属性的路由上使用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后将始终运行的函数中,但当子组件导航处于活动状态时,父路由组件将不会运行.