通过程序化导航Vue.js传递道具

JavaScript

Gil乐

2020-03-12

我有一个Vue组件,它的道具名为“ title”,例如:

<script>
export default {
  props: ['title'],
  data() {
    return {
    }
  }
}
</script>

在完成某些操作后,我将以编程方式导航至组件。是否可以通过编程方式路由用户,同时设置prop值?我知道您可以创建这样的链接:

<router-link to="/foo" title="example title">link</router-link>

但是,是否可以执行以下操作?

this.$router.push({ path: '/foo', title: 'test title' })

编辑:

根据建议,我已将路线更改为以下内容:

   {
      path: '/i/:imageID',
      component: Image,
      props: true
    }

并导航到以下内容:

this.$router.push({ path: '/i/15', params: {title: 'test title' }})

但是,我的Image组件(模板-见下文)仍然没有显示任何标题。

<h1>{{ title}}</h1>

有什么可能引起问题的吗?

第1009篇《通过程序化导航Vue.js传递道具》来自Winter(https://github.com/aiyld/aiyld.github.io)的站点

1个回答
阿飞蛋蛋 2020.03.12

在路由器中定义的子路由遇到相同的问题。下面的router.js显示映射到named的子路由

<router-view name="create"></router-view>
<router-view name="dashboard"></router-view>

router.js

    {
      path: "/job",
      name: "Job",
      component: () => import("./views/JobPage"),
      children: [
        {
          path: "create",
          name: "JobCreate",
          components: {
            create: JobCreate
          }
        },
        {
          path: ":id",
          name: JobConsole,
          components: {
            dashboard: JobConsole
          }
        }
      ]
    },

当我尝试从create传递道具时,vue-router无法捕获JobConsole所需的动态路线匹配:

      this.$router.push(
        {
          name: "Job",
          params: {
            id: this.ID_From_JobCreate
          }
      )

问题类别

JavaScript Ckeditor Python Webpack TypeScript Vue.js React.js ExpressJS KoaJS CSS Node.js HTML Django 单元测试 PHP Asp.net jQuery Bootstrap IOS Android