加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

angularjs – 当用ui-router显示404错误页面时,如何不更改url

发布时间:2020-12-17 08:05:29 所属栏目:安全 来源:网络整理
导读:我想显示404错误页面,但我也想在位置保存错误的URL。 如果我会做这样的事情: $urlRouterProvider.otherwise('404');$stateProvider .state('404',{ url: '/404',template: error404Template }); 网址将更改为/ 404。如何在不改变实际网址的情况下在错误的
我想显示404错误页面,但我也想在位置保存错误的URL。

如果我会做这样的事情:

$urlRouterProvider.otherwise('404');
$stateProvider
    .state('404',{
        url: '/404',template: error404Template
    });

网址将更改为/ 404。如何在不改变实际网址的情况下在错误的网址上显示错误消息?

有这种情况的解决方案。我们会使用1)一个ui-router本机特性和2)一个配置设置。 here可以观察到 here。

1)ui-router状态定义的本机特征是:

The url is not needed. State could be defined without its representation in location.

2)配置设置为:

> otherwise() for invalid routes,哪个参数不一定是一个带有默认url的字符串,但也可以是一个函数(cite):

path String | Function The url path you want to redirect to or a function rule that returns the url path. The function version is passed two params: $injector and $location

解决方案:这两个组合。我们会有没有url的状态和其他方式:

$stateProvider
  .state('404',{
    // no url defined
    template: '<div>error</div>',})

$urlRouterProvider.otherwise(function($injector,$location){
   var state = $injector.get('$state');
   state.go('404');
   return $location.path();
});

检查所有在这working example

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读