【问题标题】:`ui-route` - not able to redirect and not geting the `url` of current state`ui-route` - 无法重定向并且无法获取当前状态的`url`
【发布时间】:2017-04-01 00:33:15
【问题描述】:

在我的应用程序中,我在route-config 之后运行一个函数。在功能中,我正在检查登录状态,并相应地尝试重定向到登录页面。但它不起作用,

这是我调用的函数:

angular.module('meanOffice')
        .config(routeConfig)
        .run(stateController);


function stateController( $rootScope, auth, authToken, $state ){

         $rootScope.$on("$stateChangeStart", function(){

            var isLoggedIn = auth.isLoggedIn();

            console.log( $state.current.url )// consoles as ^

            if(!isLoggedIn) {
                $state.go('/'); // Could not resolve '/' from state ''
            }

         })

    }

如何解决这个问题?这里有什么问题?谁来帮帮我。

【问题讨论】:

  • 任何错误????
  • 见评论,// Could not resolve '/' from state ''
  • 检查您的状态声明和其他调用,如附加的 js...当路由器找不到给定状态时,就会出现这种情况
  • 我试过这样$state.go('login', {}); - 不存在错误。但是我的页面没有重定向到登录状态,登录状态已经存在
  • 试试 $location.path('/login');或 $state.go('/login');请更新您的州声明

标签: angularjs angular-ui-router


【解决方案1】:

event.preventDefault()

这将取消当前事件,例如状态更改.....假设您在未登录的情况下进入家庭状态,然后 event.preventDefault();函数取消它并重定向到 state.go 中的给定状态

here 你可以找到这个功能的明确演示

【讨论】:

    【解决方案2】:

    "// 无法从状态 '' 中解析 '/'" --此错误是因为 $stateProvider 中不存在带有“/”的状态。请包括一个。附上示例代码供您参考

    $stateProvider.state("/", {
             url: "/tpl.htmlpage.html",
             templateUrl: "htmlpage.html"
     });
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-12-08
      • 2018-04-07
      • 2019-12-15
      相关资源
      最近更新 更多