我在 Aurelia 网站上看到,其中一篇文章使用了 run() {}
。这种方法一般有什么作用?它是一个生命周期钩子(Hook)还是一个新的 Javascript 2016 方法?
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();
}
}
最佳答案
您可以将多个管道步骤添加到您的路由器配置中。每个管道都必须实现 PipelineStep
接口(interface):
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 )
如您所见,必须有 run
方法。稍后将执行所有步骤中的 run
方法。
所以你的问题的答案是:不,这不是 ES2015 引入的东西,而是必须遵循的约定管道步骤。
关于javascript - `run() {}` 在 javascript/Aurelia 中有什么作用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38335525/