2016-07-12 4 views
1

私はAureliaのサイトで、記事の1つにrun() {}を使用しています。この方法は一般に何をしていますか?それはライフサイクルのフックですか、それとも新しいJavascript 2016メソッドですか?javascript/Aureliaで `run(){}`は何をしますか?

http://aurelia.io/hub.html#/doc/article/aurelia/framework/latest/cheat-sheet/7

import {Redirect} from 'aurelia-router'; 
 

 
export class App { 
 
    configureRouter(config) { 
 
    config.title = 'Aurelia'; 
 
    config.addPipelineStep('authorize', AuthorizeStep); 
 
    config.map([ 
 
     { route: ['welcome'], name: 'welcome',  moduleId: 'welcome',  nav: true, title:'Welcome' }, 
 
     { route: 'flickr',  name: 'flickr',  moduleId: 'flickr',  nav: true, auth: true }, 
 
     { route: 'child-router', name: 'childRouter', moduleId: 'child-router', nav: true, title:'Child Router' }, 
 
     { route: '', redirect: 'welcome' } 
 
    ]); 
 
    } 
 
} 
 

 
class AuthorizeStep { 
 
    run(navigationInstruction, next) { 
 
    if (navigationInstruction.getAllInstructions().some(i => i.config.auth)) { 
 
     var isLoggedIn = /* insert magic here */false; 
 
     if (!isLoggedIn) { 
 
     return next.cancel(new Redirect('login')); 
 
     } 
 
    } 
 

 
    return next(); 
 
    } 
 
}

+1

[ライフサイクルの方法](http://aurelia.io/hub.html#/doc/api/aurelia/router/latest/class/LoadRouteStep)は、ES2015メソッド構文を使用して書かれています。リンクしたサイトを検索すると、 'run'メソッドを使用してさまざまなサービスが表示されます。 ES2015では新しいことではありません。 –

+0

それがあります!ありがとう! –

答えて

3

あなたは、ルータの設定に複数のパイプライン・ステップを追加することができます。あなたはrun方法が存在する必要があります見ることができるように

interface PipelineStep { 
    /** 
    * Execute the pipeline step. The step should invoke next(), next.complete(), 
    * next.cancel(), or next.reject() to allow the pipeline to continue. 
    * 
    * @param instruction The navigation instruction. 
    * @param next The next step in the pipeline. 
    */ 
    run(instruction: NavigationInstruction, next: Next): void; 
} 

source code

:パイプラインのそれぞれは、PipelineStepインタフェースを実装する必要があります。ある時点で、すべてのステップのrunメソッドが実行されます。

あなたの質問に対する答え:いいえ、それはES2015が紹介するものではなく、むしろ従来のパイプライン手順に従わなければなりません。

関連する問題