是的。有多种方法可以实现您想要的目标。您可以使用router.resetConfig
使用新配置重置路由器的配置:
this.router.resetConfig([
{ path: 'somePath', component: SomeComponent},
...
]);
你也可以push
路由到配置:
this.router.config.push({ path: 'somePath', component: SomeComponent });
我这样做的另一种方法是,我创建了一种用户可以使用 TinyMCE 之类的东西构建页面并指定他们希望页面具有的 url 路径的方法。在 Angular 中,我在路由器中指定了通配符路由{ path: '**', component: ComponentBuilder }
,其中 ComponentBuilder 组件将从数据库中获取与某人请求的路径相对应的模板,然后使用该模板动态生成组件。如果数据库中不存在模板,则正常Page cannot be found
模板已渲染。
两种方法都有效。这取决于您希望应用程序如何运行以及如何构建用户创建的页面。
自从我在版本 2 rc4 中提出解决方案以来,有更多关于如何创建动态组件的文档和示例。我没有使用过其中任何一个,但看起来很有帮助:
动态组件的 NPM 包 https://www.npmjs.com/package/angular2-dynamic-component
关于动态组件加载器的 Angular 文档 https://angular.io/docs/ts/latest/cookbook/dynamic-component-loader.html
动态创建组件 http://blog.rangle.io/dynamically-creating-components-with-angular-2/
另一篇关于动态创建组件的文章 https://blog.lacolaco.net/post/dynamic-component-creation-in-angular-2/