【问题标题】:Angular dynamic number of paths角度动态路径数
【发布时间】:2020-11-26 11:05:21
【问题描述】:

您好,我需要为组件创建动态路径... 像这样的..

localhost:4200/category/category1/category2/category3/category4

这是解决这个问题的唯一方法吗?

  {
    path: 'category/:category1',
    component: CategoryComponent,
  },
  {
    path: 'category/:category1/:category2',
    component: CategoryComponent,
  },
  {
    path: 'category/:category1/:category2/:category3',
    component: CategoryComponent,
  },
  {
    path: 'category/:category1/:category2/:category3/:category4',
    component: CategoryComponent,
  },

或者我还有其他选择吗?

【问题讨论】:

    标签: angular dynamic path routes


    【解决方案1】:

    你必须使用:

    /category/:类型

    并且使用 api 将 :type 检索到后端使用过滤器不要做 /../...

    使用单个斜线和您的 api 提供的过滤器

    【讨论】:

      【解决方案2】:

      可以查询参数

      localhost:4200/category?cat_one=1&cat_two=2&cat_three=3&cat_four=4
      

      导入激活路由

      import {ActivatedRoute} from '@angular/router';
      

      在组件的构造函数上

      constructor(private activatedRoute: ActivatedRoute) {
          this.activatedRoute.queryParams.subscribe(params => {
              console.log(params.cat_one);
              console.log(params.cat_two);
              console.log(params.cat_three);
              console.log(params.cat_four);
          });
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-01-22
        • 2021-08-04
        • 1970-01-01
        • 2017-09-03
        • 1970-01-01
        相关资源
        最近更新 更多