【发布时间】:2021-11-27 03:12:11
【问题描述】:
我有以下路由器配置,并希望将main.parent 路由的id 转换为整数,然后传递给所有子组件的子组件(给定props: true)。
{
path: '/',
component: GrandparentComponent,
name: 'main',
children: [{
path: ':id',
component: ParentComponent,
name: 'main.parent',
props: route => {
return {
// parse URL id to int
id: parseInt(route.params.id, 10)
};
},
children: [{
path: 'details',
name: 'main.parent.child',
component: ChildComponent,
props: true,
children: [{
...
}]
}]
}]
}
但是,似乎route function 只被调用一次(在评估/:id 时),而不是在评估/:id/details 时。 vue-router 中的相关代码似乎证实了这一点。
const route = routeToDisplay.value;
const matchedRoute = matchedRouteRef.value;
const ViewComponent = matchedRoute && matchedRoute.components[props.name];
// we need the value at the time we render because when we unmount, we
// navigated to a different location so the value is different
const currentName = props.name;
if (!ViewComponent) {
return normalizeSlot(slots.default, { Component: ViewComponent, route });
}
// props from route configuration
const routePropsOption = matchedRoute.props[props.name];
const routeProps = routePropsOption
? routePropsOption === true
? route.params
: typeof routePropsOption === 'function'
? routePropsOption(route)
: routePropsOption
: null;
...
const component = h(ViewComponent, assign({}, routeProps, attrs, {
onVnodeUnmounted,
ref: viewRef,
}));
...
我想知道是否有人尝试解决同样的问题,以及他们想出了什么解决方案。理想情况下,我想避免为每个子路由重复 props 函数。
【问题讨论】:
标签: javascript vue.js vue-router4