typescript - 如何覆盖 Aurelia ViewModel 分辨率?

标签 typescript single-page-application aurelia

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/

相关文章:

javascript - 字符串与字符串枚举不兼容

angular - RxJS:通过合并动态添加和删除 Observable

javascript - 带有/登录页面的单页应用程序

c# - Blazor onclick 事件从循环中传入计数器

javascript - 强制 Aurelia 使用相同组件但不同路由参数调用新路由的生命周期方法

http - Aurelia 授权响应

javascript - ContentSecurityPolicy 防止 Electron 中的获取请求

html - 单击鼠标时将文本与光标一起拖动

reactjs - 如何为React单页应用程序使用PKCE实现OAuth2授权代码授予?

node.js - gulp 命令给出模块错误找不到