【问题标题】:vue.js, vue-router and parameters/propsvue.js、vue-router 和参数/道具
【发布时间】:2018-11-20 13:12:46
【问题描述】:

我正在使用 vue-router 运行 Vue.js,并尝试将参数传递给我的模板。

这是我的路线:

{ path: '/car/:id', component: car, props: true },

这是我的模板Car.vue:

<template>
    <h2>{{ id }}</h2>
</template>

<script>
    export default {
        props: ['id']
    },
    methods: {
        getCar: function () {
        axios.get('http://my-api/car/' + id)
            .then((response) => {
                this.car = response.data
            })
            .catch((error) => {
                console.log(error)
            })
    }
</script>

我收到了错误:

  ✘  http://eslint.org/docs/rules/no-undef  'id' is not defined

但是id显示在h2中,但没有在api调用中使用。

【问题讨论】:

  • 你应该使用this.id而不是id访问道具

标签: javascript vue.js vuejs2


【解决方案1】:

使用this 使用您的道具。 this.id 会解决你的问题。

【讨论】: