UI的路由器的$ urlRouterProvider.otherwise与HTML5模式

编程入门 行业动态 更新时间:2024-10-26 23:25:49
本文介绍了UI的路由器的$ urlRouterProvider.otherwise与HTML5模式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

考虑以下略作修改$ C $从UI的路由器的维基℃。

Consider the following slightly modified code from ui-router's wiki.

var myApp = angular.module('myApp',['ui.router']); myApp.config(function($stateProvider, $urlRouterProvider, $locationProvider) { // for any unmatched url $urlRouterProvider.otherwise("/state1"); $locationProvider.html5Mode(true); // now set up the states $stateProvider .state('state1', { url: '/state1', templateUrl: "partials/state1.html" }) .state('state1.list', { url: "/list", templateUrl: "partials/state1.list.html", controller: function($scope) { $scope.items = ["A", "List", "of", "Items"]; } }) .state('state2', { url: "/state2", templateUrl: "partials/state2.list.html", controller: function($scope) { $scope.things = ["a", "set", "of", "things"]; } }) });

执行Python 3的SimpleHttpServer,我得到一个 404错误试图访问时:的http://本地主机:8000 / state1234324324 。

为什么没有 $ urlRouterProvider.otherwise(/ STATE1); 重新定向所有未知的路线 / STATE1 ,其中每本问题,都有一个定义状态与 / STATE1 URL?

Why didn't $urlRouterProvider.otherwise("/state1"); re-direct all unknown routes to /state1, which per this question, has a defined state associated with the /state1 url?

推荐答案

这是因为当你点击服务器包含/ state123413的网址,就会直接返回404响应(无路由在客户端发生)。

This is because when you hit the server with urls like "/state123413", it will directly returns the 404 response (No routing at client side takes place).

您需要做的是有一个包罗万象的路线。例如在EX preSS你可以做

What you need to do is to have a catchall route. e.g in express you can do

app.get('/*', function(req, res){ res.render('defaulttemplate') }

这将迫使客户端上的路由。请参阅UI路由器faq为共同的服务器来设置全部接收路由。

This will force the routing on client side. Please see the ui router faq for common servers to set the catchall route.

更多推荐

UI的路由器的$ urlRouterProvider.otherwise与HTML5模式

本文发布于:2023-10-31 17:20:49,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1546758.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:路由器   模式   UI   urlRouterProvider

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!