Aurelia框架对约定优于配置有很强的吸引力。
在其文档中 sample app (typescript) ,我们有这样的源代码结构:
.
├── index.html
├── config.js
├── src
├── main.ts
├── app.ts // viewmodel
├── app.html // view
├── welcome.ts // viewmodel
├── welcome.html // view
├── users.ts // viewmodel
├── users.html // view
(...)
出于组织目的,我宁愿将每个 View / View 模型/私有(private)组件/等的源代码组织在它们自己的文件夹中,获得如下文件夹结构:
.
├── index.html
├── config.js
├── src
├── main.ts
├── app
├── app.ts // viewmodel
├── app.html // view
├── welcome
├── welcome.ts // viewmodel
├── welcome.html // view
├── users
├── users.ts // viewmodel
├── user-info.ts // view-specific component (1)
├── user-detail.ts // view-specific component (2)
├── users.html // view
(...)
有没有办法实现自定义解析器或覆盖约定?也就是说,我想在我的 Router 中避免显式路径样板代码配置。有什么建议吗?
最佳答案
有一个解决方法。
我看到您不想一直键入特定路径,例如:welcome/welcome
。因此,您可以将路由数组放在一个变量中,然后根据您的模式替换所有 moduleId。像这样:
let routes = [
{ route: ['', 'welcome'], name: 'welcome', moduleId: 'welcome', nav: true, title: 'Welcome' },
{ route: 'test', name: 'test', moduleId: 'test', nav: true, title: 'Test' },
];
updateRoutes(routes);
config.map(routes);
更新路由函数:
function updateRoutes(routes) {
for (i = 0; i < routes.length; i++) {
let route = routes[i];
if (route.moduleId.indexOf('/') === -1) {
route.moduleId = `${route.moduleId}/${route.moduleId}`;
routes[i] = route;
}
}
}
所以,welcome
变成了 welcome/welcome
。请注意,如果其值中没有“/”,该函数将仅替换 moduleId;
如@JeremyDanyow 的评论所述,使用这种方式不会“伤害”框架。
关于typescript - 如何覆盖 Aurelia ViewModel 分辨率?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34726285/