我正在尝试通过Vuejs上的路由器链接将属性传递给组件,但是组件无法正确接收它们。当单击主菜单按钮时,我有一个主菜单和一个辅助菜单,我要在其中传递属性:
const Sidebar = {
name:'sidebar',
template:`
<div>
Sidemenu
<router-link v-for="route in routes" v-key="route" :to="route">{{route}}</router-link>
</div>`,
props: {
routes:{
type: Array,
required: true,
default: function(){return []}
}
}
}
const AppMenu = {
name:'app-menu',
template:`
<div>
<router-link :to="{name:'project', params:{routes: projectMenus}}">Project</router-link>
<router-link :to="{name:'assets', params:{routes: assetsMenus}}">Assets</router-link>
</div>`,
data: function(){
return{
projectMenus: ['overview', 'settings'],
assetsMenus: ['landscapes', 'characters']
}
}
}
const App = {
template:`
<div>
<app-menu/>
<router-view name="sideview"/>
<router-view name="sectionview"/>
</div>`,
components:{AppMenu}
};
const Routes= [
{path: '/', name:'home', redirect: 'project'},
{path:'/project', name:'project', components:{sideview:Sidebar}, props:{sideview:true}},
{path:'/assets', name:'assets', components:{sideview:Sidebar}, props:{sideview:true}}
]
Sidebar组件正在渲染,因为我可以在页面上看到“ Sidemenu”,但控制台会抛出[Vue warn]: Missing required prop: "routes" on Sidebar
,因此不会显示我的子菜单链接。我已经搜索了文档以及许多论坛中的许多类似问题,但是找不到我做错的事情。
答案 0 :(得分:1)
在vue路由定义中将props
设为对象时,它将替换组件props。您将它们设置为{sideview: true}
,它将覆盖Sidebar
组件中的所有道具。
至少这是我对https://router.vuejs.org/guide/essentials/passing-props.html#object-mode的理解