【问题标题】:How to route/link each user to their own data?如何将每个用户路由/链接到他们自己的数据?
【发布时间】:2019-01-14 18:19:38
【问题描述】:

我在Firebase Firestore中存储了一堆用户。

然后我创建了一个Vue 组件,其中的图片带有router-link 到用户个人资料,如果我单击该图片,我想从发布该图片的用户转到用户个人资料网站。

<router-link v-bind:to="{ name: 'view-post', params:{ userId:post.userId}}">
  <v-img :src="post.image" alt="pic"></v-img>
</router-link>

这是我现在的代码,是的,我知道它是一个Vue 组件

   <template>
     <v-layout row wrap>
      <v-content>


      </v-content>
     </v-layout>
   </template>

  <script>
   import { mapState } from 'vuex'
   const fb = require('../firebaseConfig.js')

   export default {
    name: 'view-employee',
    data: () => ({

    }),
    computed: {
     ...mapState(['userProfile', 'currentUser', 'posts'])
    },
    methods: {

    }
  }
 </script>

在计算属性...mapState 中存储来自firestore 的帖子、用户和当前用户

Router.js

import Vue from 'vue'
import Router from 'vue-router'
import firebase from 'firebase'
import World from './views/World.vue'
import Login from '@/components/Login'
import ViewEmployee from '@/components/ViewEmployee'

 Vue.use(Router)

   const router = new Router({
     mode: 'history',
     routes: [
      {
        path: '/login',
        name: 'login',
        component: Login,
        meta: {
          requiresAuth: false
        }
      },
      {
       path: '/world',
       name: 'world',
       component: World,
       meta: {
        requiresAuth: true
       }
      },
      {
       path: '/post/:userId',
       name: 'view-post',
       props: true,
       component: ViewEmployee,
       meta: {
        requiresAuth: true
       }
     }
   ]
 })


  export default router

【问题讨论】:

  • 你的问题真的很广泛......有很多不同的方法可以实现你的目标。我建议你看看这个教程savvyapps.com/blog/…
  • 我知道这个教程我已经阅读过,但没有任何帮助
  • 我想将我存储在 firestore 中的每个用户链接到他们自己的个人资料,以便当前用户看到他的个人资料并可以单击链接转到另一个个人资料
  • 另一个例子是,如果当前用户点击了一张图片,并且这张图片有一个路由到已经发布图片的用户个人资料网站。然后如何显示发布图片的用户的数据?
  • 对于您所展示的内容,我们能做的最好的事情就是提出可能与您目前所做的事情无关的建议。例如,您可以通过在代码中显示您连接到 Firebase 的位置,详细说明您在网络流量或控制台中发现的任何错误来缩小遇到问题的区域...

标签: javascript firebase vue.js google-cloud-firestore


【解决方案1】:

根据您的 Vue 路由器的代码,您可以执行以下操作:

1/ 在您的 view-post 组件中(您可以通过 /post/:userId 访问)使用 this.$route.params.userId 获取 userId 的值。

2/ 在 created 生命周期挂钩中,根据 userId 的值从 Firestore 中获取数据。

3/ 在你的 DOM 中显示数据。

您将在 Vue 路由器文档中找到详细解释此机制的页面,方法是在 Navigation 之后获取数据或在 Navigation 之前获取数据:https://router.vuejs.org/guide/advanced/data-fetching.html

如果我们调整 Fetching After Navigation 示例,我们会得到以下用于显示的代码,例如 userName

<template>
  <div>
    <div class="loading" v-if="loading">
      Loading...
    </div>

    <div v-if="error" class="error">
      {{ error }}
    </div>

    <div v-if="userName" class="content">
      <h2>{{ userName }}</h2>
    </div>
  </div>
</template>

export default {
  data () {
    return {
      loading: false,
      userName: null,
      error: null
    }
  },
  created () {
    // fetch the data when the view is created and the data is
    // already being observed
    this.fetchData();
  },
  watch: {
    // call again the method if the route changes
    '$route': 'fetchData';
  },
  methods: {
    fetchData () {
      var vm = this;
      vm.error = vm.post = null;
      vm .loading = true;

      //We make the assumption that there is a Collection userProfiles
      //with documents with ids equal to the userIds
      fb.collection("userProfiles").doc(this.$route.params.userId).get()
      .then(function(doc) {
         if (doc.exists) {
           vm.loading = false;
           vm.userName = doc.data().userName;
         } else {
           vm.error = "No such document!";
         }
       })
       .catch(function(error) {
           vm.error = "Error getting document:" + error;
       });

    }
  }
}

【讨论】:

  • 太好了,我会测试它
  • 请注意,this.userName = doc.data().userName; 行中有错字。我已经更正了!
  • 我收到一个错误:无法设置未定义的属性“错误”
  • 在哪里???您必须提供有关收到此错误的哪一行的更多详细信息。查看浏览器控制台。另外,你可以安装这个 Chrome/Firefox 浏览器扩展来调试 Vue.js 应用程序github.com/vuejs/vue-devtools
  • 总体上调用 this.error 的地方,我删除了除一个之外的所有 this.error,我也得到了那里的错误
猜你喜欢
  • 1970-01-01
  • 2015-09-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多