admin管理员组文章数量:1334692
Im using Vue Router. In my code I used to have:
<div v-bind:is="pageponent_name" v-bind:page="page"></div>
Which worked, and the page
data was passed to the ponent. But how do I do the same with a router-view? This doesn't seem to work:
<router-view v-bind:page="page"></router-view>
js:
var vm = new Vue({
...,
router : new VueRouter({
routes : [
{ path: '/foo', ponent: { template: '<div>foo</div>', created:function(){alert(1);} } },
//{ path: '/bar', ponent: { template: '<div>bar</div>', created:function(){alert(2);} } },
{ path: '/bar', ponent: Vueponent("ti-page-report") }
]
}),
...
});
Im using Vue Router. In my code I used to have:
<div v-bind:is="page.ponent_name" v-bind:page="page"></div>
Which worked, and the page
data was passed to the ponent. But how do I do the same with a router-view? This doesn't seem to work:
<router-view v-bind:page="page"></router-view>
js:
var vm = new Vue({
...,
router : new VueRouter({
routes : [
{ path: '/foo', ponent: { template: '<div>foo</div>', created:function(){alert(1);} } },
//{ path: '/bar', ponent: { template: '<div>bar</div>', created:function(){alert(2);} } },
{ path: '/bar', ponent: Vue.ponent("ti-page-report") }
]
}),
...
});
Share
Improve this question
edited Oct 28, 2018 at 18:05
Cortex0101
9272 gold badges13 silver badges33 bronze badges
asked Oct 28, 2018 at 18:03
omegaomega
44k90 gold badges285 silver badges522 bronze badges
3 Answers
Reset to default 2vue-router
has a dedicated page in docs on how to pass props to router-view
.
Passing Props to Route Components
Example snippet from docs:
const router = new VueRouter({
routes: [
{ path: '/user/:id', ponent: User, props: true },
// for routes with named views, you have to define the `props` option for each named view:
{
path: '/user/:id',
ponents: { default: User, sidebar: Sidebar },
props: { default: true, sidebar: false }
}
]
})
If you are looking for simplified usage, props
can still be passed the same way they are passed to any ponent. But ponent that is used for rendering the route (the one that is specified in route definition) should expect to receive the props.
Here is simple usage example of passing props
to router-view
:
I personally decided to use provide/inject feature: preserving reactivity with minimal overhead.
The ponent ("ti-page-report") that needs to access the props being sent just needs to add it:
<template>
<div>
<h1>Now you can access page: {{ page }}</h1>
</div>
</template>
export default {
name: "TiPageReport",
props: ['page'], // can now be accessed with this.page
...
};
See https://v2.vuejs/v2/guide/ponents-props.html for how to use props properly.
本文标签: javascriptHow to pass data to vue router routerviewStack Overflow
版权声明:本文标题:javascript - How to pass data to vue router router-view? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742366257a2461329.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论