【问题标题】:Passing static props and dynamic params to Vue route with multiple components将静态道具和动态参数传递给具有多个组件的 Vue 路由
【发布时间】:2018-06-10 21:23:12
【问题描述】:

我设置了一条 Vue 路线,用于展示学校学位课程的课程。

{
  path: '/courses-by-degree/:degree',
  components: {
    sidebar: Sidebar,
    content: Grid
  },
  props: {
    sidebar: {
      title: "Fulfills",
      type: "all",
      degree: "",
      keyword: ''
    },
    content: {
      type: "degree",
      degree: "English",
      keyword: "",
      columns: {
        "Title": "title",
        "Term": "term_description",
        "Day, Time": "",
        "Format": "format",
        "Capacity": "availability"
      }
    }
  }
}

您可以通过 URL 或 Vue 多选访问它:

<multiselect 
  v-model="degree"
  placeholder="Select a field..."
  :options="degrees"
  @select="searchDegrees">
</multiselect>

当您在多选中选择一个选项时,这称为:

searchDegrees(selectedOption, id) {
  this.$root.$router.push({ path: `/courses-by-degree/${selectedOption}` });
},

我的问题是,如何将路径中的选定选项传递给路径中的道具,而不是像上面那样将其硬编码为“英语”?这甚至可能/一种好方法吗?

【问题讨论】:

标签: vue.js vue-router


【解决方案1】:

你是对的,你需要使用函数来返回道具。在多个命名组件的情况下,您可以这样做:

{
  path: '/courses-by-degree/:degree',
  components: {
    sidebar: Sidebar,
    content: Grid
  },
  props: {
    sidebar: {
      title: "Fulfills",
      type: "all",
      degree: "",
      keyword: ""
    },
  content: route => {
    return {
      type: "degree",
      degree: route.params.degree,
      keyword: "",
      columns: {
        Title: "title",
        Term: "term_description",
        "Day, Time": "",
        Format: "format",
        Capacity: "availability"
      }
    }
  }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-12-10
    • 2018-08-07
    • 1970-01-01
    • 2018-08-19
    • 2020-04-03
    • 2021-07-08
    • 2020-05-09
    相关资源
    最近更新 更多