Vue is there a way to pass data from route to component?












0















I am using Vue.js 2.0, and I have this exact same code in 2 differents files, so I decided to build only one component and redirect the 2 routes on it, then I just need to pass the ID to the route, this way my 2 components can display differents resultats.



Note: the only thing that changes is the ID dsgh151rhj12t1j5j I would like that each route can send it own ID to my PageContentfulView component



EDIT: I just want to pass data from Route to the component



<template>
<div>
<div class="container">
<hp-row>
<hp-column>
<component v-for="component in content.column" :data="component" :key="component.id" :is="getComponentIdentifier(component.is)"></component>
</hp-column>
</hp-row>
</div>
</div>
</template>

<script>
import ModularView from '@/views/ModularView'

export default {
name: 'PageContentfulView',
mixins: [ModularView],

created () {
this.fetch('blocks/dsgh151rhj12t1j5j')
},
}
</script>


Routes:



    {
path: '/satisfaction-guarantee',
name: 'SatisfactionView',
component: load('PageContentfulView'),
},
{
path: '/about-us',
name: 'AboutUsView',
component: load('PageContentfulView'),
},









share|improve this question

























  • Inside of the component you have this.$route with useful information such as this.$route.name router.vuejs.org/api/#the-route-object

    – Thilo
    Nov 22 '18 at 11:11











  • Possible duplicate of 2 identical file, how to build a template / component in Vue.js 2

    – Bhojendra Rauniyar
    Nov 22 '18 at 16:46











  • It is not a duplicate I'm not asking the same thing

    – Ced
    Nov 22 '18 at 17:01
















0















I am using Vue.js 2.0, and I have this exact same code in 2 differents files, so I decided to build only one component and redirect the 2 routes on it, then I just need to pass the ID to the route, this way my 2 components can display differents resultats.



Note: the only thing that changes is the ID dsgh151rhj12t1j5j I would like that each route can send it own ID to my PageContentfulView component



EDIT: I just want to pass data from Route to the component



<template>
<div>
<div class="container">
<hp-row>
<hp-column>
<component v-for="component in content.column" :data="component" :key="component.id" :is="getComponentIdentifier(component.is)"></component>
</hp-column>
</hp-row>
</div>
</div>
</template>

<script>
import ModularView from '@/views/ModularView'

export default {
name: 'PageContentfulView',
mixins: [ModularView],

created () {
this.fetch('blocks/dsgh151rhj12t1j5j')
},
}
</script>


Routes:



    {
path: '/satisfaction-guarantee',
name: 'SatisfactionView',
component: load('PageContentfulView'),
},
{
path: '/about-us',
name: 'AboutUsView',
component: load('PageContentfulView'),
},









share|improve this question

























  • Inside of the component you have this.$route with useful information such as this.$route.name router.vuejs.org/api/#the-route-object

    – Thilo
    Nov 22 '18 at 11:11











  • Possible duplicate of 2 identical file, how to build a template / component in Vue.js 2

    – Bhojendra Rauniyar
    Nov 22 '18 at 16:46











  • It is not a duplicate I'm not asking the same thing

    – Ced
    Nov 22 '18 at 17:01














0












0








0








I am using Vue.js 2.0, and I have this exact same code in 2 differents files, so I decided to build only one component and redirect the 2 routes on it, then I just need to pass the ID to the route, this way my 2 components can display differents resultats.



Note: the only thing that changes is the ID dsgh151rhj12t1j5j I would like that each route can send it own ID to my PageContentfulView component



EDIT: I just want to pass data from Route to the component



<template>
<div>
<div class="container">
<hp-row>
<hp-column>
<component v-for="component in content.column" :data="component" :key="component.id" :is="getComponentIdentifier(component.is)"></component>
</hp-column>
</hp-row>
</div>
</div>
</template>

<script>
import ModularView from '@/views/ModularView'

export default {
name: 'PageContentfulView',
mixins: [ModularView],

created () {
this.fetch('blocks/dsgh151rhj12t1j5j')
},
}
</script>


Routes:



    {
path: '/satisfaction-guarantee',
name: 'SatisfactionView',
component: load('PageContentfulView'),
},
{
path: '/about-us',
name: 'AboutUsView',
component: load('PageContentfulView'),
},









share|improve this question
















I am using Vue.js 2.0, and I have this exact same code in 2 differents files, so I decided to build only one component and redirect the 2 routes on it, then I just need to pass the ID to the route, this way my 2 components can display differents resultats.



Note: the only thing that changes is the ID dsgh151rhj12t1j5j I would like that each route can send it own ID to my PageContentfulView component



EDIT: I just want to pass data from Route to the component



<template>
<div>
<div class="container">
<hp-row>
<hp-column>
<component v-for="component in content.column" :data="component" :key="component.id" :is="getComponentIdentifier(component.is)"></component>
</hp-column>
</hp-row>
</div>
</div>
</template>

<script>
import ModularView from '@/views/ModularView'

export default {
name: 'PageContentfulView',
mixins: [ModularView],

created () {
this.fetch('blocks/dsgh151rhj12t1j5j')
},
}
</script>


Routes:



    {
path: '/satisfaction-guarantee',
name: 'SatisfactionView',
component: load('PageContentfulView'),
},
{
path: '/about-us',
name: 'AboutUsView',
component: load('PageContentfulView'),
},






vue.js vuejs2 vue-router






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 22 '18 at 17:00







Ced

















asked Nov 22 '18 at 11:05









CedCed

456418




456418













  • Inside of the component you have this.$route with useful information such as this.$route.name router.vuejs.org/api/#the-route-object

    – Thilo
    Nov 22 '18 at 11:11











  • Possible duplicate of 2 identical file, how to build a template / component in Vue.js 2

    – Bhojendra Rauniyar
    Nov 22 '18 at 16:46











  • It is not a duplicate I'm not asking the same thing

    – Ced
    Nov 22 '18 at 17:01



















  • Inside of the component you have this.$route with useful information such as this.$route.name router.vuejs.org/api/#the-route-object

    – Thilo
    Nov 22 '18 at 11:11











  • Possible duplicate of 2 identical file, how to build a template / component in Vue.js 2

    – Bhojendra Rauniyar
    Nov 22 '18 at 16:46











  • It is not a duplicate I'm not asking the same thing

    – Ced
    Nov 22 '18 at 17:01

















Inside of the component you have this.$route with useful information such as this.$route.name router.vuejs.org/api/#the-route-object

– Thilo
Nov 22 '18 at 11:11





Inside of the component you have this.$route with useful information such as this.$route.name router.vuejs.org/api/#the-route-object

– Thilo
Nov 22 '18 at 11:11













Possible duplicate of 2 identical file, how to build a template / component in Vue.js 2

– Bhojendra Rauniyar
Nov 22 '18 at 16:46





Possible duplicate of 2 identical file, how to build a template / component in Vue.js 2

– Bhojendra Rauniyar
Nov 22 '18 at 16:46













It is not a duplicate I'm not asking the same thing

– Ced
Nov 22 '18 at 17:01





It is not a duplicate I'm not asking the same thing

– Ced
Nov 22 '18 at 17:01












4 Answers
4






active

oldest

votes


















2














'props' can solve your problem.



<template>
<div>
<div class="container">
<hp-row>
<hp-column>
<component v-for="component in content.column" :data="component" :key="component.id" :is="getComponentIdentifier(component.is)"></component>
</hp-column>
</hp-row>
</div>
</div>
</template>

<script>
import ModularView from '@/views/ModularView'

export default {
name: 'PageContentfulView',
mixins: [ModularView],
props: ['id'],
created () {
this.fetch('blocks/' + this.id)
},
}
</script>


route



  {
path: '/satisfaction-guarantee',
name: 'SatisfactionView',
component: load('PageContentfulView'),
props: { id: 'dsgh151rhj12t1j5j' },
},
{
path: '/about-us',
name: 'AboutUsView',
component: load('PageContentfulView'),
props: { id: 'blablablabla' },
},





share|improve this answer


























  • Thank you for your answer, but I have the following error: 'id' is not defined (no-undef) at src/views/PageContentfulView.vue:21:32: 19 | props: ['id'],

    – Ced
    Nov 22 '18 at 14:50






  • 1





    try this.id :) works fine for me.

    – Selmi Karim
    Nov 22 '18 at 16:30











  • It works perfectly, thank you a lot for your help ! :)

    – Ced
    Nov 23 '18 at 8:38



















0














You can do it using Dynamic Route Matching. You just need to include the parameter on the definition of the route, like below:



{
path: '/satisfaction-guarantee/:ID',
name: 'SatisfactionView',
component: load('PageContentfulView'),
}


Inside you component, to access the ID, you just need to use the this.$route.params.ID.






share|improve this answer
























  • First of all thank you for your answer, but the ID is a bit secret I would like to hide it from the user, so exposing it in the URL can expose the website to some security issue, do you have an Idea on how can I hide it ?

    – Ced
    Nov 22 '18 at 11:20













  • Then you could use the Vuex, that is made for sharing state in you application. When you want to change the ID, you change the state in the vuex store and your app does what it needs. Isn't easy as the dynamic routing, but, works as you need.

    – mthrsj
    Nov 22 '18 at 11:25



















0















Passing props to route component allows an object mode.



Example from the documentation:



const router = new VueRouter({
routes: [
{ path: '/promotion/from-newsletter', component: Promotion, props: { newsletterPopup: false } }
]
})


Route meta fields



Also you always can use the meta property to pass extra data (like a requireAuth flag)



const router = new VueRouter({
routes: [
{ path: '/test', component: TestComponent, meta: { someBoolean: false } }
]
})


And you can access it within your component



created() {
let meta = this.$route.meta;
}





share|improve this answer
























  • Thank you for your answer, but how can I pass my IDs as data instead of a boolean ?

    – Ced
    Nov 22 '18 at 14:43











  • I used a boolean to simplify the example, you can pass an object or any data type

    – DobleL
    Nov 22 '18 at 14:54











  • So I can say: meta: { id: dsgh15 } ?

    – Ced
    Nov 22 '18 at 15:03











  • exactly, and then in getComponentIdentifier do the magic to check that id

    – DobleL
    Nov 22 '18 at 15:43



















0














Set the props in your route to true and place /:id at the end of your path



  const router = new VueRouter({
routes: [
{ path: '/promotion/from-newsletter/:id',
component: Promotion,
props: true
}
]
})


You can now extract the param id of your route from the props in the corresponding component



<template>
<div>
<div class="container">
<hp-row>
<hp-column>
<component v-for="component in content.column" :data="component" :key="component.id" :is="getComponentIdentifier(component.is)"></component>
</hp-column>
</hp-row>
</div>
</div>
</template>

<script>
import ModularView from '@/views/ModularView'

export default {
name: 'PageContentfulView',
mixins: [ModularView],
props: ['id'],
created () {
this.fetch('blocks/'+id)
},
}
</script>





share|improve this answer
























  • Excuse me, where do you Set the Id in the code you made ? I mean where do you say ID = dsgh151rhj12t1j5j ?

    – Ced
    Nov 22 '18 at 15:01













  • In your url, so localhost:8080/promotion/from-newsletter/dsgh151rhj12t1j5j , for example

    – Djurdjen
    Nov 22 '18 at 15:15













  • I am sorry, I don't thing this is the best solution as I don't want the users to know the ID, I can't display it in the URL for privacy purposes

    – Ced
    Nov 23 '18 at 9:16











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53429565%2fvue-is-there-a-way-to-pass-data-from-route-to-component%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























4 Answers
4






active

oldest

votes








4 Answers
4






active

oldest

votes









active

oldest

votes






active

oldest

votes









2














'props' can solve your problem.



<template>
<div>
<div class="container">
<hp-row>
<hp-column>
<component v-for="component in content.column" :data="component" :key="component.id" :is="getComponentIdentifier(component.is)"></component>
</hp-column>
</hp-row>
</div>
</div>
</template>

<script>
import ModularView from '@/views/ModularView'

export default {
name: 'PageContentfulView',
mixins: [ModularView],
props: ['id'],
created () {
this.fetch('blocks/' + this.id)
},
}
</script>


route



  {
path: '/satisfaction-guarantee',
name: 'SatisfactionView',
component: load('PageContentfulView'),
props: { id: 'dsgh151rhj12t1j5j' },
},
{
path: '/about-us',
name: 'AboutUsView',
component: load('PageContentfulView'),
props: { id: 'blablablabla' },
},





share|improve this answer


























  • Thank you for your answer, but I have the following error: 'id' is not defined (no-undef) at src/views/PageContentfulView.vue:21:32: 19 | props: ['id'],

    – Ced
    Nov 22 '18 at 14:50






  • 1





    try this.id :) works fine for me.

    – Selmi Karim
    Nov 22 '18 at 16:30











  • It works perfectly, thank you a lot for your help ! :)

    – Ced
    Nov 23 '18 at 8:38
















2














'props' can solve your problem.



<template>
<div>
<div class="container">
<hp-row>
<hp-column>
<component v-for="component in content.column" :data="component" :key="component.id" :is="getComponentIdentifier(component.is)"></component>
</hp-column>
</hp-row>
</div>
</div>
</template>

<script>
import ModularView from '@/views/ModularView'

export default {
name: 'PageContentfulView',
mixins: [ModularView],
props: ['id'],
created () {
this.fetch('blocks/' + this.id)
},
}
</script>


route



  {
path: '/satisfaction-guarantee',
name: 'SatisfactionView',
component: load('PageContentfulView'),
props: { id: 'dsgh151rhj12t1j5j' },
},
{
path: '/about-us',
name: 'AboutUsView',
component: load('PageContentfulView'),
props: { id: 'blablablabla' },
},





share|improve this answer


























  • Thank you for your answer, but I have the following error: 'id' is not defined (no-undef) at src/views/PageContentfulView.vue:21:32: 19 | props: ['id'],

    – Ced
    Nov 22 '18 at 14:50






  • 1





    try this.id :) works fine for me.

    – Selmi Karim
    Nov 22 '18 at 16:30











  • It works perfectly, thank you a lot for your help ! :)

    – Ced
    Nov 23 '18 at 8:38














2












2








2







'props' can solve your problem.



<template>
<div>
<div class="container">
<hp-row>
<hp-column>
<component v-for="component in content.column" :data="component" :key="component.id" :is="getComponentIdentifier(component.is)"></component>
</hp-column>
</hp-row>
</div>
</div>
</template>

<script>
import ModularView from '@/views/ModularView'

export default {
name: 'PageContentfulView',
mixins: [ModularView],
props: ['id'],
created () {
this.fetch('blocks/' + this.id)
},
}
</script>


route



  {
path: '/satisfaction-guarantee',
name: 'SatisfactionView',
component: load('PageContentfulView'),
props: { id: 'dsgh151rhj12t1j5j' },
},
{
path: '/about-us',
name: 'AboutUsView',
component: load('PageContentfulView'),
props: { id: 'blablablabla' },
},





share|improve this answer















'props' can solve your problem.



<template>
<div>
<div class="container">
<hp-row>
<hp-column>
<component v-for="component in content.column" :data="component" :key="component.id" :is="getComponentIdentifier(component.is)"></component>
</hp-column>
</hp-row>
</div>
</div>
</template>

<script>
import ModularView from '@/views/ModularView'

export default {
name: 'PageContentfulView',
mixins: [ModularView],
props: ['id'],
created () {
this.fetch('blocks/' + this.id)
},
}
</script>


route



  {
path: '/satisfaction-guarantee',
name: 'SatisfactionView',
component: load('PageContentfulView'),
props: { id: 'dsgh151rhj12t1j5j' },
},
{
path: '/about-us',
name: 'AboutUsView',
component: load('PageContentfulView'),
props: { id: 'blablablabla' },
},






share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 23 '18 at 9:08









Ced

456418




456418










answered Nov 22 '18 at 12:35









Selmi KarimSelmi Karim

613313




613313













  • Thank you for your answer, but I have the following error: 'id' is not defined (no-undef) at src/views/PageContentfulView.vue:21:32: 19 | props: ['id'],

    – Ced
    Nov 22 '18 at 14:50






  • 1





    try this.id :) works fine for me.

    – Selmi Karim
    Nov 22 '18 at 16:30











  • It works perfectly, thank you a lot for your help ! :)

    – Ced
    Nov 23 '18 at 8:38



















  • Thank you for your answer, but I have the following error: 'id' is not defined (no-undef) at src/views/PageContentfulView.vue:21:32: 19 | props: ['id'],

    – Ced
    Nov 22 '18 at 14:50






  • 1





    try this.id :) works fine for me.

    – Selmi Karim
    Nov 22 '18 at 16:30











  • It works perfectly, thank you a lot for your help ! :)

    – Ced
    Nov 23 '18 at 8:38

















Thank you for your answer, but I have the following error: 'id' is not defined (no-undef) at src/views/PageContentfulView.vue:21:32: 19 | props: ['id'],

– Ced
Nov 22 '18 at 14:50





Thank you for your answer, but I have the following error: 'id' is not defined (no-undef) at src/views/PageContentfulView.vue:21:32: 19 | props: ['id'],

– Ced
Nov 22 '18 at 14:50




1




1





try this.id :) works fine for me.

– Selmi Karim
Nov 22 '18 at 16:30





try this.id :) works fine for me.

– Selmi Karim
Nov 22 '18 at 16:30













It works perfectly, thank you a lot for your help ! :)

– Ced
Nov 23 '18 at 8:38





It works perfectly, thank you a lot for your help ! :)

– Ced
Nov 23 '18 at 8:38













0














You can do it using Dynamic Route Matching. You just need to include the parameter on the definition of the route, like below:



{
path: '/satisfaction-guarantee/:ID',
name: 'SatisfactionView',
component: load('PageContentfulView'),
}


Inside you component, to access the ID, you just need to use the this.$route.params.ID.






share|improve this answer
























  • First of all thank you for your answer, but the ID is a bit secret I would like to hide it from the user, so exposing it in the URL can expose the website to some security issue, do you have an Idea on how can I hide it ?

    – Ced
    Nov 22 '18 at 11:20













  • Then you could use the Vuex, that is made for sharing state in you application. When you want to change the ID, you change the state in the vuex store and your app does what it needs. Isn't easy as the dynamic routing, but, works as you need.

    – mthrsj
    Nov 22 '18 at 11:25
















0














You can do it using Dynamic Route Matching. You just need to include the parameter on the definition of the route, like below:



{
path: '/satisfaction-guarantee/:ID',
name: 'SatisfactionView',
component: load('PageContentfulView'),
}


Inside you component, to access the ID, you just need to use the this.$route.params.ID.






share|improve this answer
























  • First of all thank you for your answer, but the ID is a bit secret I would like to hide it from the user, so exposing it in the URL can expose the website to some security issue, do you have an Idea on how can I hide it ?

    – Ced
    Nov 22 '18 at 11:20













  • Then you could use the Vuex, that is made for sharing state in you application. When you want to change the ID, you change the state in the vuex store and your app does what it needs. Isn't easy as the dynamic routing, but, works as you need.

    – mthrsj
    Nov 22 '18 at 11:25














0












0








0







You can do it using Dynamic Route Matching. You just need to include the parameter on the definition of the route, like below:



{
path: '/satisfaction-guarantee/:ID',
name: 'SatisfactionView',
component: load('PageContentfulView'),
}


Inside you component, to access the ID, you just need to use the this.$route.params.ID.






share|improve this answer













You can do it using Dynamic Route Matching. You just need to include the parameter on the definition of the route, like below:



{
path: '/satisfaction-guarantee/:ID',
name: 'SatisfactionView',
component: load('PageContentfulView'),
}


Inside you component, to access the ID, you just need to use the this.$route.params.ID.







share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 22 '18 at 11:15









mthrsjmthrsj

1,247621




1,247621













  • First of all thank you for your answer, but the ID is a bit secret I would like to hide it from the user, so exposing it in the URL can expose the website to some security issue, do you have an Idea on how can I hide it ?

    – Ced
    Nov 22 '18 at 11:20













  • Then you could use the Vuex, that is made for sharing state in you application. When you want to change the ID, you change the state in the vuex store and your app does what it needs. Isn't easy as the dynamic routing, but, works as you need.

    – mthrsj
    Nov 22 '18 at 11:25



















  • First of all thank you for your answer, but the ID is a bit secret I would like to hide it from the user, so exposing it in the URL can expose the website to some security issue, do you have an Idea on how can I hide it ?

    – Ced
    Nov 22 '18 at 11:20













  • Then you could use the Vuex, that is made for sharing state in you application. When you want to change the ID, you change the state in the vuex store and your app does what it needs. Isn't easy as the dynamic routing, but, works as you need.

    – mthrsj
    Nov 22 '18 at 11:25

















First of all thank you for your answer, but the ID is a bit secret I would like to hide it from the user, so exposing it in the URL can expose the website to some security issue, do you have an Idea on how can I hide it ?

– Ced
Nov 22 '18 at 11:20







First of all thank you for your answer, but the ID is a bit secret I would like to hide it from the user, so exposing it in the URL can expose the website to some security issue, do you have an Idea on how can I hide it ?

– Ced
Nov 22 '18 at 11:20















Then you could use the Vuex, that is made for sharing state in you application. When you want to change the ID, you change the state in the vuex store and your app does what it needs. Isn't easy as the dynamic routing, but, works as you need.

– mthrsj
Nov 22 '18 at 11:25





Then you could use the Vuex, that is made for sharing state in you application. When you want to change the ID, you change the state in the vuex store and your app does what it needs. Isn't easy as the dynamic routing, but, works as you need.

– mthrsj
Nov 22 '18 at 11:25











0















Passing props to route component allows an object mode.



Example from the documentation:



const router = new VueRouter({
routes: [
{ path: '/promotion/from-newsletter', component: Promotion, props: { newsletterPopup: false } }
]
})


Route meta fields



Also you always can use the meta property to pass extra data (like a requireAuth flag)



const router = new VueRouter({
routes: [
{ path: '/test', component: TestComponent, meta: { someBoolean: false } }
]
})


And you can access it within your component



created() {
let meta = this.$route.meta;
}





share|improve this answer
























  • Thank you for your answer, but how can I pass my IDs as data instead of a boolean ?

    – Ced
    Nov 22 '18 at 14:43











  • I used a boolean to simplify the example, you can pass an object or any data type

    – DobleL
    Nov 22 '18 at 14:54











  • So I can say: meta: { id: dsgh15 } ?

    – Ced
    Nov 22 '18 at 15:03











  • exactly, and then in getComponentIdentifier do the magic to check that id

    – DobleL
    Nov 22 '18 at 15:43
















0















Passing props to route component allows an object mode.



Example from the documentation:



const router = new VueRouter({
routes: [
{ path: '/promotion/from-newsletter', component: Promotion, props: { newsletterPopup: false } }
]
})


Route meta fields



Also you always can use the meta property to pass extra data (like a requireAuth flag)



const router = new VueRouter({
routes: [
{ path: '/test', component: TestComponent, meta: { someBoolean: false } }
]
})


And you can access it within your component



created() {
let meta = this.$route.meta;
}





share|improve this answer
























  • Thank you for your answer, but how can I pass my IDs as data instead of a boolean ?

    – Ced
    Nov 22 '18 at 14:43











  • I used a boolean to simplify the example, you can pass an object or any data type

    – DobleL
    Nov 22 '18 at 14:54











  • So I can say: meta: { id: dsgh15 } ?

    – Ced
    Nov 22 '18 at 15:03











  • exactly, and then in getComponentIdentifier do the magic to check that id

    – DobleL
    Nov 22 '18 at 15:43














0












0








0








Passing props to route component allows an object mode.



Example from the documentation:



const router = new VueRouter({
routes: [
{ path: '/promotion/from-newsletter', component: Promotion, props: { newsletterPopup: false } }
]
})


Route meta fields



Also you always can use the meta property to pass extra data (like a requireAuth flag)



const router = new VueRouter({
routes: [
{ path: '/test', component: TestComponent, meta: { someBoolean: false } }
]
})


And you can access it within your component



created() {
let meta = this.$route.meta;
}





share|improve this answer














Passing props to route component allows an object mode.



Example from the documentation:



const router = new VueRouter({
routes: [
{ path: '/promotion/from-newsletter', component: Promotion, props: { newsletterPopup: false } }
]
})


Route meta fields



Also you always can use the meta property to pass extra data (like a requireAuth flag)



const router = new VueRouter({
routes: [
{ path: '/test', component: TestComponent, meta: { someBoolean: false } }
]
})


And you can access it within your component



created() {
let meta = this.$route.meta;
}






share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 22 '18 at 12:42









DobleLDobleL

1,577513




1,577513













  • Thank you for your answer, but how can I pass my IDs as data instead of a boolean ?

    – Ced
    Nov 22 '18 at 14:43











  • I used a boolean to simplify the example, you can pass an object or any data type

    – DobleL
    Nov 22 '18 at 14:54











  • So I can say: meta: { id: dsgh15 } ?

    – Ced
    Nov 22 '18 at 15:03











  • exactly, and then in getComponentIdentifier do the magic to check that id

    – DobleL
    Nov 22 '18 at 15:43



















  • Thank you for your answer, but how can I pass my IDs as data instead of a boolean ?

    – Ced
    Nov 22 '18 at 14:43











  • I used a boolean to simplify the example, you can pass an object or any data type

    – DobleL
    Nov 22 '18 at 14:54











  • So I can say: meta: { id: dsgh15 } ?

    – Ced
    Nov 22 '18 at 15:03











  • exactly, and then in getComponentIdentifier do the magic to check that id

    – DobleL
    Nov 22 '18 at 15:43

















Thank you for your answer, but how can I pass my IDs as data instead of a boolean ?

– Ced
Nov 22 '18 at 14:43





Thank you for your answer, but how can I pass my IDs as data instead of a boolean ?

– Ced
Nov 22 '18 at 14:43













I used a boolean to simplify the example, you can pass an object or any data type

– DobleL
Nov 22 '18 at 14:54





I used a boolean to simplify the example, you can pass an object or any data type

– DobleL
Nov 22 '18 at 14:54













So I can say: meta: { id: dsgh15 } ?

– Ced
Nov 22 '18 at 15:03





So I can say: meta: { id: dsgh15 } ?

– Ced
Nov 22 '18 at 15:03













exactly, and then in getComponentIdentifier do the magic to check that id

– DobleL
Nov 22 '18 at 15:43





exactly, and then in getComponentIdentifier do the magic to check that id

– DobleL
Nov 22 '18 at 15:43











0














Set the props in your route to true and place /:id at the end of your path



  const router = new VueRouter({
routes: [
{ path: '/promotion/from-newsletter/:id',
component: Promotion,
props: true
}
]
})


You can now extract the param id of your route from the props in the corresponding component



<template>
<div>
<div class="container">
<hp-row>
<hp-column>
<component v-for="component in content.column" :data="component" :key="component.id" :is="getComponentIdentifier(component.is)"></component>
</hp-column>
</hp-row>
</div>
</div>
</template>

<script>
import ModularView from '@/views/ModularView'

export default {
name: 'PageContentfulView',
mixins: [ModularView],
props: ['id'],
created () {
this.fetch('blocks/'+id)
},
}
</script>





share|improve this answer
























  • Excuse me, where do you Set the Id in the code you made ? I mean where do you say ID = dsgh151rhj12t1j5j ?

    – Ced
    Nov 22 '18 at 15:01













  • In your url, so localhost:8080/promotion/from-newsletter/dsgh151rhj12t1j5j , for example

    – Djurdjen
    Nov 22 '18 at 15:15













  • I am sorry, I don't thing this is the best solution as I don't want the users to know the ID, I can't display it in the URL for privacy purposes

    – Ced
    Nov 23 '18 at 9:16
















0














Set the props in your route to true and place /:id at the end of your path



  const router = new VueRouter({
routes: [
{ path: '/promotion/from-newsletter/:id',
component: Promotion,
props: true
}
]
})


You can now extract the param id of your route from the props in the corresponding component



<template>
<div>
<div class="container">
<hp-row>
<hp-column>
<component v-for="component in content.column" :data="component" :key="component.id" :is="getComponentIdentifier(component.is)"></component>
</hp-column>
</hp-row>
</div>
</div>
</template>

<script>
import ModularView from '@/views/ModularView'

export default {
name: 'PageContentfulView',
mixins: [ModularView],
props: ['id'],
created () {
this.fetch('blocks/'+id)
},
}
</script>





share|improve this answer
























  • Excuse me, where do you Set the Id in the code you made ? I mean where do you say ID = dsgh151rhj12t1j5j ?

    – Ced
    Nov 22 '18 at 15:01













  • In your url, so localhost:8080/promotion/from-newsletter/dsgh151rhj12t1j5j , for example

    – Djurdjen
    Nov 22 '18 at 15:15













  • I am sorry, I don't thing this is the best solution as I don't want the users to know the ID, I can't display it in the URL for privacy purposes

    – Ced
    Nov 23 '18 at 9:16














0












0








0







Set the props in your route to true and place /:id at the end of your path



  const router = new VueRouter({
routes: [
{ path: '/promotion/from-newsletter/:id',
component: Promotion,
props: true
}
]
})


You can now extract the param id of your route from the props in the corresponding component



<template>
<div>
<div class="container">
<hp-row>
<hp-column>
<component v-for="component in content.column" :data="component" :key="component.id" :is="getComponentIdentifier(component.is)"></component>
</hp-column>
</hp-row>
</div>
</div>
</template>

<script>
import ModularView from '@/views/ModularView'

export default {
name: 'PageContentfulView',
mixins: [ModularView],
props: ['id'],
created () {
this.fetch('blocks/'+id)
},
}
</script>





share|improve this answer













Set the props in your route to true and place /:id at the end of your path



  const router = new VueRouter({
routes: [
{ path: '/promotion/from-newsletter/:id',
component: Promotion,
props: true
}
]
})


You can now extract the param id of your route from the props in the corresponding component



<template>
<div>
<div class="container">
<hp-row>
<hp-column>
<component v-for="component in content.column" :data="component" :key="component.id" :is="getComponentIdentifier(component.is)"></component>
</hp-column>
</hp-row>
</div>
</div>
</template>

<script>
import ModularView from '@/views/ModularView'

export default {
name: 'PageContentfulView',
mixins: [ModularView],
props: ['id'],
created () {
this.fetch('blocks/'+id)
},
}
</script>






share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 22 '18 at 14:54









DjurdjenDjurdjen

1818




1818













  • Excuse me, where do you Set the Id in the code you made ? I mean where do you say ID = dsgh151rhj12t1j5j ?

    – Ced
    Nov 22 '18 at 15:01













  • In your url, so localhost:8080/promotion/from-newsletter/dsgh151rhj12t1j5j , for example

    – Djurdjen
    Nov 22 '18 at 15:15













  • I am sorry, I don't thing this is the best solution as I don't want the users to know the ID, I can't display it in the URL for privacy purposes

    – Ced
    Nov 23 '18 at 9:16



















  • Excuse me, where do you Set the Id in the code you made ? I mean where do you say ID = dsgh151rhj12t1j5j ?

    – Ced
    Nov 22 '18 at 15:01













  • In your url, so localhost:8080/promotion/from-newsletter/dsgh151rhj12t1j5j , for example

    – Djurdjen
    Nov 22 '18 at 15:15













  • I am sorry, I don't thing this is the best solution as I don't want the users to know the ID, I can't display it in the URL for privacy purposes

    – Ced
    Nov 23 '18 at 9:16

















Excuse me, where do you Set the Id in the code you made ? I mean where do you say ID = dsgh151rhj12t1j5j ?

– Ced
Nov 22 '18 at 15:01







Excuse me, where do you Set the Id in the code you made ? I mean where do you say ID = dsgh151rhj12t1j5j ?

– Ced
Nov 22 '18 at 15:01















In your url, so localhost:8080/promotion/from-newsletter/dsgh151rhj12t1j5j , for example

– Djurdjen
Nov 22 '18 at 15:15







In your url, so localhost:8080/promotion/from-newsletter/dsgh151rhj12t1j5j , for example

– Djurdjen
Nov 22 '18 at 15:15















I am sorry, I don't thing this is the best solution as I don't want the users to know the ID, I can't display it in the URL for privacy purposes

– Ced
Nov 23 '18 at 9:16





I am sorry, I don't thing this is the best solution as I don't want the users to know the ID, I can't display it in the URL for privacy purposes

– Ced
Nov 23 '18 at 9:16


















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53429565%2fvue-is-there-a-way-to-pass-data-from-route-to-component%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







這個網誌中的熱門文章

Hercules Kyvelos

Tangent Lines Diagram Along Smooth Curve

Yusuf al-Mu'taman ibn Hud