简体   繁体   English

通过在 URL 中插入参数在 angular 中路由

[英]Routing in angular by insert parameter in URL

Suppose I have a component A which routes on localhost:4200/A I want to render A also when I pass parameters after A, like localhost:4200/A/xyz without any button click.假设我有一个在localhost:4200/A由的组件 A,当我在 A 之后传递参数时,我也想渲染 A,例如localhost:4200/A/xyz ,无需单击任何按钮。

Simply if I hit the URL localhost:4200/A/xyz it should render component A here xyz is dynamic it may be changing.简单地说,如果我点击 URL localhost:4200/A/xyz它应该呈现组件 A,这里的xyz是动态的,它可能会发生变化。

You can add a route definition with a parameter like this:您可以使用如下参数添加路由定义:

{
        path: 'A/:id', component: AComponent
    },

Then in AComponent然后在AComponent

import { ActivatedRoute } from '@angular/router';
@Component({
  selector: 'app-a',
  templateUrl: './a.component.html',
  styleUrls: ['./a.component.css']
})
export class AComponent{
  data: string;
  constructor(private activatedRoute: ActivatedRoute) {
     this.loadData();
  }

  loadData() {
    this.activatedRoute.paramMap.subscribe(params => {
      this.data= params.get('id');

    });
  }

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

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