当我使用$ locationProvider.html5Mode(true);时,$ window.location.href会刷新整个页面。

时间:2018-07-09 13:34:03

标签: angularjs

我在应用程序中使用了html5mode。 $locationProvider.html5Mode(true);

,当我在应用程序中使用$window.location.href时,它将重新加载整个页面。

如果我删除$window而使用$state.go,则该页面将不会完全重新加载。

有人知道为什么会这样吗?有什么解决方法可以避免在使用$window时重新加载整个页面?

2 个答案:

答案 0 :(得分:0)

您可以在下面使用此代码:

// enable html5Mode for pushstate ('#'-less URLs)
    $locationProvider.html5Mode(true);
    $locationProvider.hashPrefix('!');

并添加:

<base href="/" />

发送到您的<head />

答案 1 :(得分:0)

重装确实是due to $window.location.href use

您可以使用$location.path方法来更改URL,而无需重新加载整个页面。

$location.path('/newUrlValue');

您还可以在标头中为相关链接添加基本标签:

<html>
    <head>
        <base href="/">
    </head>
</html>

对于无法处理HTML5历史记录API的浏览器,将自动使用hashbang前缀('!' by default),并可以使用hashPrefix提供程序的方法进行更改。

相关问题