laravel 从 vue-router 的链接中获取 'id' 值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/35379777/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 13:14:10  来源:igfitidea点击:

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

javascriptphplaravelvue.js

提问by Muhammad Izzuddin Al Fikri

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

如何从 vue-router 的链接中获取对象值?例如此链接中的“id” http://laravel.dev/#!/messages/ 1703

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

因此,使用该 ID,我可以从数据库中获取特定数据。

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

抱歉,错误是点击链接,查看和 url 更改但发送到服务器的是 http://laravel.dev/api/messages/undefined

Here error screenshot

这里错误截图

Thanks

谢谢

回答by Ogie Sado

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

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

然后您可以访问 vm 中的任何地方 this.$route.params.message_id