【问题标题】:Convert Vue route param title to id将 Vue 路由参数标题转换为 id
【发布时间】:2020-12-12 01:58:23
【问题描述】:

我正在创建一个课程网站,每个课程都有自己的页面。 假设我打开了 1 号课程,那么链接是 http://localhost:8080/course/1 。我想将其更改为 http://localhost:8080/course/course-name-here 。我的问题是,当我更改参数时,没有从 API 中获取数据。

index.js

{
    path: '/courses',
    name: 'Courses',
    component: () => import(/* webpackChunkName: "about" */ '../views/Courses.vue')
},
{
    path: '/course/:id',
    name: 'CourseDetail',
    props: true,
    component: () => import(/* webpackChunkName: "about" */ '../views/CourseDetail.vue')
}

课程列表.vue

<v-row>
   <v-col sm="6" md="4" v-for="course in courses" v-bind:key="course.courseId">
      <router-link v-bind:to="{name: 'CourseDetail', params: {id: course.courseTitle}}" class="text-decoration-none">
         <VerticalCard v-bind:course="course"/>
      </router-link>
   </v-col>
</v-row>

CourseDetail.vue(脚本)

import axios from 'axios'
export default {
  props:['id'],
  data: function(){
    return {
      singleCourse: null,
      selectedIndex: null,
      showPage: false
    }
  },
  methods: {
    getData() {
      var that = this
      axios.get('http://my-api-here.amazonaws.com/v1/api/course?id=' + this.id,
            {
              headers: {
                'Authorization' : 'Bearer ' + this.$store.state.authKey 
              }
            })
            .then(function(response) {
              that.singleCourse = response.data.body
            })
      },
      show() {
        if(this.$store.state.isLogin == true){
          this.showPage = true
        }
        else {
          this.$router.replace('/login')
        }
      }
  },
  mounted: function() {
    this.getData()
    this.show()
  }
}

单门课程示例

{ "_id": { "$oid": "5fc63dab36d8491476999831" }, 
"courseTitle": "Python For Everybody", 
"createdDate": { "$date": 1606852635135 }, 
"creator": "Admin", 
"courseId": 1, 
"courseDesc": "Description Lorem Ipsum is simply dummy text of the printing", 
"courseCategory": "Programming", 
"courseImage": "https://cwpwp2.betterthanpaper.com/wp-content/uploads/2019/06/2-1-1.jpg", 
"syllabus": [ { "chapterName": "Introduction to Python 3 Programming Tutorial", "chapterVideo": "eXBD2bB9-RA" }] }

【问题讨论】:

  • API 似乎需要id 参数,那么如果你停止传递它会如何工作?
  • 是的,我知道。这就是为什么我对如何从 API 获取标题感到困惑(因为 API 需要 id),然后将其设置为路由参数。还是真的不能这样做? @丹

标签: javascript vue.js vuejs2 vue-component vue-router


【解决方案1】:

使用对象通过名称查找每门课程的 ID。传递title 参数而不是id(在路由、链接和道具中更改为title)。在组件中:

data() {
  return {
    courseIds: {
      'course-name-here': 1,
      'other-course': 2,
      'one-more': 3
    }
  }
}

getData()中使用这个:

getData() {
   ...
   const url = 'http://my-api-here.amazonaws.com/v1/api/course?id=';
   axios.get(url + this.courseIds[this.title])
   ...
}

如果其他模块/组件需要访问它,您可以将查找数据保留在 Vuex 中。

【讨论】:

  • 感谢您的回答!但这是否意味着我必须对所有 courseIds 进行硬编码?
  • 不客气。查找需要在某个地方,但不一定在应用程序中。也许您的 api 提供了一个列表,您可以将其转换为该格式。如果您愿意,甚至可以将其保存在 JSON 文件中
  • 哦,我明白了。不确定我是否会接受它,因为我的网站还具有供用户发布课程的功能。我不可能硬编码所有的课程。但是,非常感谢您的解释。非常感谢!
  • 应该没问题。您的系统会在他们发布课程时生成一个 ID,并且您将标题和 ID 输入到数据库/JSON 文件等中。它不必进行硬编码。但是在某个地方,应用程序需要能够从名称中获取 id,或者无法将 id 传递给 api
猜你喜欢
  • 1970-01-01
  • 2020-07-03
  • 1970-01-01
  • 1970-01-01
  • 2016-01-22
  • 2023-03-14
  • 2018-09-30
  • 1970-01-01
  • 2011-05-05
相关资源
最近更新 更多