DurandalJS路由行为 [英] DurandalJS Routing Behavior

查看:92
本文介绍了DurandalJS路由行为的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我拥有的东西

试图了解正在发生的事情以及如何控制它。对于尚未经过身份验证的用户,我有一个公共视图,对于经过身份验证的用户,我有一个主页视图。这是我的路线配置:

Trying to understand what's going on and how to control it. I have a "public" view for users that have not yet been authenticated, and a "home" view for users that are authenticated. Here's my route config:

app.start().then(function() {
        //Replace 'viewmodels' in the moduleId with 'views' to locate the view.
        //Look for partial views in a 'views' folder in the root.
        viewLocator.useConvention();

        //configure routing
        router.useConvention();

        router.mapRoute('home', 'viewmodels/home', 'Test App', true);
        router.mapRoute('public', 'viewmodels/public', 'Test App', true);
        router.mapRoute('set/:id', 'viewmodels/set', 'Set');
        router.mapRoute('folder/:id', 'viewmodels/folder', 'Folder');
        router.mapRoute('api', 'viewmodels/api', 'API Reference');
        router.mapRoute('error', 'viewmodels/error', 'Error', false);
        app.adaptToDevice();

        //Show the app by setting the root view model for our application with a transition.

        if (dataservice.isAuthenticated() === true) {
            app.setRoot('viewmodels/shell', 'entrance');
            router.navigateTo('home');
        } else {
            app.setRoot('viewmodels/public');
            router.navigateTo('#/public');
        }

        router.handleInvalidRoute = function (route, params) {
            logger.logError('No route found', route, 'main', true);
            router.navigateTo('#/error');
        };
    });

问题

当我第一次运行应用程序时,我没有通过身份验证,而且收到错误:

When I run the app for the first time, I'm not authenticated, and I get an error:

Uncaught TypeError: Cannot call method 'lookupRoute' of undefined 

源自'路由器.navigateTo('#/ public');'行。

然后当我尝试点击登录按钮时,我从此得到同样的错误:

Then when I try to click the login button, I get the same error from this:

define(['durandal/app', 'durandal/plugins/router', 'services/dataservice'], function (app, router, dataservice) {
   var publicViewModel = function () {
        self.logIn = function () {
            app.setRoot('viewmodels/shell');
            router.navigateTo('#/home');
        };

但内容加载正确。当我通过点击导航到特定页面时,请说/ folder / 2 ,然后将URL更改为/ folders / 2(无效),我在我的日志中找到找不到路由,正如预期的那样,但我遇到了一些其他问题:

But the content loads correctly. When I navigate to a particular page by clicking, say to /folder/2, and then change the url to /folders/2 (invalid), I get "route not found" in my log, as expected, but I run into a few other issues:


  1. 我没有收到错误页面或任何错误(我认为我应该根据我的handleInvalidRoute)

  2. 如果我点击其他内容,网址不会更改,并且未加载新内容,同样没有错误。

I以为我在某种程度上打破了路由,但我不确定如何。我该如何纠正上述问题?

I think I'm breaking routing somehow, but I'm not sure how. How can I correct the above issues?

屏幕:

推荐答案

我怀疑在某些地方召唤导航到某个地方可能太早了原因。要测试此理论,请尝试移动此代码。

I suspect calling navigateTo where you are might be too soon for some reason. To test this theory try move this code.

    if (dataservice.isAuthenticated() === true) {
        app.setRoot('viewmodels/shell', 'entrance');
        router.navigateTo('home');
    } else {
        app.setRoot('viewmodels/public');
        router.navigateTo('#/public');
    }

进入publicviewmodel和main.js的激活方法离开这个:

into an "activate" method on your publicviewmodel, and in the main.js just leave this:

app.setRoot('viewmodels/public');

编辑:旧建议

我相信你的viewmodel你需要一个路由器属性。因此,修改您的公共视图模型以添加一个:

I believe on your viewmodel for the root you need a router property. So modify your public viewmodel to add one:

define(['durandal/app', 'durandal/plugins/router', 'services/dataservice'], function (app, router, dataservice) {
   var publicViewModel = function () {
        self.router = router;
        self.logIn = function () {
            app.setRoot('viewmodels/shell');
            router.navigateTo('#/home');
        };

(你在哪里定义自己?)

(where do you define self though?)

这篇关于DurandalJS路由行为的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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