【问题标题】:VueJS: how to write path with parameter correctlyVueJS:如何正确编写带参数的路径
【发布时间】:2019-03-20 14:53:41
【问题描述】:

我正在关注a tutorial,但带有参数的路径不起作用。

data() {
    return {
        id: this.$route.params.id,
        element: {
            title: '',
            description: '',
        }
    }
},
methods: {
        getBook() {
            const path = 'http://127.0.0.1:8000/api/v1/books/${this.id}/'
            axios.get(path).then((response) => {
                this.element.title = response.data.title
                this.element.description = response.data.description
        })
        .catch((error) => {
            console.log(error)
        })          
    },
    created() {
        this.getBook()
    }

在控制台中:

"GET /api/v1/books/$%7Bthis.id%7D/ HTTP/1.1" 404 2410

我所做的到底有什么问题?

【问题讨论】:

标签: vue.js vuejs2 axios


【解决方案1】:

你需要使用反引号``:

`http://127.0.0.1:8000/api/v1/books/${this.id}/`

或者只是:

'http://127.0.0.1:8000/api/v1/books/'+this.id

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-11-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-08-20
    • 1970-01-01
    相关资源
    最近更新 更多