【问题标题】:How to use URL parameters with router in Vaadin Fusion?如何在 Vaadin Fusion 中将 URL 参数与路由器一起使用?
【发布时间】:2021-05-30 15:42:59
【问题描述】:

我尝试按照指南在 Question,结果是一个

mobx.esm.js?4fd9:2362 [mobx] Encountered an uncaught exception that was thrown by a reaction or observer component, in: 'Reaction[MainView.update()]' TypeError: Expected "item" to be a string

我的配置是

path: 'item/:item',
component: 'item-view',

有没有例子,如何解决这个问题?我是否需要在 MainView 中处理这个问题(我按照 vaadin.com 上的 todo-tutorial 进行操作?

【问题讨论】:

    标签: vaadin vaadin-fusion hilla


    【解决方案1】:

    问题在于启动项目中的main-view.ts 具有尝试列出所有导航路线以为其生成链接的逻辑。目前,该逻辑还不够智能,无法检测和跳过具有非可选路由参数的路由。

    path 具有非可选路由参数时,router.urlForPath(viewRoute.path) 会引发错误,因为这里我们没有指定路由参数的值(对于生成的 URL)。要为路径 'item/:item' 生成 URL,需要执行类似 router.urlForPath('item/:item', { item: 'my-item' }) 的操作。

    Marcus 建议的快速修复(从路由定义中删除title)有效,因为main-view.ts 具有跳过所有没有title 的路由的逻辑。如果您真的想为这些路线生成有效的导航链接,您可以更改该逻辑以也跳过一些其他条件,或者您可以尝试在此处包含路线参数的值(针对特定路线)。

    其他替代方法是将路由参数标记为可选(如果您希望路由也可以在没有参数的情况下访问),方法是在其后添加问号,然后可以在不指定参数值的情况下生成链接.

    {
      path: 'item/:item?', // <- optional route parameter
      component: 'item-view',
      title: 'Hello World',
    }
    

    【讨论】:

      【解决方案2】:

      您似乎遇到了启动器的问题。在main-view.ts 中,它会遍历带有标题的路线,并尝试为菜单中的每个路线创建一个链接:

      ${this.getMenuRoutes().map(
        (viewRoute) => html`
          <vaadin-tab>
            <a href="${router.urlForPath(viewRoute.path)}" tabindex="-1">${viewRoute.title}</a>
          </vaadin-tab>
        `
      )}
      

      但是,router.urlForPath() 会在 URL 参数定义而不是实际参数(items/:item 而不是 items/2)时中断。

      对此的快速解决方法是从您的路由配置中删除任何具有参数的路由的 title 属性。

      {
        path: 'item/:item',
        component: 'item-view' 
        // make sure there is no title here
      }
      

      【讨论】:

        猜你喜欢
        • 2020-11-05
        • 2017-01-03
        • 2016-09-14
        • 2021-04-03
        • 2021-09-22
        • 2016-09-15
        • 1970-01-01
        • 2017-09-20
        • 2018-10-10
        相关资源
        最近更新 更多