【问题标题】:Angular 10: display user information on a viewAngular 10:在视图上显示用户信息
【发布时间】:2020-10-21 22:37:30
【问题描述】:

我有一个页面,其中列出了我所有的注册用户,我想创建一个操作,在其中单击用户名并打开另一个组件以显示来自此特定用户的信息。我已经创建了另一个名为 user-details 的组件。如何做到这一点的最佳方法?也许从 URL 读取 ID?

这是我的代码:

users.component.ts

import { Component, OnInit } from '@angular/core';
import { AngularFirestore } from '@angular/fire/firestore';
import { Observable } from 'rxjs';


@Component({
  selector: 'app-manage-users',
  templateUrl: './manage-users.component.html',
  styleUrls: ['./manage-users.component.scss']
})
export class ManageUsersComponent implements OnInit {

  items: Observable<any[]>;
  constructor(firestore: AngularFirestore) {
    this.items = firestore.collection('users').valueChanges();
  }

  ngOnInit(): void {
  }


}

users.component.html

<div class="card card-custom example example-compact gutter-b">    
  <div class="card-body">
    <table class="table table-striped">
        <thead>
        <tr>
          <th scope="col">Actions</th>
          <th scope="col">Sign-up Time</th>
          <th scope="col">Username</th>
          <th scope="col">Email</th>
          <th scope="col">Is Enabled</th>
          <th scope="col">Is Online</th>
          <th scope="col">Last Time Online</th>
          <th scope="col">Age</th>
          <th scope="col">Gender</th>
        </tr>
        </thead>
        <tbody>
          
          <tr *ngFor="let item of items | async">
              <td>
                <div ngbDropdown container="body">
                  <button class="btn" ngbDropdownToggle>Actions</button>
                  <div ngbDropdownMenu>
                    <button ngbDropdownItem>View</button>
                    <button ngbDropdownItem>Block</button>
                    <div class="dropdown-divider"></div>
                    <button ngbDropdownItem>Delete</button>
                  </div>
                </div>
              </td>
    
              <td>{{item.createdDate}}</td>
              <td><a target="_blank" href="/user-details">{{item.name}}</a></td>
              <td>{{item.email}}</td>
              <td>{{item.isEnabled}}</td>
              <td>{{item.online}}</td>
              <td>{{item.last_time_online}}</td>
              <td>{{item.birthday}}</td>
              <td>{{item.gender}}</td>
          </tr>

        </tbody>
      </table>
  </div>
</div>

【问题讨论】:

  • 您需要将唯一标识符传递给用户详细信息页面。前任。 href="/user-details/{{item.id}}"
  • 您可以通过多种方式做到这一点,i) pass in urlii) set on click in the localStorageiii) use @Input to pass the id to the child component.

标签: angular typescript firebase-realtime-database


【解决方案1】:

我会这样做:

  • 在服务中缓存您的用户,以便您可以通过 id 从中检索他们:
@Injectable({providedIn: 'root'})
export class UserCacheService {
  getUserById(): User {
    // Naive approach, you of course have to think about asynchronicity and error handling here.
    return this.users[id];
  }
}
  • 您使用 id 路由到 details 组件。

这样定义路线:

const routes: Routes = [
  { path: 'users/:id', component: UserDetailComponent },
];

然后当你路由到它时,你可以像这样从 url 中获取 id:

// You can e.g. do this in ngOnInit.
const id = this.activatedRoute.snapshot.paramMap.get('id');
// Think about what happens when someone opens the url while the user is not already cached!
const user = this.userCacheService.getUserById(id);

【讨论】:

  • 您好,谢谢!我做到了,但出现错误:TS1005:';'预期的。 getUserById:用户{
  • 谢谢!!现在页面加载了 url 上的参数,但我无法读取参数,我收到错误:找不到名称 'id'。我的 URL 是 /user-details/:id 我应该更改参数行 return this.users[id]; ?
  • 我放上视图:

    USER: {{ user }}

    ID: {{ id }}

  • 我收到错误:“UserDetailsComponent”类型上不存在属性“activatedRoute”。并且“UserDetailsComponent”类型上不存在属性“userCacheService”。
  • 我在 user-details.component 上导入:import {userCacheService} from './user-cache.service';
【解决方案2】:

您可以像这样将用户 ID 作为 URL 参数传递给用户

&lt;a href="/user-details/{{item.id}}" class="btn btn-link"&gt;John Mike&lt;/a&gt;

并使用 ActivatedRoute

在 user-details 组件中获取 t id 参数

  constructor(
    private readonly route: ActivatedRoute,
 
  ) { }

  ngOnInit() {
    // No Subscription
    this.snapshotParam = this.route.snapshot.paramMap.get("id");

  }

参考下面的例子

Click here

【讨论】:

  • 谢谢!一旦我这样做了,我将如何在另一个视图上阅读用户信息? {{ snapshotParam.name }} 之类的东西?
  • @FernandoAureliano 因此,您将根据 ID 从 snapshotParam 中获取 ID,您可以查询数据库以获取详细信息。
  • 我以最简单的方式遵循代码示例,但它不适用于使用路由 /manage-users 的项目。我改变这一行 this.router.navigate(["animals", animal]);到 this.router.navigate(["manage-users/animals", animal]);还是不行
  • 它可以获取参数,但是当我尝试 const user = firestore.collection('users').doc(this.snapshotParam);我无法在此常量内从用户那里获取参数。我收到错误:找不到名称“firestore”。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2019-09-21
  • 2019-11-24
  • 1970-01-01
  • 2011-03-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多