gpt4 book ai didi

angularjs - 没有 urlRouterProvider.otherwise 的 ui 路由

转载 作者:数据小太阳 更新时间:2023-10-29 03:27:58 25 4
gpt4 key购买 nike

如果我不包含 $urlRouterProvider.otherwise('/'); 就不会发生路由在配置中。

我的 gorilla go 应用程序引擎设置是

r := mux.NewRouter()
r.HandleFunc(/admin, handleAdmin)

angularjs 配置是;

angular.module('admin')
.config(function($stateProvider, $urlRouterProvider) {
$stateProvider.state('home', {
url: '/',
templateUrl: '/admin/index.html',
controller: 'AdminController as admin'
});
//$urlRouterProvider.otherwise('/');
})

当我不调用 $urlRouterProvider.othwerwise 行时,我打开 http://localhost:8080/admin我希望看到 admin/index.html,但我没有。我只有在导航到 http://localhost:8080/admin#/ 时才能看到它手动。但是如果我添加 $urlRouterProvider.othwerwise 选项并转到 http://localhost:8080/admin它会自动重定向到 http://localhost:8080/admin#/我认为这不是通常的做法,因为我可能希望“否则”路由到自定义 404 页面。我错过了什么?

最佳答案

默认情况下,Angular 会在 url 前面添加 hashPrefix。因此,当您导航到 http://localhost:8080/admin 时,您看不到 index.html,因为您尚未访问 angular 的 ui-router 中定义的 url。您必须导航到 http://localhost:8080/admin/#/ 才能真正处于应用程序的 / 状态。

这与您的应用程序在没有 .otherwise() 的情况下无法运行的原因相同,从那以后它会自动将您重定向到 / 状态。

对于可能的修复:

在你的 .config 函数中:

// This is in your app module config.
$locationProvider.html5Mode(true);

在你的 index.html 中:

// This is in your index.html head.
<base href="/" />

关于angularjs - 没有 urlRouterProvider.otherwise 的 ui 路由,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34191096/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com