javascript - 我如何通过vue路由器传递自定义属性

标签 javascript vue.js vue-router

我有一个路由实例:

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
        }
      ]
    }
  ]
})

在我的组件中,我试图控制 this.$route 但我只得到名称、路径、组件并且那里没有标题属性。所以我的问题是:通过 Vue 路由器传递自定义属性是否有效?如果有,我尝试的问题在哪里?

最佳答案

您可以添加到元数据中:

 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

关于javascript - 我如何通过vue路由器传递自定义属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52776069/

相关文章:

javascript - 表打开时应用事件状态

javascript - 如何以编程方式检测对象是否为 jQuery 对象?

javascript - 如何以编程方式创建 Vue.js 插槽?

vue.js - 在vuejs中单击浏览器后退按钮时如何关闭模式弹出窗口?

node.js - Express 抛出 404 url​​ 中是否有点 - Vue、webpack

vue.js - Nuxt 中的动态嵌套路由

javascript - 对象 : null prototype does not allow to retrieve a value

javascript - vuejs 如果/否则 : check if element exsists else add a element

javascript - fullcalendar.js 插件上的单击事件不起作用

vue.js - Vue test-utils 如何测试 router.push()