我有一个带有配置路线的router.js文件:
routes.js
export class App {
configureRouter(config, router) {
config.title = 'Aurelia';
config.map([
{route: ['', `welcome`], name: 'welcome', moduleId: './welcome', nav: true, title:'Welcome'},
{route: 'http', name: 'http', moduleId: './http', nav: true, title:'HTTPTest'},
{route: 'users', name: 'users', moduleId: './users', nav: true, title:'Github Users'},
{route: 'plan', name: 'plan', moduleId:'./dp/plan', nav: true, title:'DPTest'},
{route: 'user_table', name: 'user_table', moduleId:'./dp/user_table', nav: true, title:'Benutzer'}
]);
this.router = router;
}
}
我想从plan
中的函数调用其中一条路线(users.js
)。我是否必须在users.js中导入{Router}?还是注入?
答案 0 :(得分:12)
以下任何一种都可以使用
router.navigateToRoute()
import { Router } from 'aurelia-router';
@inject(Router)
export class UsersViewModel {
constructor(router) {
this.router = router;
}
doStuff() {
this.router.navigateToRoute('plan');
}
}
export class UsersViewModel {
doStuff() {
location.assign('#/users');
}
}