Skip to content

$locationProvider html5mode #20

@Narutocc

Description

@Narutocc

angular路由配置中可以使用$locationProvider服务。

H5之前,一个URL对应一个页面,无论以何种方式修改地址栏地址,都会导致页面完全刷新,无论跳转页面之间是否有关联。简单来说,H5的history API提供接口,可以使用JavaScript修改地址栏路径,而不刷新页面。

!$("base").size() && $("head").prepend('<base href="' + location.href.match("^[^/]+//(?:[^/?#]+/){1,2}")[0] + '">');
angular.module('app',['ui.router','ui.bootstrap']).config(function($stateProvider,$urlRouterProvider,$locationProvider){
	//重定向
	$urlRouterProvider.when('/main','/index').otherwise('/login');
	//路由配置
	$stateProvider.state('login',{
		url:'/login',
		templateUrl:'html/login.html',
		controller:function($state,$scope,$stateParams){

		}
	});
	//跳转设置
	$locationProvider.html5Mode(true);
})

使用locationProvider的时候在页面需要加入<base href="/"/>

  1. 当设置$locationProvider.html5Mode(true);
    angular默认会将其定位到服务器根目录
    地址栏会变为http://hostname/test,而不是http://hostname/#/test
  2. 当设置$locationProvider.html5Mode(false);
    angular默认会url处理

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions