I'm building an app with laravel and VueJs and I was wondering how to pass a url parameter like the user slug or the user id to vuejs in a proper way to be able to use that parameter for making ajax requests?
For example when someone clicks on a link leading to
domain.com/user/john-appleseed
I want to make an ajax request within my vuejs application with the slug 'john-appleseed' as a parameter.
What is the "right/proper" way to this?
Thanks in advance!
You can get the url of the link you click and then use this url to do your ajax request.
And in your template call this method when the user click on a link:
I suppose you are building a component. Let's say, you have a button inside that component and when is clicked, you wanna perform an action (AJAX request, or just redirect).
In this case you pass the URL as parameter to the component and you can use it inside.
HTML:
Your component JS:
Then you can access the param in the component as
this.loginUrl
.Please notice that param name difference in HTML part as dash-notatinon and in JS as camelCase.
vue-router would probably be of help here. It lets you define your routes like so:
Within your
Profile
component, the requested user's slug would then become available underthis.$route.params.slug
Check the official documentation for details: http://router.vuejs.org/en/
If you are using vue-router and want to capture query parameters and pass them to props, you can do it this way.
Url being called : http://localhost:8080/#/?prop1=test1&prop2=test2
MyComponent.vue
router/index.js