Skip to content

Can't use method to generate route paths with compiler-cli #10731

Closed

Description

@sod

I'm submitting a ... (check one with "x")

[?] bug report
[x] feature request

Current behavior

export const generateRoute = function(route: string, params?: Object): string {
    return decodeURIComponent(Routing.generate(route, params).substr(1));
};

export const routes: Routes = [
    {
        path: generateRoute('first_action'),
        component: MyFirstAction,
    },
    {
        path: generateRoute('second_action', {arg: ':arg'}),
        component: MySecondAction,
    },
];

ngc error:

Error: Error encountered resolving symbol values statically. Calling function 'generateRoute', function calls are not supported. Consider replacing the function or lambda with a reference to an exported function, resolving symbol

generateRoute generates a Route by name defined by the backend API. On each locale the route name can differ. If it has to be static, we were required to create seperate builds for each locale.

Expected/desired behavior

Don't require the path to be known by ngc. Why does it have to? Does compile cli generate a custom router for the given routes?

Activity

  1. sod commented on Aug 15, 2016

    @sod
    ContributorAuthor

    Here is what worked with both, JIT and AoT: {provide: ROUTES, multi: true, useFactory: _routes}.

    export const generateRoute = function(route: string, params?: Object): string {
        return decodeURIComponent(Routing.generate(route, params).substr(1));
    };
    
    export const _emptyRoutesArray = [];
    export const _routesEntryActions = [
        MyFirstAction,
        MySecondAction,
    ];
    
    export function _routes() {
        return [
            {
                path: generateRoute('first_action'),
                component: MyFirstAction,
            },
            {
                path: generateRoute('second_action', {arg: ':arg'}),
                component: MySecondAction,
            },
        ];
    }
    
    @NgModule({
        imports: [
            RouterModule.forRoot(_emptyRoutesArray),
        ],
        providers: [
            {provide: ROUTES, multi: true, useFactory: _routes},
        ],
        entryComponents: [
            _routesEntryActions
        ],
        bootstrap: [
            MySinglePageApp,
        ],
    })
    export class MySinglePageModule {
    }

    Sadly the ROUTES token is not exported from @angular/router but instead from import {ROUTES} from '@angular/router/src/router_config_loader';.

    Could you make it public? Or instead make RouterModule.forRoot to also accept factories?

  2. sod commented on Aug 15, 2016

    @sod
    ContributorAuthor

    Closing as I might just as well create a pull request later.

  3. angular-automatic-lock-bot commented on Sep 9, 2019

    @angular-automatic-lock-bot

    This issue has been automatically locked due to inactivity.
    Please file a new issue if you are encountering a similar or related problem.

    Read more about our automatic conversation locking policy.

    This action has been performed automatically by a bot.

  4. locked and limited conversation to collaborators on Sep 9, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions