简体   繁体   English

Angular2中的无限嵌套路由

[英]Infinite Nested Routing in Angular2

I am trying to Implement a File Explorer App in Angular2. 我正在尝试在Angular2中实现文件资源管理器应用程序。

My Home Component will be having a list of Folders and Files. 我的主页组件将包含文件夹和文件列表。 If I click on Some Folder in the List, then using Clicked Folder's name as query string i should navigate to another component which again having list of Further folder and files inside it and this can continue for some more nested levels. 如果我单击列表中的某些文件夹,然后使用单击文件夹的名称作为查询字符串,我应该导航到另一个组件,该组件再次具有其中的进一步文件夹和文件的列表,这可以继续一些更多的嵌套级别。 I want each opened folder's path to be visible in url bar (ie ./Folder1/Folder1.1/Folder1.1.2...so on.) 我希望每个打开的文件夹的路径在url栏中可见(即./Folder1/Folder1.1/Folder1.1.2...so on。)

Folder1
    Folder1.1
        Folder1.1.1
        Folder1.1.2
    Folder1.2
Folder2

Can Anyone please help me in achieving this because since component can not be used both as view and router, I am finding difficulty in achieving this because this almost infinite nesting is not allowed. 任何人都可以帮助我实现这一点,因为组件不能用作视图和路由器,我发现很难实现这一点,因为这种几乎无限的嵌套是不允许的。

Is there a reason why on each click you need to go to a separate view? 有没有理由为什么每次点击都需要进入单独的视图? Wouldn't it be simpler to just update the current view with some type of bootstrap breadcrumb representing your traversal of the file system? 用某种类型的bootstrap breadcrumb代表你遍历文件系统来更新当前视图会不会更简单?

That being said you could always do something like this. 话虽如此,你总是可以这样做。

Setting up your paths with @RouteConfig: 使用@RouteConfig设置路径:

@RouteConfig([
    {path:'/', name:'Home', component:HomeComponent},
    {path:'/dir/:name', name:'Dir',component:DirComponent}
])

Example of how you could pass the dir name as a url param: 如何将dir名称作为url参数传递的示例:

<a [routerLink]="['Dir',{name:'MyDirectory'}]">Profile</a>

Then within the constructor of your DirComponent you could get that param: 然后在你的DirComponent的构造函数中你可以得到那个参数:

constructor(private params: RouteParams) {

            let dirName = params.get('name');
}

The basic concept is that in your main component you could pass a directory name and hand it off to another route as a url param. 基本概念是,在您的主要组件中,您可以传递目录名称并将其作为url参数传递给另一个路径。

Once again, I would suggest rethinking why you need a separate route for each directory traversal, but this should give you one option for passing information between components. 再一次,我建议重新思考为什么每个目录遍历需要一个单独的路由,但这应该为您提供一个在组件之间传递信息的选项。

I would also suggest looking into sharing data between parent/child components. 我还建议在父/子组件之间共享数据。 That could be another option if you need data available to multiple components. 如果您需要可用于多个组件的数据,那么这可能是另一种选择。

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

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