【问题标题】:Profile page component does not display on new router link with VueJS route配置文件页面组件不会显示在带有 VueJS 路由的新路由器链接上
【发布时间】:2021-02-15 15:00:50
【问题描述】:

我的目标是创建一个用户列表,当您单击每个用户的 first_name 时,它会转到一个名为 Profile.vue 的新路由器视图,其中应该显示该用户的特定信息。问题是页面似乎是空白的。

我的应用如下所示:

<template>
  <div id="app">
    <div id="nav">
      <router-link to="/">Home Page</router-link> 
      <!-- <router-link to="/">Home</router-link> -->
    </div>
    <router-view />
  </div>
</template>

我的 index.js

const routes = [

  {
    path: "/",
    name: "List",
    component: List
  },

  {
    path: "List/profile/:id",
    name: "Profile",
    component: Profile,
    props:true 
  },

在 List.vue(使用 bootsrap)中,我显示了来自 API 的用户列表,我想单击名字并打开 Profile.vue 并分别显示每个用户的信息。

<template>
  <div class="container mt-4" id="list">
    <table class="table table-bordered">
      <thead class="table-dark">
      </thead>
      <tbody>
        <tr v-for="user in userList.data" :key="user">
          <v-avatar class="figure"
            ><img :src="user.avatar" alt="Connection lost please reload"
          /></v-avatar>
          <td>
            <router-link :to="'List/profile/' + user.id">{{
              user.first_name
            }}</router-link>
          </td>
          <!-- <td>{{ user.first_name }}</td> -->
          <td>{{ user.last_name }}</td>
          <td>{{ user.email }}</td>
          <th>{{ user.id }}</th>
        </tr>
      </tbody>
    </table>
    <router-view />
  </div>
</template>



<script>
export default {
  name: "List",

  data: () => ({
    userList: [],

  }),

  created() {
    fetch("API LINK" + this.$route.params)
      .then((response) => response.json())
      .then((data) => {
        this.userList = data;
        this.default = [...data.data];
      });
  },

};
</script>




<style>
th {
  cursor: pointer;
}
</style>

Profile.vue

<template>
  <div class="container">
    <h1>This is Profile page number {{ user.id }}</h1>
  </div>
</template>

<script>

export default {
  name: "Profile",
  props: [
      'user'
  ],
  components: {},

  data: () => ({
    destinationId: this.$route.params.user.id,
  }),

  computed: {
    destination() {
      return this.userList.data.find((user) => user.id == this.destinationId);
    },
  },
};
</script>

这是我主页的样子:https://i.stack.imgur.com/wVjKZ.png

但是它转到的页面(示例):http://localhost:8080/List/profile/7 是空白的。

【问题讨论】:

  • 您传递了一个id,但配置文件组件需要一个user
  • 如何更改它以便它按 id 搜索呢?
  • 好吧,您的 Profile.vue 也无权访问 userList 数据。因此,您可以通过 id 获取数据,或者将 userList 放入 Vuex 并通过 id 从那里检索它

标签: vue.js


【解决方案1】:

试试这个方法一次: index.js

{
    path: "List/profile/:id",
    name: "Profile",
    component: Profile
  }

在 List.vue 中

 <td>
            <router-link :to="{ name: 'Profile', params: { user_id: user.id }}">{{
              user.first_name
            }}</router-link>
          </td>

在 Profile.vue 中 获取该 ID this.$route.params.user_id

【讨论】:

  • 不够,Profile.vue 无权访问userList 数据
  • 它不需要在特定的个人资料视图中获取用户列表,只需获取该个人资料的ID即可获取他/她的信息并显示在profile.vue上
  • @GunarajKhatri- 您提供的方法仍然使页面空白,现在链接不显示 id
  • user.id 没有 id 值可以尝试手动检查以确保安全
猜你喜欢
  • 2021-07-27
  • 1970-01-01
  • 2018-04-22
  • 2021-10-09
  • 1970-01-01
  • 2018-12-06
  • 2016-12-13
  • 2022-01-18
  • 1970-01-01
相关资源
最近更新 更多