简体   繁体   English

如何通过Vue路由器传递自定义属性

[英]How can I pass custom property via vue router

I have a route instance: 我有一个路由实例:

const router = new Router({
  routes: [
    {
      path: '/',
      name: 'Home',
      component: MainContainer,
      redirect: '/news/list',
      children: [
        {
          path: 'advertisement/create',
          name: 'Create Advertisement',
          title: 'This is title of the page'
          component: CreateAdvertisement
        }
      ]
    }
  ]
})

In my component, i tried to console this.$route but I got only name, path, component and there is no title property there. 在我的组件中,我尝试控制台this。$ route,但是我只有名称,路径,组件,并且那里没有title属性。 So my question is: Is this valid to pass a custom property via Vue router? 所以我的问题是:通过Vue路由器传递自定义属性是否有效? If there is, where the problem with my attemp? 如果存在,我的尝试问题在哪里?

You can use meta to set/get other data like below example in link 您可以使用meta来设置/获取其他数据,例如链接中的以下示例

Live Fiddle 现场小提琴

I am using this.$parent.title to change Title. 我正在使用this.$parent.title更改标题。

 const http404 = { template: '<div>http404 path is : {{$route.path}}</div>', mounted(){ console.log(this.$route.path); this.$parent.title ="http404 Page"; } } const index = { template: '<div>index path is : {{$route.path}}</div>', mounted(){ this.$parent.title ="Index Page"; console.log(this.$route.path); } } const panda = { template: '<div>panda path is : {{$route.path}}</div>', mounted(){ this.$parent.title ="panda Page"; console.log(this.$route.path); } } const router = new VueRouter({ mode: 'history', routes: [ { path: "/", name: "root", redirect: '/index' }, { path: "/index", name: "index", component: index }, { path: "/panda", name: "panda", component: panda }, //... { path: "**", name: "http404", component: http404 } ] }) new Vue({ router, el: '#app', data:{ title:"NA" } }) 
 <script src="https://unpkg.com/vue/dist/vue.js"></script> <script src="https://unpkg.com/vue-router/dist/vue-router.js"></script> <div id="app"> <div>Page : {{title}}</div> <router-link to="/">Root</router-link> | <router-link to="/index">Index</router-link> | <router-link to="/panda">Panda</router-link> | <router-link to="/http404">http404</router-link> <router-view></router-view> </div> 

You can add to the meta: 您可以添加到元数据:

 const router = new Router({
  routes: [
    {
      path: '/',
      name: 'Home',
      component: MainContainer,
      redirect: '/news/list',
      children: [
        {
          path: 'advertisement/create',
          name: 'Create Advertisement',
          title: 'This is title of the page'
          component: CreateAdvertisement,
          meta:{
           // here
           key: value
          }
        }
      ]
    }
  ]
})

https://router.vuejs.org/guide/advanced/meta.html https://router.vuejs.org/guide/advanced/meta.html

you can add the props for the custom property to the children component: 您可以将custom属性的props添加到children组件:

const router = new Router({
    routes: [
        {
            path: '/',
            name: 'Home',
            component: MainContainer,
            redirect: '/news/list',
            children: [
                {
                    path: 'advertisement/create',
                    name: 'Create Advertisement',
                    component: CreateAdvertisement,
                    props: {
                        title: 'This is title of the page'
                    }
                }
         ]
      }
   ]
})

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM