How to dynamically get a list of array values ​​based on 'id' in Vuejs?
P粉515066518
P粉515066518 2024-03-26 21:25:26
0
1
482

**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> &nbsp;&nbsp;&nbsp;
      <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粉515066518
P粉515066518

reply all(1)
P粉276577460

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

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template