【问题标题】:AngularJS UI-router stateprovider issueAngularJS UI-路由器状态提供者问题
【发布时间】:2015-02-26 15:06:27
【问题描述】:

我目前正在使用 Ionic,并且正在使用带有标签的应用程序。 (即有一个带图标的底栏)。

现在我想删除它,但最终弄乱了路由。 devtools 没有显示任何错误。我无法从登录页面转移到主“帖子”页面。登录时,单击登录按钮可以执行任何操作。我已经相应地编辑了控制器中的 state.go('')。

会在更改前后显示。 完全删除视图是否有意义:{}?我发现它使事情复杂化,我不使用嵌套视图。不确定模态页面是否需要视图。 感谢有关如何解决错误的一些帮助/建议。

之前

    .state('tab', {
      url: '/tab',
      abstract: true,
      templateUrl: 'templates/tabs.html'
    })
   // --------Authenticate states ----------------
   .state('auth', {
      url: '/auth',
      templateUrl: 'templates/auth.html',
      controller: 'StartCtrl'
    })
    .state('register', {
      url: '/register',
      templateUrl: 'templates/register.html',
      controller: 'AuthCtrl',
    })
    .state('login', {
      url: '/login',
      templateUrl: 'templates/login.html',
      controller: 'AuthCtrl',
    })

    // ---------- Main states ---------------------

    .state('tab.posts', {
      url: '/posts',
      views: {
        'tab-posts': {
          templateUrl: 'templates/tab-posts.html',
          controller: 'PostsCtrl'
        }
      }
    })
    .state('tab.newpost', {
      url: '/newpost',
      views: {
        'tab-posts': {
          templateUrl: 'templates/tab-newpost.html',
          controller: 'NavCtrl'
        }
      }
    })
    .state('tab.posts.view', {
      url: '/posts/:postId',           
      views: {
        'tab-posts@tab': {
          templateUrl: 'templates/tab-showpost.html',
          controller: 'PostViewCtrl'
        }
      }
    })
    .state('tab.profile', {
      url: '/users/:userId',  
      views: {
        'tab-posts': {
          templateUrl: 'templates/tab-profile.html',
          controller: 'ProfileCtrl',
        }
      }
    })

之后

.state('/', {
  url: '/',
  abstract: true,
  templateUrl: 'templates/tab-posts.html'
})
// --------Authenticate states ----------------
.state('auth', {
  url: '/auth',
  templateUrl: 'templates/auth.html',
  controller: 'StartCtrl'
})

.state('register', {
  url: '/register',
  templateUrl: 'templates/register.html',
  controller: 'AuthCtrl',
  resolve: {
    user: function(Auth){
      return Auth.resolveUser();
    }
  }
})
.state('login', {
  url: '/login',
  templateUrl: 'templates/login.html',
  controller: 'AuthCtrl',
  resolve: {
    user: function(Auth){
      return Auth.resolveUser();
    }
  }
})

//----------- Main states--------------------

.state('posts', {
  url: '/posts',
  views: {
    'posts': {
      templateUrl: 'templates/tab-posts.html',
      controller: 'PostsCtrl'
    }
  }
})
.state('newpost', {
  url: '/newpost',
  views: {
    'posts': {  
      templateUrl: 'templates/tab-newpost.html',
      controller: 'NavCtrl'
    }
  }
})
.state('posts.view', {
  url: '/posts/:postId',
  views: {
    'posts@': {
      templateUrl: 'templates/tab-showpost.html',
      controller: 'PostViewCtrl'
    }
  }
})
.state('profile', {
  url: '/users/:userId',   
  views: {
    'posts': {
      templateUrl: 'templates/tab-profile.html',
      controller: 'ProfileCtrl',
    }
  }
});

为李维斯更新

    app.factory('Auth', function($firebase, $firebaseAuth, FIREBASE_URL, $rootScope) {

      var ref = new Firebase(FIREBASE_URL);
      var auth = $firebaseAuth(ref);            

      var Auth = {
        register: function (user) {
          return auth.$createUser(user.email, user.password);
        },
        login: function (user) {
          return auth.$authWithPassword(user);  
        },
        logout: function() {                     
          auth.$unauth();
        },
        resolveUser: function() {
          return auth.$waitForAuth();          
        },
        signedIn: function() {
          return !!Auth.user.provider;
        },
        createProfile: function (user) {
          var profile = {
            username: user.username,     
            md5_hash: user.md5_hash
          };
          var profileRef = $firebase(ref.child('profile'));
          return profileRef.$set(user.uid, profile);
        },
        user: {}
      };

  auth.$onAuth(function (user){
    if(user) {
      angular.copy(user, Auth.user);
      Auth.user.profile = $firebase(ref.child('profile').child(Auth.user.uid)).$asObject();
      console.log(Auth.user);
    } else {
      console.log('logged out');

      if (Auth.user && Auth.user.profile) {
        Auth.user.profile.$destroy();
      }
      angular.copy({}, Auth.user);
    }
  });

  return Auth;
});


错误跟踪图像

【问题讨论】:

  • 为什么在进入登录状态之前解析用户?
  • 我的意思是,如果有人登录,如果因为没有登录,Auth.resolveUser(); 将解析为无。你能发布 Auth.resolveUser 函数吗?
  • 嗨,Levi,我更新了代码。当我删除解决方案时,屏幕变为空白。 resolveUser 位于 Auth 工厂中。即使在更改之前,我的注销也无法正常工作,用户配置文件被破坏但屏幕没有更改为 Auth 屏幕(即仍然停留在帖子页面)
  • 如果你将一个解析放入一个状态,如果该解析的项目没有返回,你的应用程序不会显示该状态。因此,您强制用户登录,以便进入登录页面,这是不正确的。如果用户想去登录页面是因为他没有登录。
  • @levi 我应该如何更改它?从状态和工厂中删除它会引发错误。 Error: [$injector:unpr] Unknown provider: userProvider <- user

标签: angularjs angular-ui-router ionic-framework


【解决方案1】:

从您的登录状态中删除您的解析器。

.state('login', {
  url: '/login',
  templateUrl: 'templates/login.html',
  controller: 'AuthCtrl',
})

如果有人想登录,你不能强迫他登录。

【讨论】:

    猜你喜欢
    • 2015-08-02
    • 2016-04-16
    • 2023-03-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多