Angular2中的通配符嵌套路由

时间:2016-10-14 06:45:24

标签: angular angular2-routing

如何使用Angular 2路由器完成深度嵌套的通配符路由。所有通配符示例代码都倾向于处理Page Not Found示例。假设以下用例。您有一个程序充当文件系统上的文件夹查看器。示例路由可能包括:

  • /home/Documents/Personal
  • /home/Pictures/Vacation/2015
  • /home/Pictures/Vacations/2015/10

到目前为止,我已尝试过以下内容:

const r = [
  {
    path: 'home',
    component: HomeComponent,
    children: [
      {
        path: '**',
        component: FolderViewerComponent
      }
    ]
  }
];

这种作品。我可以使用以下内容访问URL和部分:

export class FolderViewerComponent implements OnInit {
  constructor(private router: Router, private route: ActivatedRoute ) {
  }

  ngOnInit() {
    console.log(this.route.url);
  }
}

但是,当我导航相同时,由于新路由使用相同的组件,因此不会再次触发ngOnInit。因此,我没有办法从服务器请求新的文件夹内容。

所以在一天结束时:

  1. 我有更好的方法吗?
  2. 如果没有,我可以在路线改变时订阅,即使该组件没有?

1 个答案:

答案 0 :(得分:1)

我认为可能有更好的方法,但基于一些评论,我最终确定了以下内容。基本上我订阅路由更改并在更改时执行代码。

Traceback (most recent call last):
  File "/Users/jason/dev/gain-data/venv/lib/python2.7/site-packages/nose/loader.py", line 418, in loadTestsFromName
    addr.filename, addr.module)
  File "/Users/jason/dev/gain-data/venv/lib/python2.7/site-packages/nose/importer.py", line 47, in importFromPath
    return self.importFromDir(dir_path, fqname)
  File "/Users/jason/dev/gain-data/venv/lib/python2.7/site-packages/nose/importer.py", line 94, in importFromDir
    mod = load_module(part_fqname, fh, filename, desc)
  File "/Users/jason/dev/gain-data/data/storage/__init__.py", line 4, in <module>
    from google.cloud.storage import Blob, Client
  File "/Users/jason/dev/gain-data/lib/google/cloud/storage/__init__.py", line 42, in <module>
    from google.cloud.storage.batch import Batch
  File "/Users/jason/dev/gain-data/lib/google/cloud/storage/batch.py", line 30, in <module>
    from google.cloud.storage.connection import Connection
  File "/Users/jason/dev/gain-data/lib/google/cloud/storage/connection.py", line 17, in <module>
    from google.cloud import connection as base_connection
  File "/Users/jason/dev/gain-data/lib/google/cloud/connection.py", line 31, in <module>
    get_distribution('google-cloud-core').version)
  File "/Users/jason/dev/gain-data/venv/lib/python2.7/site-packages/pkg_resources/__init__.py", line 557, in get_distribution
    dist = get_provider(dist)
  File "/Users/jason/dev/gain-data/venv/lib/python2.7/site-packages/pkg_resources/__init__.py", line 431, in get_provider
    return working_set.find(moduleOrReq) or require(str(moduleOrReq))[0]
  File "/Users/jason/dev/gain-data/venv/lib/python2.7/site-packages/pkg_resources/__init__.py", line 968, in require
    needed = self.resolve(parse_requirements(requirements))
  File "/Users/jason/dev/gain-data/venv/lib/python2.7/site-packages/pkg_resources/__init__.py", line 854, in resolve
    raise DistributionNotFound(req, requirers)
DistributionNotFound: The 'google-cloud-core' distribution was not found and is required by the application