ui-router中的家庭路线 [英] Home route in ui-router

查看:30
本文介绍了ui-router中的家庭路线的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用 https://github.com/angular-ui/ui-router 图书馆.当我尝试访问索引路由 ('/') 时,我被重定向到 404.代码:

I use https://github.com/angular-ui/ui-router library. When I try to access index route ('/') I'm redirected to 404. The code:

angular.module('cr').config(function($stateProvider, $urlRouterProvider) {
    $stateProvider
        .state('home', {
            url: '/',
            templateUrl: 'views/index.html'
        });

    $urlRouterProvider.otherwise('/404');
});

那个代码有什么问题?虽然当我使用 ui-sref="home" 时它可以工作,但 url 看起来像 '/#/' 但是当用户输入站点名称时,他只使用域名,如 'mysite.com',而不是 'mysite.com/#/'

What's wrong with that code? Although when I use ui-sref="home" it works but the url looks like '/#/' but when a user inputs site name he uses just domain name, like 'mysite.com', not 'mysite.com/#/'

推荐答案

您已经声明了在提供任何未知/其他路由时的行为 - 转到 /404.

You've declared how to behave when any unknown/other route is provided - go to /404.

但我们也必须定义如何表现,当访问一些预期的但不是确切"/未知"的路由时,即.创建别名

But we also have to define how to behave, when some expected, but not "exact" / "not known" route is accessed, ie. create alias

那是可以/应该使用 .when() 的地方:

That's where the .when() could/should be used:

...

// the known route, with missing '/' - let's create alias
$urlRouterProvider.when('', '/');

// the unknown
$urlRouterProvider.otherwise('/404');

这篇关于ui-router中的家庭路线的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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