Aurelia:找不到Route的名字

时间:2016-04-27 18:02:26

标签: aurelia aurelia-router aurelia-navigation

我正在尝试按如下方式导航到路线:

add.js:

import {HttpClient} from "aurelia-http-client";
import $ from "jquery";
import {Router} from "aurelia-router";

const baseURI = "/posts";

export class Add {

    constructor() {
        this.router = new Router();
    }

    add() {
        let url = this.router.generate("home");
        this.router.navigate(url);
    }
}

app.js:

configureRouter(config, router) {
    this.router = router;
    config.map([
        {
            route: ["", "home"],
            moduleId: "./home",
            title: "Home",
            name: "home",
            nav: true
        },
        {
            route: "add",
            moduleId: "./add",
            title: "Add New Post",
            name: "Add",
            nav: true
        }
    ]);
}

我收到此错误:A route with name 'home' could not be found. Check that name: 'home' was specified in the route's config.

是因为家的路线是在阵列中吗?我尝试将“”和“home”分开,但错误仍然存​​在。

1 个答案:

答案 0 :(得分:0)

你必须注入路由器。这样做,框架会为您提供由app.js配置的相同实例

import { inject } from 'aurelia-dependency-injection'; //or aurelia-framework
import {HttpClient} from "aurelia-http-client";
import $ from "jquery";
import {Router} from "aurelia-router";


@inject(Router)
export class Add {

    constructor(router) {
        this.router = router;
    }

    add() {
        //let url = this.router.generate("home");
        this.router.navigateToRoute("home");
    }
}