x4js - v2.2.49
    Preparing search index...

    Class Router

    micro router

    const router = new Router( );

    router.get( "/detail/:id", ( params: any ) => {
    this._showDetail( detail );
    } );

    router.get( "/:id", ( params: any ) => {
    if( params.id==0 )
    router.navigate( '/home' );
    }
    });

    router.on( "error", ( ) => {
    router.navigate( '/home' );
    })

    router.init( );

    Hierarchy (View Summary)

    Index
    • Registers an event listener for a specific event name.

      Type Parameters

      Parameters

      • name: K

        The name of the event to listen for (must be a key in E).

      • callback: (ev: RouterEvents[K]) => void

        The function to be called when the event is fired.

      • capturing: boolean = false

        If true, the listener will be added to the beginning of the listener list (capturing phase).

      Returns () => void

    • Registers a route handler for the given URI pattern.

      Parameters

      • uri: string | RegExp

        A path string (may include parameter placeholders) or a RegExp used to match request URIs.

      • handler: RouteHandler

        A function conforming to the RouteHandler type that will be executed when a request matches the route.

      Returns void

      void

      The provided uri is parsed (via parseRoute) into a RegExp pattern and an ordered list of parameter keys. The resulting route descriptor ({ keys, pattern, handler }) is appended to the router's internal route list.

    • Navigate to a given URI within the router, update browser history, and optionally notify route handlers.

      Normalizes the supplied URI updates the browser history and emits lifecycle events.

      Behavior summary:

      • If no matching route or no handlers are found, logs a message, fires an "error" event with { code: 404, message: "route not found" } and returns false.
      • If notify is true, invokes the first handler of the matched route with (params, uri).
      • Always fires a "change" event with { value: this._getLocation() } after performing the navigation.

      Parameters

      • uri: string

        Target URI to navigate to. If it does not start with '/', a leading '/' will be added. When m_useHash is true the resulting location will be converted to a '#...' fragment.

      • notify: boolean = true

        Whether to invoke the matched route handler after updating history. Defaults to true.

      • replace: boolean = false

        Whether to replace the current history entry (true) or push a new one (false). Defaults to false.

      Returns boolean

      True if navigation succeeded (route found and history updated); false if no route or handlers were found.

      // Navigate to /dashboard and notify handlers (pushes new history entry)
      navigate('/dashboard');
      // Replace the current history entry without notifying handlers
      navigate('/login', false, true);
    • Removes a previously registered event listener.

      Type Parameters

      Parameters

      • name: K

        The name of the event from which to remove the listener.

      • callback: (ev: RouterEvents[K]) => any

        The specific callback function to remove. It must be the same function instance that was originally registered.

      Returns void