**User.vue** <template> <div> <div v-for="list in lists" :key="list.id">{{ list.val }} {{ list.kk }}</div> </div> </template> <script> import { datalist } from "./datalist"; export default { name: "User", data() { return { lists: datalist, }; }, }; </script>
**datalist.js** export const datalist = [ { id: 1, val: "11", kk: "potter" }, { id: 2, val: "22", kk: "james" }, { id: 3, val: "55", kk: "limda" }, { id: 4, val: "77", kk: "stepen" } ];
**HelloWorld.vue** <template> <div> <b>Vuejs dynamic routing</b> <div v-for="item in items" :key="item.id"> <b>{{ item.id }}.</b> <router-link :to="{ name: 'UserWithID', params: { id: item.id } }"> {{ item.kk }} </router-link> </div> <br /><br /><br /> <b> {{ $route.params.id }}</b> <User /> </div> </template> <script> import User from "./User.vue"; import { datalist } from "./datalist"; export default { name: "HelloWorld", components: { User, }, data() { return { items: datalist, }; }, }; </script>
I'm working on dynamic routing where onclick of each array value, I get the "id" dynamically. (Such as 1,2,3,4,5...)
Now I want to display the array value. Specific to id only. Like...
If I click on id:1, then I just need to get the specific array value linked to id:1. Similar situations for 2, 3, 4...
Currently the problem is that if I click "array id:2" or any id value.. then I will get the entire list of array values.
1
11 Porter 22 James 55 Lin 77 degrees
Full working code:- https://codesandbox.io/s/proud-fast-sokui?file=/src/components/HelloWorld.vue
P粉2765774602024-03-27 20:13:19
You can find the required user information through $route.params.id
in User.vue
.
Example User.vue
:
{{ item }}... computed: { user: function () { return this.lists.find((item) => item.id === this.$route.params.id); }, }
Demo codesandbox