代码之家  ›  专栏  ›  技术社区  ›  Alfred Balle

Vue.js、Vue路由器和参数/道具

  •  0
  • Alfred Balle  · 技术社区  · 7 年前

    我正在使用Vue路由器运行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调用中未使用。

    1 回复  |  直到 7 年前
        1
  •  2
  •   Sunny Sheth    7 年前

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