【问题标题】:Angular 6 passing in parameters into URLAngular 6 将参数传入 URL
【发布时间】:2019-04-26 14:00:28
【问题描述】:

所以我有一个 Angular 应用程序,该应用程序当前具有诸如“urlname/stockdata”之类的路由。我希望能够将股票代码传递到该 URL,例如“urlname/stockdata/AAPL”。我的应用程序正在使用该符号调用第三方 API。你们对我如何做到这一点有什么建议吗?任何帮助表示赞赏。谢谢

【问题讨论】:

    标签: javascript angular routing


    【解决方案1】:

    您可以使用如下参数声明路由:

    export const routes: Routes = [
      { path: 'urlname/stockdata/:id', component: MyComp}
    ];
    

    在模板中创建链接:

    <a [routerLink]="['/urlname/stockdata', id]">MyLink</a>
    

    或者从.ts导航:

     this.router.navigate(['/urlname/stockdata', id]);
    

    然后你可以阅读它:

    constructor(private route: ActivatedRoute) {}
    
    this.route.params.subscribe(params => {
      this.id = params['id'];
    });
    

    【讨论】:

      【解决方案2】:

      TS 文件

      const routes: Routes = [{
        path: '',
        children: [{
          path: 'plans/:id',
          component: PlanListComponent,
          canActivateChild: [AuthGuard]
        }]
      }];
      

      HTML

      <a class="dropdown-item" (click)="viewDetails(product.id)">View Detail</a>
      

      TS

       viewDetails (productId) {
              this.router.navigate([ BASE_URL +"/plans/" + productId]);
          }
      

      【讨论】:

        【解决方案3】:

        首先,考虑您必须在 URL 中发送 4 个参数,例如。 MPJC
        为此转到文件 sample.component.ts

        
         functionName() {
         let param={
              zone:this.selectZone,
              state:this.selectState,
              jc:this.stateJC,
              mp:this.MPdata
            }
        
         this.availservice.AvailReport(param)
              .subscribe((json) => {
              this.ArrayResponse= json;
              } );
        }
        

        现在来归档 sample.service.ts

         AvailReport(param) {  
        let UrlTemp= `http://11.111.11.11:3838/getdata/AVAILABILITY_${param.zone}_${param.state}_${param.jc}_${param.mp}.JSON?callback=true`;
            return this.http.get(UrlTemp);
          }
        

        你肯定会得到这样的回应:

        Request URL: http://10.141.55.49:3838/getdata/AVAILABILITY_ZoneName_StateName_MPName_JCName.JSON?callback=true
        

        使用此方法,您可以在 URL 中传递任意数量的参数并要求您的后端团队接收这些参数。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2019-10-26
          • 2017-02-12
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2017-05-08
          • 1970-01-01
          相关资源
          最近更新 更多