简体   繁体   English

直接路由到动态路由 Angular 时从 API 检索数据

[英]Retrieve data from an API when routing directly to a dynamic route Angular

The question is: How would I retrieve data from an API when routing directly to a route with a dynamic value inside of it (eg a username)问题是:当直接路由到内部具有动态值(例如用户名)的路由时,我将如何从 API 检索数据

  • If I was to for example create a social media angular web application that gets its data for each user from an API, how would I populate the data on the page by routing directly to that users page.例如,如果我要创建一个社交媒体 angular web 应用程序,该应用程序从 API 获取每个用户的数据,我将如何通过直接路由到该用户页面来填充页面上的数据。
  • EG -> With Instagram, you can go directly to 'instagram.com/{username}' and it will get that users data and populate the page with it no matter what user you go to EG -> 使用 Instagram,您可以将 go 直接发送到“instagram.com/{username}”,无论您的 go 是哪个用户,它都会获取用户数据并使用它填充页面
    • so with angular if I was to route directly to 'mywebapp.com/users/chumberjosh' without going through any other page, how would I get the data for the user 'chumberjosh' on init of the page without going through services etc. so that whatever user I go to, it will get that users information.因此,使用 angular 如果我要直接路由到“mywebapp.com/users/chumberjosh”而不通过任何其他页面,我将如何在页面初始化时获取用户“chumberjosh”的数据而不通过服务等所以无论我是 go 的用户,它都会获得该用户的信息。
  • I understand that you can route from eg 'mywebapp.com/users/' to 'mywebapp.com/users/chumberjosh' using services to store and retrieve data but I don't understand how it would work without the data being stored anywhere before the page is loaded.我知道您可以使用服务来存储和检索数据,例如从“mywebapp.com/users/”路由到“mywebapp.com/users/chumberjosh”,但我不明白如果之前没有将数据存储在任何地方,它将如何工作页面已加载。
  • The way I think it will work is by just loading the page but without any data in it我认为它的工作方式是加载页面但没有任何数据

The route would look like this路线看起来像这样

{ path: '/users/:username', component: 'userDetailedComponent' } { 路径:'/users/:username',组件:'userDetailedComponent' }

The solution would be to gather the route parameter from the url.解决方案是从 url 收集路由参数。

{ path: '/users/:username', component: 'userDetailedComponent' }

From the above you are passing in the username along with the route.从上面您将传递用户名和路由。 Now at ngOnInit you can gather the user name from the url by the code below现在在 ngOnInit,您可以通过以下代码从 url 收集用户名

import { ActivatedRoute } from '@angular/router';
import { Component, OnInit } from '@angular/core';
import { Service } from './service.service';

@Component({
  selector: 'app-userdetails',
  templateUrl: './userdetails.component.html',
  styleUrls: ['./userdetails.component.css']
})

export class UserdetailsComponent implements OnInit {

  constructor( private service: Service, private router1: ActivatedRoute) { }
  username:string ='';
  ngOnInit(): void {    
    this.username = this.router1.snapshot.paramMap.get('username'));
    this.service.getdetails('username').subscribe((data)=>{
      console.log(data);

    });
  }

In the above code, ongOnInit, we have declare a variable reading the url parameter using the ActivatedRoutes .在上面的代码 ongOnInit 中,我们使用ActivatedRoutes声明了一个读取 url 参数的变量。

this.router1.snapshot.paramMap.get('username')) will fetch the username from the url. this.router1.snapshot.paramMap.get('username'))将从 url 获取用户名。 This user name may be send to the service and the required data may be fetched.该用户名可以被发送到服务并且可以获取所需的数据。

Hope this helps.希望这可以帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM