N
N
Nightmare0582020-11-16 08:03:01
Vue.js
Nightmare058, 2020-11-16 08:03:01

How to make vue axios dynamic url?

Good afternoon.
I need to make an API call to Laravel, passing the post id in the URL. How can I pass it to axios? Below is an example of my code.

import axios from 'axios';
    export default {
        data(){
            return {
                id: 1
            }
        },
        mounted() {
            this.getData()
        },
        methods: {
            getData() {
                axios.get('/api/notes/$id')
                .then(res => {
                    console.log(res.data)
                })
            }
        }
    }

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
Alexey Yarkov, 2020-11-16
@Nightmare058

`/api/notes/${this.id}`

N
Nightmare058, 2020-11-16
@Nightmare058

I solved it like this, but something tells me that there is a more elegant solution.

getData() {
                this.url = this.url + this.id;
                axios.get(this.url)
                .then(res => {
                    console.log(res.data)
                })
            }

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question