【问题标题】:Angular 1.5 component router $canActivate: accessing next route and rerouting in case of no dataAngular 1.5 组件路由器 $canActivate:在没有数据的情况下访问下一个路由并重新路由
【发布时间】:2016-06-01 14:15:57
【问题描述】:

我实际上在使用 $canActivate 时遇到了三个问题(也请查看下面的代码以更好地理解这些问题):

  1. 当点击<a ng-link="['MachineDetails', { id: 'c6bd76947fc0' }]</a> 时,我无法从 $canActivate (url 并且路线还没有改变,所以也无法通过 注入 $location 或注入 $rootRouter)
  2. 我无法找到将数据从 $canActivate 传递到 控制器。
  3. 我无法从 $canActivate 重新路由(通过使用 $rootRouter.navigate())。

    import template from './machine-details.html';
    
    export const machineDetailsComponent = {
        template: template,
        controller: machineDetailsController,
        $canActivate: canActivate,
    };
    
    /*@ngInject*/
    function machineDetailsController (machineDetailsService) {
        this.$routerOnActivate = function (nextRoute) {
            // do something;
        };
    }
    
    /*@ngInject*/
    function canActivate ($q, $rootRouter, machineDetailsService) {
        const deferred = $q.defer();
        /**
         * ISSUE 1:
         * if I get here via ng-link, then $rootRouter.lastNavigationAttempt 
         * shows the route I was on when clicking, however if I type the url manually 
         * (or refresh) I get something I can use which is for example '/machineDetails/c6bd76947fc01'
         **/ 
        const machineId = $rootRouter.lastNavigationAttempt.split('/')[2];
    
        machineDetailsService.getMachineDetailsData(machineId)
            .then(successHandler, errorHandler);
    
        /**        
         * ISSUE 2:
         * 'result' does not make its way into controller so I need to call
         * machineDetailsService.getMachineDetailsData again in controller...
         **/ 
        function successHandler (result) {
            deferred.resolve(); // passing on result has no affect
        }
    
        function errorHandler (error) {
            deferred.resolve(error);
            /** 
             * ISSUE 3: 
             * $rootRouter.navigate doesn't work here (and controller is 
             * never called) so how do I cause system to go back to list view ?
            **/ 
            // $rootRouter.navigate(['ListView']);
        }
    
        return deferred.promise;
    }
    

    以上代码是我用 ES6 编写的组件定义。

【问题讨论】:

    标签: angularjs angular-component-router


    【解决方案1】:
    1. 您可以在$canActivate 挂钩中注入$nextInstruction,然后您可以通过$nextInstruction.params 访问您的参数。

      function canActivate($nextInstruction, $q, $rootRouter, machineDetailsService) {
        const machineId = $nextInstruction.params.id;
      }
      
    2. 我遇到了同样的问题,但找不到将数据从 $canActivate 挂钩传递到控制器的任何解决方案。

    3. 您可以通过$rootRouter.navigate(['/ListView']) 导航到ListView。注意正斜杠。这需要指定,因为您的 ListView 不是子路由。

    【讨论】:

    • Re #2 我找到了一种方法,但感觉很hacky:控制器和canActivate都可以访问machineDetailsComponent(组件定义对象),因此可以用作共享范围。 .
    猜你喜欢
    • 1970-01-01
    • 2020-05-12
    • 2018-11-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-12-11
    • 1970-01-01
    相关资源
    最近更新 更多