Aurelia 奥雷莉亚:可以';找不到路由的名称

Aurelia 奥雷莉亚:可以';找不到路由的名称,aurelia,Aurelia,我正在尝试导航到以下路线: 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(); } ad

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

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
        }
    ]);
}
我收到此错误:
找不到名为“home”的路由。检查是否在路由的配置中指定了名称:“home”。


是因为回家的路线在一个阵列中吗?我尝试将“”和“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");
    }
}

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

试试这个:
this.router.navigateToRoute('home')
我在
navigateToRoute
哪一行抛出错误时收到相同的错误?第一个还是第二个?显示add.js的完整代码。也许你没有正确地注入路由器,我会更新我的问题,同样在你第一次建议后,我改为以下代码:
//let url=this.router.generate(“home”);本.路由器.导航路由(“主”)navigateToRoute()
linetry上获得错误:
this.router.navigateToRoute('home')
我在
navigateToRoute
哪一行抛出错误时收到相同的错误?第一个还是第二个?显示add.js的完整代码。也许你没有正确地注入路由器,我会更新我的问题,同样在你第一次建议后,我改为以下代码:
//let url=this.router.generate(“home”);本.路由器.导航路由(“主”)navigateToRoute()
Line上出现了错误。噢,我现在看到问题了。我想知道这两个路由器是如何相互了解的。还有一件事,我在
@inject(Router)
行上遇到一个语法错误,我需要在jspm中添加一些依赖项吗?不,你不需要。尝试从“aurelia框架”导入{inject}我首先将其与“aurelia框架”一起使用。但是现在已经解决了,必须在
config.js
中的babelOptions中添加
“es7.decorators”
,而且,您的解决方案现在可以按预期工作了。谢谢你的解释。哦,我现在明白问题了。我想知道这两个路由器是如何相互了解的。还有一件事,我在
@inject(Router)
行上遇到一个语法错误,我需要在jspm中添加一些依赖项吗?不,你不需要。尝试从“aurelia框架”导入{inject}我首先将其与“aurelia框架”一起使用。但是现在已经解决了,必须在
config.js
中的babelOptions中添加
“es7.decorators”
,而且,您的解决方案现在可以按预期工作了。谢谢你的解释。