You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Vaadin Fusion中路由URL参数的使用方法及TypeError报错解决方案咨询

Fixing "TypeError: Expected 'item' to be a string" in Mobx + Vaadin Router Setup

Let’s start by unpacking your error: the Mobx reaction tied to MainView.update() is throwing because it expects the item value to be a string, but it’s getting something else (probably undefined, a number, or an invalid type). Your route path: 'item/:item' is set up correctly, so the issue likely comes from how you’re accessing or handling this route parameter in your components.

First, Verify Route Parameter Access in item-view

In Vaadin Router, route parameters are available via the router’s location object or component lifecycle hooks. If you’re not properly extracting the item parameter, it might be undefined when your Mobx observer/reaction tries to use it. Here’s a safe way to handle this in your ItemView component:

class ItemView extends LitElement {
  static get properties() {
    return {
      item: { type: String }
    };
  }

  onBeforeEnter(location) {
    // Extract the parameter from the route match
    const item = location.params.item;
    // Validate it's a string before proceeding
    if (typeof item !== 'string') {
      // Redirect to home or show an error for invalid parameters
      return Router.redirect('/');
    }
    this.item = item;
    // Pass the validated string to your Mobx store/state
    this.store.setSelectedItem(item);
  }
}

Next, Fix the MainView.update() Reaction

Since the error points directly to Reaction[MainView.update()], you need to add safeguards in that reaction to ensure item is always a string before processing it. Here’s how to adjust your Mobx reaction in MainView:

import { reaction } from 'mobx';

class MainView extends LitElement {
  connectedCallback() {
    super.connectedCallback();
    // Reaction that watches for item state changes
    this.disposer = reaction(
      () => this.store.selectedItem,
      (item) => {
        // Add type check first to prevent the error
        if (typeof item !== 'string') {
          console.warn('Invalid item value - expected string');
          // Set a default value or handle the invalid state
          this.store.setSelectedItem('');
          return;
        }
        // Your existing update logic goes here
        this.refreshItemDisplay(item);
      }
    );
  }

  disconnectedCallback() {
    super.disconnectedCallback();
    // Clean up the reaction to avoid memory leaks
    this.disposer();
  }
}

Key Fixes to Remember

  • Validate early: Always check the type of your route parameter as soon as you access it, before passing it to Mobx state. This stops invalid values from triggering problematic reactions.
  • Add guards in reactions: Even with upfront validation, a type check in your Mobx reaction acts as a safety net for unexpected edge cases.
  • Ensure navigation passes strings: When navigating to /item/:item, make sure the value you’re passing is a string (e.g., convert numbers first with Router.go(/item/${String(itemId)})).

Do You Need to Handle This in MainView?

Yes, absolutely. Since MainView is observing the item state that originates from the route parameter, adding type safeguards there is critical. The Vaadin Todo tutorial likely doesn’t cover edge cases like invalid route parameters, so extending the logic with validation is necessary here.

内容的提问来源于stack exchange,提问作者Ben

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.04.29 17:32:56