如何从ui-router中的url地址中删除哈希标记(#)

时间:2021-09-02 10:41:04

My url looks like this http://127.0.0.1:50884/index.html#/home

我的网址看起来像这样http://127.0.0.1:50884/index.html#/home

This is index page. I am using angular ui-router to change to various views like but url comes with # tag like this: http://127.0.0.1:50884/index.html#/mobileOperator http://127.0.0.1:50884/index.html#/contentExpertise

这是索引页面。我使用角度ui-router来改变各种视图,但url带有#tag这样的:http://127.0.0.1:50884 / index.html#/ mobileOperator http://127.0.0.1:50884/index。 HTML#/ contentExpertise

Question : Is there a way to remove/clean # tag from url.. to show the url path like below:

问题:有没有办法从url中删除/清除#tag ..以显示如下所示的url路径:

http://127.0.0.1:50884/index.html/home

http://127.0.0.1:50884/index.html/contentExpertise

I tried html5mode but it give angular error in console.

我尝试了html5mode,但它在控制台中给出了角度错误。

Error: [$location:nobase] http://errors.angularjs.org/1.5.0-rc.2/$location/nobase
K/<@http://127.0.0.1:50884/js/angular-v1.5.min.js:6:421
pf/this.$get<@http://127.0.0.1:50884/js/angular-v1.5.min.js:110:96
h/<.invoke@http://127.0.0.1:50884/js/angular-v1.5.min.js:41:293
gb/F<@http://127.0.0.1:50884/js/angular-v1.5.min.js:43:96
d@http://127.0.0.1:50884/js/angular-v1.5.min.js:40:270
e@http://127.0.0.1:50884/js/angular-v1.5.min.js:41:1
h/<.invoke@http://127.0.0.1:50884/js/angular-v1.5.min.js:41:86
gb/F<@http://127.0.0.1:50884/js/angular-v1.5.min.js:43:96
d@http://127.0.0.1:50884/js/angular-v1.5.min.js:40:270
e@http://127.0.0.1:50884/js/angular-v1.5.min.js:41:1
h/<.invoke@http://127.0.0.1:50884/js/angular-v1.5.min.js:41:86
gb/F<@http://127.0.0.1:50884/js/angular-v1.5.min.js:43:96
d@http://127.0.0.1:50884/js/angular-v1.5.min.js:40:270
e@http://127.0.0.1:50884/js/angular-v1.5.min.js:41:1
h/<.invoke@http://127.0.0.1:50884/js/angular-v1.5.min.js:41:86
s/</<@http://127.0.0.1:50884/js/angular-v1.5.min.js:52:121
n@http://127.0.0.1:50884/js/angular-v1.5.min.js:7:364
s/<@http://127.0.0.1:50884/js/angular-v1.5.min.js:52:90
h/<.invoke@http://127.0.0.1:50884/js/angular-v1.5.min.js:41:293
e/<@http://127.0.0.1:50884/js/angular-v1.5.min.js:38:458
h/<.invoke@http://127.0.0.1:50884/js/angular-v1.5.min.js:41:293
gb/F<@http://127.0.0.1:50884/js/angular-v1.5.min.js:43:96
d@http://127.0.0.1:50884/js/angular-v1.5.min.js:40:270
K@http://127.0.0.1:50884/js/angular-v1.5.min.js:71:447
la@http://127.0.0.1:50884/js/angular-v1.5.min.js:60:266
R@http://127.0.0.1:50884/js/angular-v1.5.min.js:58:229
R@http://127.0.0.1:50884/js/angular-v1.5.min.js:58:397
R@http://127.0.0.1:50884/js/angular-v1.5.min.js:58:397
N@http://127.0.0.1:50884/js/angular-v1.5.min.js:56:263
Ac/c/</<@http://127.0.0.1:50884/js/angular-v1.5.min.js:21:99
sf/this.$get</m.prototype.$eval@http://127.0.0.1:50884/js/angular-v1.5.min.js:140:363
sf/this.$get</m.prototype.$apply@http://127.0.0.1:50884/js/angular-v1.5.min.js:141:83
Ac/c/<@http://127.0.0.1:50884/js/angular-v1.5.min.js:21:57
h/<.invoke@http://127.0.0.1:50884/js/angular-v1.5.min.js:41:293
Ac/c@http://127.0.0.1:50884/js/angular-v1.5.min.js:20:1
Ac@http://127.0.0.1:50884/js/angular-v1.5.min.js:21:274
de@http://127.0.0.1:50884/js/angular-v1.5.min.js:20:83
@http://127.0.0.1:50884/js/angular-v1.5.min.js:306:372
n.Callbacks/i@http://127.0.0.1:50884/js/jquery-2.2.1.min.js:2:27060
n.Callbacks/j.fireWith@http://127.0.0.1:50884/js/jquery-2.2.1.min.js:2:27828
.ready@http://127.0.0.1:50884/js/jquery-2.2.1.min.js:2:29619
J@http://127.0.0.1:50884/js/jquery-2.2.1.min.js:2:29804
"

Please help

2 个解决方案

#1


11  

By default, AngularJS will route URLs with a hashtag. It is very easy to get clean URLs and remove the hashtag from the URL using $locationProvider in AngularJS. You need to inject $locationProvider into your config section of module and use the HTML5 History API like this. e.g.

默认情况下,AngularJS将使用主题标签路由URL。使用AngularJS中的$ locationProvider很容易获得干净的URL并从URL中删除主题标签。您需要将$ locationProvider注入模块的config部分,并使用HTML5 History API。例如

app.config(function ($stateProvider, $urlRouterProvider, $locationProvider) {
    $stateProvider
        .state('home', {
            url: '/home',
            templateUrl: 'partial-home.html'
        })
        .state('about', {
            url: '/aboutus',
            templateUrl: 'aboutus.html'   
        });
    $locationProvider.html5Mode(true);
});

Also don't forget to put a <base href="/"> tag inside index.html section.

另外,不要忘记在index.html部分中放置一个标记。

#2


2  

In order for html5 mode to work you must define a baseurl like:

为了使html5模式起作用,您必须定义一个像下面这样的基础:

<base href="whatever/" />

See w3schools for more information: http://www.w3schools.com/tags/tag_base.asp

有关更多信息,请访问w3schools:http://www.w3schools.com/tags/tag_base.asp

#1


11  

By default, AngularJS will route URLs with a hashtag. It is very easy to get clean URLs and remove the hashtag from the URL using $locationProvider in AngularJS. You need to inject $locationProvider into your config section of module and use the HTML5 History API like this. e.g.

默认情况下,AngularJS将使用主题标签路由URL。使用AngularJS中的$ locationProvider很容易获得干净的URL并从URL中删除主题标签。您需要将$ locationProvider注入模块的config部分,并使用HTML5 History API。例如

app.config(function ($stateProvider, $urlRouterProvider, $locationProvider) {
    $stateProvider
        .state('home', {
            url: '/home',
            templateUrl: 'partial-home.html'
        })
        .state('about', {
            url: '/aboutus',
            templateUrl: 'aboutus.html'   
        });
    $locationProvider.html5Mode(true);
});

Also don't forget to put a <base href="/"> tag inside index.html section.

另外,不要忘记在index.html部分中放置一个标记。

#2


2  

In order for html5 mode to work you must define a baseurl like:

为了使html5模式起作用,您必须定义一个像下面这样的基础:

<base href="whatever/" />

See w3schools for more information: http://www.w3schools.com/tags/tag_base.asp

有关更多信息,请访问w3schools:http://www.w3schools.com/tags/tag_base.asp