How to handle / ignore a bad route with durandal?

后端 未结 2 823
南旧
南旧 2021-01-03 10:43

I\'m trying to conditionally block a route from being accessed. I think this can be done with guardRoute: http://durandaljs.com/documentation/Router/

2条回答
  •  温柔的废话
    2021-01-03 10:56

    You should use guardRoute before activating the router e.g. in shell.js. The example is taken from a Durandal 2.0 alpha site. AFAIK guardRoute hasn't changed from 1.2, but setting a breakpoint will allow you to figure out what arguments are passed in for 1.2. As a general rule, return true to allow navigation, false to prevent it and a hash or url value to redirect.

    define(['plugins/router'], function (router) {
    
        // Redirecting from / to first route in route.map
        router.guardRoute = function(routeInfo, params, instance){
            if (params.fragment === ''){
                return routeInfo.router.routes[0].hash;
            }
            return true;
        };
        ...    
    
        return {
            ...
            router: router,
            activate: function () {
                router.map([
                   ---
                ]);
    
                return router.activate();
            }
        };
    });
    

提交回复
热议问题