javascript - Getting NaN in URL even when right variable is passed via router-link
问题描述
I am using vue router-link to pass courseId and quizId to Quiz Component. When the Quiz component in initialized, it gets the courseId and quizId in the created() via this.$router.params.
The params are obtained in the first attempt, but when I refresh the page, the quizId params is set to NaN. Also only the courseId is passed in the URL of the component whereas the quizId is NaN. I don't understand what exactly the problem is. I will post the code below.
routes:
const routes = [
{
path: '',
component: Course,
name:'ViewCourse'
},
{
path: '/module/:course_id/:module_id',
component: Module,
name: 'ViewModule'
},
{
path: '/:course_id/:quiz_id',
component: Quiz,
name: 'QuizModule'
},
{
path: '/:course_id/:unit_id/:quiz_id',
component: QuizInstructions,
name: 'QuizInstructions'
}
]
QuizInstructions:
<router-link :to="{name: 'QuizModule', params: {course_id: courseId, quiz_id: quiz.id}}" class="btn btn-primary">Start Quiz</router-link>
Quiz:
created () {
const courseId = parseInt(this.$route.params.course_id)
const quizId = parseInt(this.$route.params.quiz_id)
// This condition fails as quizId is NaN on page refresh
if (courseId && quizId){
this.fetchQuestions(courseId, quizId)
}
},
Please help me in understanding what I am doing wrong. This used to work before now it is breaking and I am unable to find the issue. Please tell me if more content is required. I have only posted the code which I think is causing the issue.
Thanks.
Edit: Issue resolved. I accidentally did router.push() in one of the vuex action which resulted in setting quizId as NaN. It was because I was pushing wrong undefined variable in the URL.
解决方案
推荐阅读
- sql - 在日志文件中建立业务层次结构——用别的东西代替 CURSOR?
- android - android studio 3.1.2 gradle 失败
- python - 为什么我在尝试安装模块时收到错误“找不到模型:product.print.zpl.barcode”?
- html - 响应平方的图像,中间有一个重叠的图像
- power-automate - 创建 Microsoft Flow 自定义连接器失败并显示“上传文件失败”
- javascript - 如何格式化嵌套的json数组取决于api的条件请求json
- java - 如何使用 PixelReader 和 JavaFX 提取子图像?
- javascript - RxJS - 映射流的最后一个元素
- rest - 使用 REST API 和 powershell 向多个收件人发送电子邮件
- java - 如何在 JAR 中进行 JPA?