gpt4 book ai didi

Angular 2 : Define two Async routes on the same path conditionally/alternatively

转载 作者:太空狗 更新时间:2023-10-29 18:26:59 24 4
gpt4 key购买 nike

我可以借助一些帮助来使用 RouterConfig 中的 AsyncRoute。我有两个组件,我需要加载它们并将它们放在同一路径上。

我必须做一些 if else 语句,我可以控制应该加载哪个组件。

代码:

@RouteConfig([
{ path: '/', name: 'Bookings', component: BookingsComponent },
{ path: '/bookings', name: 'Bookings', component: BookingsComponent, useAsDefault: true },
new AsyncRoute({
path: '/mian/:id/...',
loader: () => this.getMainComponent(),
name: 'Main'
}),
{ path: '/main/:id/...', name: 'Main', component: MainComponent },
{ path: '/main/:id/passenger', name: 'Passenger', component: PassengerComponent },
{ path: '/main/:id/date', name: 'Date', component: DateComponent },
{ path: '/main/:id/vehicle', name: 'Vehicle', component: VehicleComponent },
{ path: '/main/:id/confirmation', name: 'Confirmation', component: ConfirmationComponent },
{ path: '/main', redirectTo: ["Bookings"] },
])

@Injectable()
export class AmendmentComponent {

locale: string;

constructor() {

this.locale = localStorage.getItem('locale');
}

getMainComponent() {

if (this.locale == "de") {
return MainTestComponent;
}
else {
return MainComponent;
}

}
}

我导入的组件:

import { MainComponent } from '../amendmentMain/mainComponent';
import { MainTestComponent } from '../amendmentMain/mainTestComponent';

如您所见,我在 routerConfig 中实现了新的 AsyncRoute。路径是 '/main/:id/...'

如果 locale 等于 "de",则应加载 MainTestComponent,否则应加载 MainComponent。

我做错了什么?

系统更新报错,typescript 找不到:

setMainComponent() {

if (this.locale == "de") {

this.router.config([
new AsyncRoute({
path: '/main:id/...',
loader: () => System.import(MainComponent).then(m => m.MainTestComponent),
name: 'MainComponent'
})
])
}
else {

}

}

pdate: 添加了一些 index.html 文件

  <!-- base url -->
<base href="/">

<link href="/assets/css/site.css" rel="stylesheet">

<!-- Include lock0 module -->
<script src="//cdn.auth0.com/js/lock-8.1.min.js"></script>

<script>
System.config({
defaultJSExtensions: true
});
</script>

</head>
<body>

<app>
Loading...
</app>

{% if (o.webpackConfig.metadata.ENV === 'development') { %}
<!-- Webpack Dev Server reload -->
<script src="http://{%= o.webpackConfig.metadata.host %}:{%= o.webpackConfig.metadata.port %}/webpack-dev-server.js"></script>
{% } %}

<!-- Webpack HtmlWebpackPlugin manually inject scripts -->
{% for (var chunk in o.htmlWebpackPlugin.files.chunks) { %}
<script src="{%= o.htmlWebpackPlugin.files.chunks[chunk].entry %}"></script>
{% } %}

最佳答案

可以像这样动态的为同一条路径注册不同的路由

@RouteConfig([
{ path: '/', name: 'Bookings', component: BookingsComponent },
{ path: '/bookings', name: 'Bookings', component: BookingsComponent, useAsDefault: true },
{ path: '/main/:id/passenger', name: 'Passenger', component: PassengerComponent },
{ path: '/main/:id/date', name: 'Date', component: DateComponent },
{ path: '/main/:id/vehicle', name: 'Vehicle', component: VehicleComponent },
{ path: '/main/:id/confirmation', name: 'Confirmation', component: ConfirmationComponent },
{ path: '/main', redirectTo: ["Bookings"] },
])

@Injectable()
export class AmendmentComponent {

locale: string;

constructor(public router:Router) {

this.locale = localStorage.getItem('locale');
this.setMainComponent();
}

setMainComponent() {

if (this.locale == "de") {
this.router.config([
new AsyncRoute({path: '/main/:id/...',
loader: () => System.import('app/path/to/MainTestComponent').then(m => m.MainTestComponent), name: 'MainComponent'}];
}
else {
this.router.config([
new AsyncRoute({path: '/main/:id/...',
loader: () => System.import('app/path/to/MainComponent').then(m => m.MainComponent), name: 'MainComponent'}];
}

}
}

没有异步路由:

   if (this.locale == "de") {
this.router.config([
{ path: '/main/:id/...', name: 'MainComponent', component: MainTestComponent}];
}
else {
this.router.config([
{ path: '/main/:id/...', name: 'MainComponent', component: MainComponent}]);
}

关于 Angular 2 : Define two Async routes on the same path conditionally/alternatively,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36134511/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com