Angular UI路由器:具有相同URL的不同状态?

Angular UI Router: Different states with same URL?(Angular UI路由器:具有相同URL的不同状态?)
本文介绍了Angular UI路由器:具有相同URL的不同状态?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的应用的登陆页面有两种状态:home-publichome-logged-in.现在我想在同一个 URL 上显示两种状态,但让控制器和模板依赖于用户会话(用户是否登录?).

The landing page of my app has two states: home-public, home-logged-in. Now I want to show both states on the same URL, but let the controller and template depend on the user session (is the user logged in or not?).

有没有办法做到这一点?

Is there a way to achieve this?

推荐答案

你可以有一个基本状态来控制要加载的状态,你可以简单地让声明该基本状态的子状态没有 url:

You could have a base state that controls which state to load, and you could simply have the child stated of that base state not have urls:

.state('home', {
  url: "/home",
  templateUrl: "....",
  controller: function($scope,$state,authSvc) {
     if(authSvc.userIsLoggedIn()){
          $state.go('home.loggedin')
     }else{
          $state.go('home.public')
     }
  }
})


.state('home.public', {
  url: "",
  templateUrl: "....",
  controller: function($scope) {
     ...........
  }
})

.state('home.loggedin', {
  url: "",
  templateUrl: "....",
  controller: function($scope) {
     ...........
  }
})

现在在你的基本状态(home)的控制器中,你可以检查用户是否登录,并使用 $state.go() 加载适当的状态.

Now in the controller of your base state (home) you can check if the user is logged in or not, and use $state.go() to load an appropriate state.

编辑

正如所承诺的,一个有效的小程序.

这篇关于Angular UI路由器:具有相同URL的不同状态?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

Swipable Vuetify Tabs with router(带路由器的可滑动Vutify标签)
Vue router - how to have multiple components loaded on the same route path based on user role?(VUE路由器-如何根据用户角色将多个组件加载到同一路由路径上?)
Router Link in VUE JS is not showing active(VUE JS中的路由器链路未显示为活动状态)
Can i import single file component using Vue and Vue Router CDN?(我可以使用VUE和VUE路由器CDN导入单个文件组件吗?)
Vue 3 - Get access to router-view instance in order to call child methods(VUE 3-访问路由器视图实例以调用子方法)
How to replace one parameter in Vuejs router(如何替换Vuejs路由器中的一个参数)