[英]Angular2 Routing: redirectTo a dynamically generated string
Per default I need to redirect to today's date. 默认情况下,我需要重定向到今天的日期。 Here is the routing config I have for now:
这是我现在的路由配置:
import { CALENDAR_ROUTE } from './_methods/utils';
export const appRoutes: Routes = [
{
path: CalendarComponent.path,
component: CalendarComponent,
canActivate: [AuthGuard],
resolve: refDataResolver,
data: {
navi: {
at: 'main',
icon: 'navbar-calendar'
},
roles: {
employeeOnly: true
}
},
children: [
{
path: '',
pathMatch: 'full',
redirectTo: CALENDAR_ROUTE.DEFAULT_MONTH
},
{
path: LAYOUT_MONTH,
children: [
{
path: '',
pathMatch: 'full',
redirectTo: CALENDAR_ROUTE.MONTH
},
{
path: ':year/:month',
component: CalendarMonthViewComponent
},
]
},
{
path: LAYOUT_DAY,
children: [
{
path: '',
pathMatch: 'full',
redirectTo: CALENDAR_ROUTE.DAY
},
{
path: ':year/:month/:day',
component: CalendarDayViewComponent
},
]
}
]
}
];
Here is how I generate the strings in utils.ts : 这是我在utils.ts中生成字符串的方法 :
import { LAYOUT_MONTH } from './../_classes/const';
import * as moment from 'moment';
export function getRoute(withDay: boolean, prefix?: string): string {
const now = moment();
const day = ( withDay ) ? now.format('DD') : null;
return [prefix, now.format('YYYY'), now.format('MM'), day]
.filter( str => !!str)
.join('/');
}
export const CALENDAR_ROUTE = {
DEFAULT_MONTH: getRoute(false, LAYOUT_MONTH),
MONTH: getRoute(false),
DAY: getRoute(true)
};
It all goes well - in dev mode and localhost – until I need to compile it for UAT/PROD, which utilizes AOT builds, which then throws a 这一切顺利 - 在开发模式和localhost - 直到我需要为UAT / PROD编译它,它利用AOT构建,然后抛出一个
ERROR in Error during template compile of 'AppRoutingModule'
Function calls are not supported in decorators but 'getRoute' was called in 'appRoutes'
'appRoutes' calls 'getRoute' at app/app-routing.module.ts(67,42).
I haven't yet found a solution to this problem. 我还没有找到解决这个问题的方法。 There are some issues which would use
providers
and useValue
- but it feels like an overkill right here. 有一些问题会使用
providers
和useValue
- 但在这里感觉就像是一种过度useValue
。 Maybe there is a different kind of a workaround? 也许有一种不同的解决方法? Maybe I should structure my components differently?
也许我应该以不同的方式构建组件? Any ideas?
有任何想法吗?
One solution may be to inject the Router
into your component and then update the route config at runtime. 一种解决方案可能是将
Router
注入组件,然后在运行时更新路由配置。
I'v not tested this myself, but the idea would be something like this: 我自己没有测试过,但想法是这样的:
In your component 在您的组件中
constructor(private router: Router) {
this.router.config.unshift({path: '', component: '', redirectTo: 'Your new redirect path'})
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.