gpt4 book ai didi

javascript - Angular ui-router向url添加哈希

转载 作者:塔克拉玛干 更新时间:2023-11-02 20:45:24 24 4
gpt4 key购买 nike

我正在为一个带有 angular 和 angular-ui-router 的应用设置脚手架。我让它工作,但它似乎在我的 url 中添加了一个散列(我在本地主机上运行开发)localhost:9000/#/test。当我登陆主页时,它只是 localhost:9000 并且它仍然提供主视图内容。如果可能的话,我想摆脱哈希。

这是我的设置:

在我的 index.html 中,我只有我的导航,然后是它下面的 ui-view:

 <div class="row">
<ul class="nav navbar-nav">
<li><a ui-sref="index">Home</a></li>
<li><a ui-sref="test">Test</a></li>
</ul>
</div>

<div ui-view=""></div>

在我的 app.js 中我只有:

angular
.module('playApp', [
'ui.router'
])
.config(function($stateProvider) {
$stateProvider
.state('index', {
url: '',
templateUrl: 'views/main.html',
controller: 'MainCtrl'
})
.state('test', {
url: '/test',
templateUrl: 'views/test.html',
controller: 'testCtrl'
});
});

所以当我着陆时,它很好,但是当我开始使用我设置的导航时,它会将哈希添加到 url,如果可能的话我宁愿不要它们。谢谢!

最佳答案

包含 $locationProvider 并执行 $locationProvider.html5Mode(true); :

angular.module('playApp', ['ui.router'])
.config(function($stateProvider, $locationProvider) {
$stateProvider.state('index', {
url: '',
templateUrl: 'views/main.html',
controller: 'MainCtrl'
})
.state('test', {
url: '/test',
templateUrl: 'views/test.html',
controller: 'testCtrl'
});

$locationProvider.html5Mode(true);
});

我也有一个 otherwise 在那里,所以如果它找不到指定的路线,它会默认返回:

angular.module('playApp', ['ui.router'])
.config(function($stateProvider, $locationProvider, $urlRouterProvider) {
$stateProvider.state('index', {
url: '',
templateUrl: 'views/main.html',
controller: 'MainCtrl'
})
.state('test', {
url: '/test',
templateUrl: 'views/test.html',
controller: 'testCtrl'
});

$locationProvider.html5Mode(true);
$urlRouterProvider.otherwise('/');
});

关于javascript - Angular ui-router向url添加哈希,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28768460/

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