Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

routing with params in vuejs using router-link

Tags:

vue.js

I am trying to build a spa using vuejs in which I have a list of users now when I click a user it should route to a new page with user details of the particular user how to params the user id in router-link

like image 506
Kushagra Agarwal Avatar asked Aug 17 '17 11:08

Kushagra Agarwal


People also ask

How do I use my vue3 Vue router?

Installing Vue Router for Vue 3 Like many frameworks, Vue has its own CLI. So at first, you need to install it using NPM or Yarn. Then you can create a new project using the vue create <project-name> command. After executing it, the CLI prompts with several options as follows.

What is router-link in Vue?

May 13, 2020. The router-link component creates an <a> tag that's configured to work correctly with Vue router.


3 Answers

You can pass params in router link using

<router-link :to="{ name: 'user', params: { userId: 123 }}">User</router-link>

Here "name" key parameter defines the name of the route and "params" key defines the parameters you need to send with that route.

If you need to use Route Path instead of Route name, You can use.

<router-link :to="{ path: 'home', params: { userId: 123 }}">Home</router-link>

Reference

like image 116
Shubham Patel Avatar answered Sep 23 '22 04:09

Shubham Patel


Looks like params: {...} doesn't work on non-named routes so to get that working I had to make my links like this:

<router-link :to="'/view-customer/' + customer.ID">
like image 34
Pylinux Avatar answered Sep 21 '22 04:09

Pylinux


With ES6 template literals:

<router-link :to="`/books/${book.id}`"></router-link>

like image 33
Expenzor Avatar answered Sep 23 '22 04:09

Expenzor