【问题标题】:VueJs dynamically bind data to a routeVueJs 动态绑定数据到路由
【发布时间】:2018-08-06 16:10:39
【问题描述】:

我有一个包含所有试卷 ID 的对象,我也将这些 ID 呈现到表格中。 需要的是当有人点击Take Exam按钮时, 相关的paperid 应该传递给路由。 我试过了

this.$router.push('/startExam/{{paperid}}');

this.$router.push('/startExam/this.paperid');

但两者都不起作用。 如何动态地将数据传递给路由? 我的代码在这里。

<tbody>
      <tr v-for="paperid in QuestionPapersArray">
        <td>{{paperid}}</td>
        <td></td>
        <th></th>
        <th><button class="btn" @click="takeExam">Take Exam</button></th>
      </tr>
</tbody>

methods:{
    takeExam(){
      console.log("Inside the function");
      this.$router.push('/startExam');
    }
  }

【问题讨论】:

    标签: vue.js vue-router


    【解决方案1】:
    <tbody>
          <tr v-for="paperId in QuestionPapersArray">
            <td>{{ paperId }}</td>
            <td></td>
            <th></th>
            <th><button class="btn" @click="takeExam(paperId)">Take Exam</button></th>
          </tr>
    </tbody>
    

    试试下面的。

    methods: {
      takeExam (paperId) {
        this.$router.push(`/startExam/${paperId}`)
      }
    }  
    

    或者,您可以在takeExam() 方法中尝试以下操作

    this.$router.push({
      path: '/startExam',
      params: {
        id: paperId
      }
    })
    

    你的路由配置文件应该是path: '/startExam/:id'

    【讨论】:

    • ReferenceError: paperid 未定义。可能是表中有太多的纸张 ID。有什么方法可以将相关的论文 id 传递给方法吗?
    • 我已经修改了答案。
    • 接受答案并关闭此问题?
    猜你喜欢
    • 2018-02-06
    • 2021-02-24
    • 2020-12-13
    • 2019-03-02
    • 2021-10-02
    • 2018-03-26
    • 2020-02-27
    • 1970-01-01
    • 2018-01-31
    相关资源
    最近更新 更多