简体   繁体   English

vue.js,vue路由器和参数/属性

[英]vue.js, vue-router and parameters/props

I'm running Vue.js with vue-router, and are trying to pass parameters to my template. 我正在使用vue-router运行Vue.js,并尝试将参数传递给模板。

This is my route: 这是我的路线:

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

This is my template Car.vue : 这是我的模板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>

I'm getting the error: 我收到错误消息:

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

But the id is shown in h2 , but not used in the api call. 但是id显示在h2 ,但未在api调用中使用。

Use your props using this . 通过使用你的道具this this.id will solve your problem. this.id将解决您的问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM