6

我正在尝试从app.js文件中取出路由器配置并将其放入单独的文件(app.router.js)中。这可能是一件容易的事,但我不知道该怎么做。

当前app.js文件如下所示:

import {Router} from 'aurelia-router';

export class App {

  static inject() { return [Router]; };

  constructor(router) {

    this.router = router;

    // router - put this part in a separate file
    this.router.configure(config => {

      config.title = 'demo';
      config.options.pushState = true;
      config.map([

        // home routes
        { route: ['','home'], moduleId: './home/home', nav: true, title:'Home' },

        // User routes
        { route: ['user/register'], moduleId: './user/register', nav: true, title:'User Registration'}

      ]);

    });

  }
}

一旦配置部分在一个单独的文件中,我相信我已经这样称呼它app.js

this.router.configure(myRouterConfig);

请让我知道如何使用代码示例进行操作。

4

1 回答 1

9

The solution is easier to understand when you realize that the argument you pass to this.router.configure is just a function. To put your router configuration in a separate file, just have that file export a function that takes one argument (config).

// app.router.js
export default function (config) {
  config.title = 'demo';
  config.options.pushState = true;

  config.map([
    // home routes
    { route: ['','home'], moduleId: './home/home', nav: true, title:'Home' },
    // User routes
    { route: ['user/register'], moduleId: './user/register', nav: true, title:'User Registration'}
  ]);      
}

Then, in app.js:

import appRouter from 'app.router';

// ...then...
this.router.configure(appRouter);

You can, of course, name appRouter anything you want.

于 2015-03-25T22:26:58.950 回答