【问题标题】:How to structure CRUD routes correctly in Angular projects?如何在 Angular 项目中正确构建 CRUD 路由?
【发布时间】:2018-09-14 15:22:22
【问题描述】:

问题:是否存在定义单页应用程序路由的最佳实践?

在 Angular 项目中,功能通常在延迟加载模块中分离,然后在 AppRoutingModule 和延迟加载模块中配置路由。

假设应用将管理目录,例如:产品。路由可以这样配置:

选项 1:

  • 名单:/products
  • 创建:/products/create
  • 阅读:/products/:id
  • 更新:/products/:id/edit

它可以工作,但看起来有点乱,/products/:id/products/create 之间存在一些歧义,因为参数:id 可以匹配字符串“create”。示例代码:

app-routing.module.ts:

const routes: Routes = [
    {
        path: '',
        children: [
            { path: 'products', loadChildren: 'app/products/products.module#ProductsModule' },
        ]
    }
];

products-routing.module.ts

const routes: Routes = [
    { path: '', component: ListProductsComponent },
    { path: 'create', component: CreateProductComponent },
    { path: ':id', component: ViewProductComponent },
    { path: ':id/edit', component: EditProductComponent },
];

选项 2

  • 名单:/products
  • 创建:/products/create
  • 阅读:/product/:id(注意“产品”是单数)
  • 更新:/product/:id/edit(注意“产品”是单数)

没有歧义,但配置变得更乱:

app-routing.module.ts:

const routes: Routes = [
    {
        path: '',
        children: [
            // Empty path. It works as long as the ProductsModule has no empty paths. You can define more lazy modules like this.
            { path: '', loadChildren: 'app/products/products.module#ProductsModule' },
        ]
    }
];

products-routing.module.ts

const routes: Routes = [
    { path: 'products', component: ListProductsComponent },
    { path: 'products/create', component: CreateProductComponent },
    { path: 'product/:id', component: ViewProductComponent },
    { path: 'product/:id/edit', component: EditProductComponent },
];

如您所见,在定义路由时,您必须考虑惰性模块的结构和 URL 的“美”。

定义路线的最佳做法是什么,特别是对于 CRUD 操作?有没有好的命名约定?

【问题讨论】:

    标签: angular routes angular-cli lazy-loading angular-router


    【解决方案1】:

    对于 CRUD 应用程序,我不喜欢所有文件路径样式。相反,我更愿意为操作使用额外的参数。

    enter code here
    List: /products
    Create: /products/;action=new
    Update: /products/123456;action=change
    Read: /products/123456;action=display
    

    请参阅我关于如何构建真正的 CRUD 应用程序的冗长博客:https://vincezk.github.io/Portal/

    【讨论】:

      【解决方案2】:

      我知道的关于 Angular 的唯一约定/最佳实践来自官方 Style guide,它没有提及任何关于路由的内容。但绝对是推荐/好书。

      我个人会将“创建”和“编辑”放入另一个具有不同根 url 的模块中。

      admin/product/list
      admin/product/edit/:id
      admin/product/create
      

      否则我会使用您的选项 1 的略微修改版本。

      List: /products
      Create: /products/create
      Update: /products/edit/:id
      Read: /products/:id
      

      简洁易读的代码 > 网址的“美”

      【讨论】:

      • “我个人会将‘创建’和‘编辑’放到另一个具有不同根 url 的模块中。”您能否详细说明一下,您为什么要这样做?或者也许用示例路由编辑你的答案?
      • 它主要归结为提供一些关注点分离......关键是要有一个延迟加载的功能模块,其中包含一些路由保护等与管理相关的功能。由于“读取”操作是最常见的操作,通常不需要对用户进行身份验证(至少在目录/产品应用程序中不需要)。虽然“创建、更新和删除”操作在本质上有所不同。
      • 您的修改版本没有为/products/:id/products/create之间的操作模糊提供解决方案
      猜你喜欢
      • 2012-09-23
      • 1970-01-01
      • 1970-01-01
      • 2019-05-30
      • 1970-01-01
      • 2016-05-15
      • 2016-12-25
      • 1970-01-01
      • 2017-05-07
      相关资源
      最近更新 更多