如何使用 lit 访问 vaadin-router 的 location.params?

How to access the location.params of the vaadin-router using lit?

根据Vaadin提供的documentation and demos,路由参数应该绑定到location.params。 提供的示例使用的是聚合物,当我使用 LitElement 时,location.params 未定义。 除了使用 JavaScript 结合 Lit 解析 url 来提取使用过的 url :parameter 之外,还有其他技巧吗?

您可以通过覆盖 onBeforeEnter 生命周期回调来访问它:

@customElement('example-view')
export class ExampleView extends LitElement implements BeforeEnterObserver {

  @state()
  private user = '';

  render() {
    return html`
      <h1>Hello, ${this.user ? this.user : 'stranger'}</h1>
    `;
  }

  async onBeforeEnter(location: RouterLocation) {
    this.user = location.params.user as string;
  }

}