如何在没有可用解析的情况下确定.otherwise()上的默认URL

时间:2022-12-15 01:51:32

I'm trying to set the .otherwise() using a rule function to determine the default URL. I can't inject state or read the path since the application hasn't loaded yet, what I really need to be able to do is something similar to resolve within .otherwise() to check my UserService and ping the server for a session, since my UserService.isLoggedIn is undefined. How do you handle having different default URLs where you really need something like resolve?

我正在尝试使用规则函数设置.otherwise()以确定默认URL。我无法注入状态或读取路径,因为应用程序尚未加载,我真正需要做的是类似于.otherwise()中的解析来检查我的UserService并ping服务器进行会话,因为我的UserService.isLoggedIn未定义。你如何处理不同的默认URL,你真的需要像解决一样的东西?

2 个解决方案

#1


2  

Have a look at the example in the official docs: http://angular-ui.github.io/ui-router/site/#/api/ui.router.router.$urlRouterProvider#methods_otherwise

看看官方文档中的示例:http://angular-ui.github.io/ui-router/site/#/api/ui.router.router.$urlRouterProvider#methods_otherwise

  // Example of using function rule as param
  $urlRouterProvider.otherwise(function ($injector, $location) {
    return '/a/valid/url';
  });

You have $injector and $location so you can snag whatever you need. You could even wrap it in an easy-to-read injectable function if you want:

你有$ injector和$ location,所以你可以随心所欲。如果您需要,您甚至可以将其包装在易于阅读的注射功能中:

function myOtherwiseFunction($state, UserService, Whatever) { 
  // check some $state stuff
  if ($state.current) { /* blah */ }
  // check some Whatever stuff
  if (Whatever.myFancyLogic()) { /* blah */ }

  if (UserService.isLoggedIn()) 
    return "/app";
  else 
    return "/login";
}

$urlRouterProvider.otherwise(function ($injector, $location) {
  return $injector.invoke(myOtherwiseFunction);
});

#2


0  

You can call your function isLogged in your otherwise function.

你可以在你的函数中调用你的函数isLogged。

But... the thing is that you can't inject your service UserService in your config function (I suppose a bit) because it is not a constant neither a provider.

但是......问题是你不能在配置函数中注入你的服务UserService(我想有点),因为它既不是一个常量也不是一个提供者。

What you can do is calling you logging process in a constant that returns a promise like the resolve function:

您可以做的是在一个常量中调用您的日志记录进程,该常量返回一个类似resolve函数的promise:

.constant('login', function(){

  var access = function(UserService){
     return $q.when('start').then(function(){
       return UserService.initSession(); // I suppose you have a similar function
     })
  }

  access.$inject = ['UserService'];

  return access;
})

And in your config function:

并在您的配置功能:

.config([..., 'login',
...
 $urlRouterProvider.otherwise(function() {
     login();
  ...

#1


2  

Have a look at the example in the official docs: http://angular-ui.github.io/ui-router/site/#/api/ui.router.router.$urlRouterProvider#methods_otherwise

看看官方文档中的示例:http://angular-ui.github.io/ui-router/site/#/api/ui.router.router.$urlRouterProvider#methods_otherwise

  // Example of using function rule as param
  $urlRouterProvider.otherwise(function ($injector, $location) {
    return '/a/valid/url';
  });

You have $injector and $location so you can snag whatever you need. You could even wrap it in an easy-to-read injectable function if you want:

你有$ injector和$ location,所以你可以随心所欲。如果您需要,您甚至可以将其包装在易于阅读的注射功能中:

function myOtherwiseFunction($state, UserService, Whatever) { 
  // check some $state stuff
  if ($state.current) { /* blah */ }
  // check some Whatever stuff
  if (Whatever.myFancyLogic()) { /* blah */ }

  if (UserService.isLoggedIn()) 
    return "/app";
  else 
    return "/login";
}

$urlRouterProvider.otherwise(function ($injector, $location) {
  return $injector.invoke(myOtherwiseFunction);
});

#2


0  

You can call your function isLogged in your otherwise function.

你可以在你的函数中调用你的函数isLogged。

But... the thing is that you can't inject your service UserService in your config function (I suppose a bit) because it is not a constant neither a provider.

但是......问题是你不能在配置函数中注入你的服务UserService(我想有点),因为它既不是一个常量也不是一个提供者。

What you can do is calling you logging process in a constant that returns a promise like the resolve function:

您可以做的是在一个常量中调用您的日志记录进程,该常量返回一个类似resolve函数的promise:

.constant('login', function(){

  var access = function(UserService){
     return $q.when('start').then(function(){
       return UserService.initSession(); // I suppose you have a similar function
     })
  }

  access.$inject = ['UserService'];

  return access;
})

And in your config function:

并在您的配置功能:

.config([..., 'login',
...
 $urlRouterProvider.otherwise(function() {
     login();
  ...