大佬教程收集整理的这篇文章主要介绍了angular-ui-router使用,大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
npm install --save angular-ui-router
备注: 以下所有示例代码来源于个人所写的练习.
地址为:@L_262_3@
import uiRouter from 'angular-ui-router';
angular.module('app',[uiRouter]);
function runBlock($rootScope,$state,$stateParams) { 'ngInject'; $rootScope.$state = $state; $rootScope.$stateParams = $stateParams; }
export function routerConfig($urlRouterProvider) { 'ngInject'; // 默认路由设置 $urlRouterProvider.otherwise('/home'); // 无视浏览器中 url末尾的"/" // 设置时 url,末尾不要添加 "/" $urlRouterProvider.rule(($injector,$LOCATIOn) => { const path = $LOCATIOn.path(); const hashTrailingSlash = path[path.length -1] === '/'; if (hashTrailingSlash) { return path.slice(0,path.length -1); } }); }
angular .module('app',[uiRouter]) .config(routerConfig) .run(runBlock);
export const HomeRouter = { state: 'home',config: { url: '/home',views: { '@': { template: homeTpl,controller: 'HomeController',controllerAs: 'vm' } },title: '好玩的app' } };
import {HomeRouter} from './Routers'; function config($stateProvider) { 'ngInject'; $stateProvider.state(HomeRouter.state,HomeRouter.config); } export default angular .module('app.home',[]) .config(config) .name;
<ul class="nav navbar-nav"> <li ui-sref-active="active"><a ui-sref="home">首页</a></li> <li ui-sref-active="active"><a ui-sref="album">相册</a></li> <li ui-sref-active="active"><a ui-sref="user.baseInfo">个人中心</a></li> </ul> <div ui-view=""></div>
ui-view使用有三种,分别为:
as element:
<ui-view></ui-view>
as attribute:
<ANY ui-view ></ANY>
as class:
<ANY class="ui-view"></ANY>
具体里面的参数不做介绍,自己查阅官方文档
上面的HTML代码会被compile为:
<ul class="nav navbar-nav"> <li ui-sref-active="active" ui-sref-active-eq="" class="active"><a ui-sref="home" href="#/home">首页</a></li> <li ui-sref-active="active" class=""><a ui-sref="album" href="#/album">相册</a></li> <li ui-sref-active="active" class=""><a ui-sref="user.baseInfo" href="#/user/baseInfo">个人中心</a></li> </ul>
<a href="javascript:;" ng-click="vm.getConfig();">获取state配置</a>
let config = this.state.get('user'); console.log(config); // => Object {url: "/user",views: Object,title: "个人中心",name: "user"}...
<a href="javascript:;" ng-click="vm.goUserCenter();">这是一个跳转链接...</a>
// 不带参数跳转 this.state.go('user.baseInfo'); // 带参数跳转 this.state.go('album.detail',{id: 1});
console.log(this.state.href('album.detail',{id: 0})); // => #/album/0
console.log(this.state.includes('album')); // => false
console.log(this.state.is('home')); //=> true
function stateChangeStart($rootScopE) { $rootScope.$on('$stateChangeStart',(event,toState,toParams,fromState,fromParams) => { // event.preventDefault(); console.log('开始改变====='); console.log(toStatE); console.log(toParams); console.log(fromStatE); console.log(fromParams); // 开始改变===== // app.js:48Object {url: "/home",title: "好玩的app",name: "home"} // app.js:49Object {} // app.js:50Object {name: "",url: "^",views: null,abstract: truE} // app.js:51Object {} // client.js:55 [HMR] connected }); }
// somewhere,assume lazy.state has not been defined $state.go("lazy.state",{a:1,b:2},{inherit:falsE}); // somewhere else $scope.$on('$stateNotFound',function(event,unfoundState,fromParams) { console.log(unfoundState.to); // "lazy.state" console.log(unfoundState.toParams); // {a:1,b:2} console.log(unfoundState.options); // {inherit:falsE} + default options })
onEnter: function() { console.log('enter user.footprint state'); }
onExit: function() { // 用于初始化一些数据什么的,清空表单... console.log('exit user.footprint state'); }
在html中给ui-view添加名字
<div ui-view="content"></div> <div ng-show="vm.isShowThumb" class="module-content" ui-view="thumbList"></div> <div ng-show="vm.isShowDetail" ui-view="detailList"></div>
在路由配置中添加配置信息
export const UserFootprintRouter = { state: 'user.footprint',config: { url: '/footprint',views: { 'content@user': { template: footPrintTpl,controller: 'FootprintController',controllerAs: 'vm' },'thumbList@user.footprint': { template: thumbListTpl },'detailList@user.footprint': { template: detailListTpl } },title: '我的足迹' } };
个人理解就是: viewname@statename去设置template
先看代码:
export const AlbumDetailRouter = { state: 'album.detail',config: { url: '/:id',views: { '@': { template: albumDetailTpl,controller: 'PhotoController',title: '单张照片show' } };
问: 我们经常会需要用到路由去传参,例如编辑一条信息,获取单个信息,应该如何去做呢?
答: angular-ui-router提供了一个$stateParams的service,可直接获取.在controller中的使用示例
export default class PhotoController { constructor(photoresource,$stateParams) { let vm = this; photoresource.success(data => { vm.detail = data[$stateParams.id]; }) } }
有人肯定会疑问,$stateParams从何而来,在上面我们给angular.module中已经将其初始化,挂在到$rootScope.
when() for redirection
app.config(function($urlRouterProvider){ $urlRouterProvider.when('','/index'); });
otherwise() for invalid routes
app.config(function($urlRouterProvider){ $urlRouterProvider.otherwise('/home'); });
以上是大佬教程为你收集整理的angular-ui-router使用全部内容,希望文章能够帮你解决angular-ui-router使用所遇到的程序开发问题。
如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。