1

I am trying to pass a variable as the param to vue-router so it can be set dynamically. Pretty much as the example below:

<router-link :to="{ 
  name: notification.name,
  (notification.param_name): notification.param_value
}">Blabla</router-link>

I am not looking at setting the key like this: var[notification.param_name].

I suppose this question could be expanded to a more general problem, but I am having a hard time explaining it in another way.

0

1 Answer 1

1

It'd be simplest to make it a computed which returns the definition of the route based on the notification object (note that you need to specify the params inside the params property of the object):

computed: {
  notificationRoute() {
    let { name, param_name, param_value } = this.notification;
    return { name, params: { [param_name]: param_value } };
  }
}

And bind that to the to:

<router-link :to="notificationRoute">Blabla</router-link>
Sign up to request clarification or add additional context in comments.

2 Comments

Thank you. The actual answer I was looking for was [param_name]:. Although, making the route computed seems wise!
Yeah as a general rule, if I'm passing anything inline to a component that needs to be more than one line, I define it as a computed property instead.

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.