如何处理/忽略durandal的错误路线? [英] How to handle / ignore a bad route with durandal?

查看:51
本文介绍了如何处理/忽略durandal的错误路线?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正试图有条件地阻止路由访问。我认为可以使用guardRoute完成此操作: http://durandaljs.com/documentation/Router/

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


function guardRoute(routeInfo,params,instance):object -在任何路由之前激活后,将调用guardRoute函数。您可以插入此功能以添加自定义逻辑,以基于请求的路由允许,拒绝或重定向。要允许,请返回true。要拒绝,返回false。要重定向,请返回带有哈希或URL的字符串。您也可以为这些值中的任何一个返回承诺。

function guardRoute(routeInfo, params, instance) : object - Before any route is activated, the guardRoute function is called. You can plug into this function to add custom logic to allow, deny or redirect based on the requested route. To allow, return true. To deny, return false. To redirect, return a string with the hash or url. You may also return a promise for any of these values.

我不确定如何指定应访问的路由,或者如果我需要拒绝访问,如何重新路由到另一个视图。有人可以以此方式发布其使用示例吗?

I'm not sure how to specify which route should be accessed though, or how to re-route to another view if I need to deny access. Can someone post an example of its use in this manner?

推荐答案

在激活路由器之前,应使用 guardRoute 。在 shell.js 中。
该示例取自Durandal 2.0 alpha网站。 AFAIK GuardRoute的版本从1.2不变,但是设置断点将使您能够确定1.2传入的参数。
作为一般规则,返回 true 允许导航,返回 false 防止导航,并返回哈希 url 值进行重定向。

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();
        }
    };
});

这篇关于如何处理/忽略durandal的错误路线?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆