将角匹配器用于路由抛出“考虑将函数表达式更改为导出函数”

问题描述 投票:1回答:1

我想知道你们是否可以帮助您编写此代码!因为我觉得我不省人事

这是\helpers\matcher.ts(4,12)

import { UrlMatcher , UrlSegment } from "@angular/router";

export function match(path: string): UrlMatcher {
    return function matcher(url: UrlSegment[], group) {
        console.log(path, url, group);
        if (url.length > 0 && url[0].path === path) {
            return { consumed: [url[0]] };
        }
        return null;
    };
}

这是我执行ng --prod时遇到的错误

src \ app \ app.module.ts(124,30)中的错误:'AppModule'的模板编译期间出错

'rootRouterConfig'中的装饰器不支持函数表达式'rootRouterConfig'引用'match''match'包含src \ app \ shared \ helpers \ matcher.ts(4,12)处的错误考虑将函数表达式更改为导出函数

但是它可以编译并与ng build和ng serve完美配合。

这里是app.routes路径

export const rootRouterConfig: Routes = [

    { path: ':param', component: HomeComponent,
        children: [
            { matcher: match('job-spec/:id'), component: JobSpecDetailsComponent },
            { path: '**',  redirectTo: '404' },
        ]
    },
];

这是app.module

@NgModule({
    imports: [
        ....
        RouterModule.forRoot(rootRouterConfig, {
            useHash: false,
            scrollPositionRestoration: 'enabled',
            anchorScrolling: 'enabled',
        }),
        ....
angular routing matcher
1个回答
1
投票

我无法测试,但请尝试以下操作:

export function match(url: UrlSegment[]): UrlMatcher {
    if (url.length > 0 && url[0].path === path) {
       return { consumed: [url[0]] };
    }
    return null;
}

并用作:

{ matcher: match, component: JobSpecDetailsComponent },

URL具有路径,但是不确定您为什么要再次传递路径。静态使用路径,查看其是否可以编译,然后找出您的路径。

© www.soinside.com 2019 - 2024. All rights reserved.