Aurelia:找不到路线名称

Aurelia: can't find name of Route

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

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" 分开,但错误仍然存​​在。

你必须注入路由器。这样做,框架会为您提供由 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");
    }
}