简体   繁体   English

带参数的角路由

[英]Angular Routing with parameter

I am trying to implement an edit button, where it should navigate to the page that is meant to be edited. 我正在尝试实现一个“编辑”按钮,该按钮应导航到要编辑的页面。 I know that parameterised Routes should be used, but I am not sure how exactly it should be implemented in relation to this code? 我知道应该使用参数化的Routes,但是我不确定相对于此代码应该如何实现? could someone please come with a suggestion or links of tutorials that I can solve my problem as well as I want to learn more in depth about Routing in Angular later on. 有人可以提出建议或教程链接来解决我的问题,以及稍后我想进一步深入了解“ Angular路由”。 I did read the Angular documentation on Routing, but it didn't help me much. 我确实阅读了有关路由的Angular文档,但并没有太大帮助。

export interface FileModel {
  Id?: Array<string>;
  dbList?: Array<string>;
  name?: string;
}

export interface File {
  fileId?: number;
  name?: string;
  dbList?: string;
}

public editFile: FileModel  = {};

      edit(file: File){
         this.editFile.dbList = file.dbList.split(',');
         this.editFile.name = file.name;

         // navigate and send

      }

Firstly you need to inject the Router in the constructor: 首先,您需要将Router注入构造函数中:

constructor(private router: Router) {}

Then in your code where you would like to navigate do something like this: 然后在您要导航的代码中执行以下操作:

this.router.navigate([`/navigateUrlPart/${file.id}`]);

And of course you should create a routing in your AppRoutingModule to keep it working together 当然,您应该在AppRoutingModule创建一个路由以使其协同工作

const routes: Routes = [
    ...
    { navigateUrlPart/:id', component: YourEditComponent },
];

@NgModule({
    imports: [RouterModule.forRoot(routes})],
    exports: [RouterModule]
})
export class AppRoutingModule { }

And in the YourEditComponent you can read the edited id from the route params using the ActivatedRoute class YourEditComponent您可以使用ActivatedRoute类从路由参数中读取已编辑的id

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

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