Sunday, May 19, 2024
 Popular · Latest · Hot · Upcoming
49
rated 0 times [  55] [ 6]  / answers: 1 / hits: 6429  / 4 Years ago, sun, august 9, 2020, 12:00:00

I have a ProjectCard component which, among other things, links to a specific ProjectPage component. The ProjectPage is going to have a bunch of different paragraphs, pictures and so on, not wholly relevant for my question.


How do I pass on props to a ProjectPage? The Vue Docs portion on Boolean Mode is incredibly vague and doesn't really tell me anything useful.


My Router index.js file has the prop decoupling that they mention in the docs


import ProjectPage from "@/views/ProjectPage.vue"

const routes = [
{
path: "projects/:id",
component: ProjectPage,
props: true
}

To test things out, I made the ProjectPage.vue component look like this


<template>
<div>Static text, followed by {{ testString }}</div>
</template>

<script>
export default {
name: "ProjectPage",
props: {
testString: String
}
};
</script>

And I tried passing on a test string to it from my ProjectCard component like this:


<template>
<router-link class="project-link" :to="{ path: `projects/${url}`, params: { testString: 'a dynamic string' } }" exact>
</template>

<script>
export default {
name: "ProjectCard",
props: {
url: String
}
};
</script>

This will correctly take me to the specified route, eg projects/table. However, the {{ testString }} doesn't render and I can only see the Static text, followed by a portion of the component. There's no errors or warnings in the console.


Checking a ProjectCard component in the Vue debugger, the testString exists nested under to/params/testString:


Vue


However, on any of the ProjectPage components, testString is undefined


Vue


What am I doing wrong here?


More From » vue.js

 Answers
1

what about this


import ProjectPage from "@/views/ProjectPage.vue"

const routes = [
{
name: 'Project',
path: "projects/:id",
component: ProjectPage,
props: true
}

and


<template>
<router-link class="project-link" :to="{ name: `Project`, params: { testString: 'a dynamic string' ,id: url} }" exact>
</template>

As the docs said



params are ignored if a path is provided, which is not the case for query, as shown in the example above.



const userId = '123'
router.push({ name: 'user', params: { userId } }) // -> /user/123
router.push({ path: `/user/${userId}` }) // -> /user/123
// This will NOT work
router.push({ path: '/user', params: { userId } }) // -> /user


The same rules apply for the to property of the router-link component.



[#2942] Wednesday, August 5, 2020, 4 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
masonm

Total Points: 167
Total Questions: 87
Total Answers: 103

Location: Rwanda
Member since Wed, Jun 8, 2022
2 Years ago
;