在UI路由器首页路线 [英] Home route in ui-router

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

问题描述

我使用 https://github.com/angular-ui/ui-router库。当我尝试访问索引路径('/'),我重定向到404 code:

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

什么是错的code?虽然当我使用的UI SREF =家它的工作原理,但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.

但是,我们还必须定义如何做人,当一些预期,但没有精确/不知道的路线被访问时,IE浏览器。创建别名

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

这就是在。当()可以/应该使用:

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路由器首页路线的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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