4

I'm running Vue.js and have a for-loop and a trying to generate links with href like /car/xyz:

<router-link to="/car/+ car.id">{{ car.id }}</router-link>

But it fails to render correctly, as I guess I'm appending the car.id wrong to the to value?

3 Answers 3

12

Just replace to with :to and format value as a string:

<router-link :to="'/car/' + car.id">{{ car.id }}</router-link>
Sign up to request clarification or add additional context in comments.

Comments

0

Check out the documentation; the recommended way is to bind the path as an object.

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

6 Comments

Doesn't seem to work <router-link :to="{ name: 'car', params: { id: car.id }}">{{ car.id }}</router-link>
Are you getting any errors? How does your routes array look?
{ path: '/car/:id', component: CAR, props: true }
@AlfredBalle If you use this option, the route has to define the name property.
Exactly, so { path: '/car/:id', component: CAR, name: 'car', props: true } should work ;)
|
0

I think it's better to write like this...

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

for template literals: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Template_literals

for binding in vue: https://vuejs.org/guide/essentials/class-and-style

1 Comment

As it’s currently written, your answer is unclear. Please edit to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers in the help center.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.