passing data between components with vue-router
There a re quite a few question with kind of a similar title to mine, but that is where my problem lies. Let's take a very common case where i'm building a recipe book.
i'm looping over my recipes and creating a link to the Recipe.vue
component:
<router-link v-for="recipe in recipes" :to="{path: `/recipe/${recipe.title}`">
<a>{{recipe.id}}</a>
</router-link>
now from what i understand there are few solutions:
1. setting props:true
and pass a prop:
<router-link v-for="recipe in recipes" :to="{name: 'Recipe', params: {recipe}}">
<a>{{recipe.title}}</a>
</router-link>
set
Recipe
as child component and pass the data as a regur parent=>child data transferuse Vuex and state management to then "withdraw" the data from the store in the
Recipe
componentpass id to the
Recipe
component and then when itsmounted()
execute an http request to get the data of the specific item.
I'm finding it hard to understand what is considered best practice here and what should i choose for my production env.
the voice inside me says that number 3 - state management is an overkill and #4 is an unnecessary api call since i already have the data i need.
Any advice on which method should i use in the basic case of: first component is a list of recipes and then navigate from it to a component with single purpose of displaying the recipe data?
vue.js
add a comment |
There a re quite a few question with kind of a similar title to mine, but that is where my problem lies. Let's take a very common case where i'm building a recipe book.
i'm looping over my recipes and creating a link to the Recipe.vue
component:
<router-link v-for="recipe in recipes" :to="{path: `/recipe/${recipe.title}`">
<a>{{recipe.id}}</a>
</router-link>
now from what i understand there are few solutions:
1. setting props:true
and pass a prop:
<router-link v-for="recipe in recipes" :to="{name: 'Recipe', params: {recipe}}">
<a>{{recipe.title}}</a>
</router-link>
set
Recipe
as child component and pass the data as a regur parent=>child data transferuse Vuex and state management to then "withdraw" the data from the store in the
Recipe
componentpass id to the
Recipe
component and then when itsmounted()
execute an http request to get the data of the specific item.
I'm finding it hard to understand what is considered best practice here and what should i choose for my production env.
the voice inside me says that number 3 - state management is an overkill and #4 is an unnecessary api call since i already have the data i need.
Any advice on which method should i use in the basic case of: first component is a list of recipes and then navigate from it to a component with single purpose of displaying the recipe data?
vue.js
I would say the /recipe/example-recipe page should be loading the recipe information itself, not having it passed from the recipe list page. Otherwise, and this is just one of several examples, what if a user bookmarked that recipe and went directly to the specific recipe page? So I guess #4 kind of, but not even passing the id - looking it up from the url.
– Jayem163
Nov 26 '18 at 20:59
i understand, the best solution will be another http request? feels odd because i already have the data but i guess ,considering the issues you brought up that the best solution
– yariv bar
Nov 26 '18 at 21:11
1
Well you could check if data is passed, and if it is not then load it with an http request - It's just necessary to have an http request implemented in case they aren't coming from the list of recipes. If you wanted to pass the recipe when possible, as you noted you could use any of the methods 1-3. You probably wouldn't see a typical programmer set up Vuex just for this, but if you were planning on a more complicated app with users, comments, or just more complexity then Vuex would definitely be best. If it were just this simple app then I think the most common would be #2 but #1 is fine too
– Jayem163
Nov 27 '18 at 14:09
add a comment |
There a re quite a few question with kind of a similar title to mine, but that is where my problem lies. Let's take a very common case where i'm building a recipe book.
i'm looping over my recipes and creating a link to the Recipe.vue
component:
<router-link v-for="recipe in recipes" :to="{path: `/recipe/${recipe.title}`">
<a>{{recipe.id}}</a>
</router-link>
now from what i understand there are few solutions:
1. setting props:true
and pass a prop:
<router-link v-for="recipe in recipes" :to="{name: 'Recipe', params: {recipe}}">
<a>{{recipe.title}}</a>
</router-link>
set
Recipe
as child component and pass the data as a regur parent=>child data transferuse Vuex and state management to then "withdraw" the data from the store in the
Recipe
componentpass id to the
Recipe
component and then when itsmounted()
execute an http request to get the data of the specific item.
I'm finding it hard to understand what is considered best practice here and what should i choose for my production env.
the voice inside me says that number 3 - state management is an overkill and #4 is an unnecessary api call since i already have the data i need.
Any advice on which method should i use in the basic case of: first component is a list of recipes and then navigate from it to a component with single purpose of displaying the recipe data?
vue.js
There a re quite a few question with kind of a similar title to mine, but that is where my problem lies. Let's take a very common case where i'm building a recipe book.
i'm looping over my recipes and creating a link to the Recipe.vue
component:
<router-link v-for="recipe in recipes" :to="{path: `/recipe/${recipe.title}`">
<a>{{recipe.id}}</a>
</router-link>
now from what i understand there are few solutions:
1. setting props:true
and pass a prop:
<router-link v-for="recipe in recipes" :to="{name: 'Recipe', params: {recipe}}">
<a>{{recipe.title}}</a>
</router-link>
set
Recipe
as child component and pass the data as a regur parent=>child data transferuse Vuex and state management to then "withdraw" the data from the store in the
Recipe
componentpass id to the
Recipe
component and then when itsmounted()
execute an http request to get the data of the specific item.
I'm finding it hard to understand what is considered best practice here and what should i choose for my production env.
the voice inside me says that number 3 - state management is an overkill and #4 is an unnecessary api call since i already have the data i need.
Any advice on which method should i use in the basic case of: first component is a list of recipes and then navigate from it to a component with single purpose of displaying the recipe data?
vue.js
vue.js
asked Nov 26 '18 at 20:16
yariv baryariv bar
306216
306216
I would say the /recipe/example-recipe page should be loading the recipe information itself, not having it passed from the recipe list page. Otherwise, and this is just one of several examples, what if a user bookmarked that recipe and went directly to the specific recipe page? So I guess #4 kind of, but not even passing the id - looking it up from the url.
– Jayem163
Nov 26 '18 at 20:59
i understand, the best solution will be another http request? feels odd because i already have the data but i guess ,considering the issues you brought up that the best solution
– yariv bar
Nov 26 '18 at 21:11
1
Well you could check if data is passed, and if it is not then load it with an http request - It's just necessary to have an http request implemented in case they aren't coming from the list of recipes. If you wanted to pass the recipe when possible, as you noted you could use any of the methods 1-3. You probably wouldn't see a typical programmer set up Vuex just for this, but if you were planning on a more complicated app with users, comments, or just more complexity then Vuex would definitely be best. If it were just this simple app then I think the most common would be #2 but #1 is fine too
– Jayem163
Nov 27 '18 at 14:09
add a comment |
I would say the /recipe/example-recipe page should be loading the recipe information itself, not having it passed from the recipe list page. Otherwise, and this is just one of several examples, what if a user bookmarked that recipe and went directly to the specific recipe page? So I guess #4 kind of, but not even passing the id - looking it up from the url.
– Jayem163
Nov 26 '18 at 20:59
i understand, the best solution will be another http request? feels odd because i already have the data but i guess ,considering the issues you brought up that the best solution
– yariv bar
Nov 26 '18 at 21:11
1
Well you could check if data is passed, and if it is not then load it with an http request - It's just necessary to have an http request implemented in case they aren't coming from the list of recipes. If you wanted to pass the recipe when possible, as you noted you could use any of the methods 1-3. You probably wouldn't see a typical programmer set up Vuex just for this, but if you were planning on a more complicated app with users, comments, or just more complexity then Vuex would definitely be best. If it were just this simple app then I think the most common would be #2 but #1 is fine too
– Jayem163
Nov 27 '18 at 14:09
I would say the /recipe/example-recipe page should be loading the recipe information itself, not having it passed from the recipe list page. Otherwise, and this is just one of several examples, what if a user bookmarked that recipe and went directly to the specific recipe page? So I guess #4 kind of, but not even passing the id - looking it up from the url.
– Jayem163
Nov 26 '18 at 20:59
I would say the /recipe/example-recipe page should be loading the recipe information itself, not having it passed from the recipe list page. Otherwise, and this is just one of several examples, what if a user bookmarked that recipe and went directly to the specific recipe page? So I guess #4 kind of, but not even passing the id - looking it up from the url.
– Jayem163
Nov 26 '18 at 20:59
i understand, the best solution will be another http request? feels odd because i already have the data but i guess ,considering the issues you brought up that the best solution
– yariv bar
Nov 26 '18 at 21:11
i understand, the best solution will be another http request? feels odd because i already have the data but i guess ,considering the issues you brought up that the best solution
– yariv bar
Nov 26 '18 at 21:11
1
1
Well you could check if data is passed, and if it is not then load it with an http request - It's just necessary to have an http request implemented in case they aren't coming from the list of recipes. If you wanted to pass the recipe when possible, as you noted you could use any of the methods 1-3. You probably wouldn't see a typical programmer set up Vuex just for this, but if you were planning on a more complicated app with users, comments, or just more complexity then Vuex would definitely be best. If it were just this simple app then I think the most common would be #2 but #1 is fine too
– Jayem163
Nov 27 '18 at 14:09
Well you could check if data is passed, and if it is not then load it with an http request - It's just necessary to have an http request implemented in case they aren't coming from the list of recipes. If you wanted to pass the recipe when possible, as you noted you could use any of the methods 1-3. You probably wouldn't see a typical programmer set up Vuex just for this, but if you were planning on a more complicated app with users, comments, or just more complexity then Vuex would definitely be best. If it were just this simple app then I think the most common would be #2 but #1 is fine too
– Jayem163
Nov 27 '18 at 14:09
add a comment |
0
active
oldest
votes
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
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53488380%2fpassing-data-between-components-with-vue-router%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
0
active
oldest
votes
0
active
oldest
votes
active
oldest
votes
active
oldest
votes
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.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53488380%2fpassing-data-between-components-with-vue-router%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
I would say the /recipe/example-recipe page should be loading the recipe information itself, not having it passed from the recipe list page. Otherwise, and this is just one of several examples, what if a user bookmarked that recipe and went directly to the specific recipe page? So I guess #4 kind of, but not even passing the id - looking it up from the url.
– Jayem163
Nov 26 '18 at 20:59
i understand, the best solution will be another http request? feels odd because i already have the data but i guess ,considering the issues you brought up that the best solution
– yariv bar
Nov 26 '18 at 21:11
1
Well you could check if data is passed, and if it is not then load it with an http request - It's just necessary to have an http request implemented in case they aren't coming from the list of recipes. If you wanted to pass the recipe when possible, as you noted you could use any of the methods 1-3. You probably wouldn't see a typical programmer set up Vuex just for this, but if you were planning on a more complicated app with users, comments, or just more complexity then Vuex would definitely be best. If it were just this simple app then I think the most common would be #2 but #1 is fine too
– Jayem163
Nov 27 '18 at 14:09