Get 'id' value from vue-router's link

20,467
router.map({
'/api/messages/:message_id': {
    name: 'sentMessage',
    component: detailMessage
    }
})

You can then access anywhere in the vm with this.$route.params.message_id

Share:
20,467
Muhammad Izzuddin Al Fikri
Author by

Muhammad Izzuddin Al Fikri

I code to life, not life to code. A math lover but believer of beliefs.

Updated on December 25, 2021

Comments

  • Muhammad Izzuddin Al Fikri
    Muhammad Izzuddin Al Fikri over 2 years

    How to get an object value from vue-router's link? For example 'id' in this link http://laravel.dev/#!/messages/1703

    So with that id i can fetch specific data from database.

    My code

    var detailMessage = Vue.extend({
    template: '#detailMessage',
    data: function() {
        return {
            id: ''
        }
    },
    ready: function(id) {
        this.getID(id);
    },
    methods: {
        getID: function(id) {
        this.$http.get('/api/messages/' + id, function(data) {
            alert(data);
                })
            }
        }
    })
    
    
    var router = new VueRouter()
    router.map({
    '/api/messages/:idMessage': {
        name: 'sentMessage',
        component: detailMessage
        }
    })
    

    Sorry, the error was when link clicked, view and url change but what send to server is http://laravel.dev/api/messages/undefined

    Here error screenshot

    Thanks

  • Arlo Guthrie
    Arlo Guthrie about 4 years
    What if the route is being added by router.push?
  • Arlo Guthrie
    Arlo Guthrie about 4 years
    Never mind, this site explains it clearly: router.vuejs.org/guide/essentials/navigation.html