代码之家  ›  专栏  ›  技术社区  ›  Yogesh Aggarwal

角度8中的请求

  •  0
  • Yogesh Aggarwal  · 技术社区  · 6 年前

    urlpatterns = [
        path("view/<id:str>", include(views.urls))
    ]
    

    http://localhost:4200/view/32
    

    3 回复  |  直到 6 年前
        1
  •  1
  •   Norbert Bartko    6 年前

    您可以在路由器配置中设置此项:

    { path: 'view/:id', component: ViewComponent }
    

    然后进入 id

    constructor(
      private route: ActivatedRoute,
    ) { }
    
    ngOnInit() {
      this.route.params.subscribe( params => {
        this.id = params.id;
        console.log(this.id) 
      });
    }
    
        2
  •  0
  •   Nikolai Kiefer    6 年前

    rouing.module .

    const routes: Routes = [
      {path: '', component: FirstComponent},
      {path: 'tools/:article', component: ViewComponent}
      ];
    
    @NgModule({
      imports: [RouterModule.forRoot(routes)],
      exports: [RouterModule],
      providers: [
        {provide: APP_BASE_HREF, useValue: '/'}
      ]
    })
    export class AppRoutingModule {
    }
    

    然后可以在ViewComponent中访问项目编号

      constructor(private route: ActivatedRoute)
    

    然后在你的ngOnInit中订阅

     this.route.params.subscribe(result => { 
        console.log(result.article)}
    
        3
  •  0
  •   StepUp    6 年前

    您需要创建一个组件。例如。 ProductComponent app.module.ts (或其他模块)您应该编写路由:

    @NgModule({
      declarations: [
        ProductFormComponent,
        AdminProductComponent,
        AdminOrderComponent
      ],
      imports: [
        SharedModule,
        RouterModule.forRoot([          
          { path: 'products/:id',
            component: ProductComponent
          },
          { path: 'admin/products',
            component: AdminProductComponent
          },
          {
            path: 'admin/orders',
            component: AdminOrderComponent
          }
        ])
      ]      
    })
    export class AppModule { }
    

    让我们看看这条路: path: 'products/:id' ,在哪里 products/ 是你的观点(就像在Django) 和 :id id product )

    More info can be seen here in Angular docs

    推荐文章