This is unreleased documentation for Yew Next version.
For up-to-date documentation, see the latest version on docs.rs.

Trait yew_router::scope_ext::RouterScopeExt

source ·
pub trait RouterScopeExt {
    // Required methods
    fn navigator(&self) -> Option<Navigator>;
    fn location(&self) -> Option<Location>;
    fn route<R>(&self) -> Option<R>
       where R: Routable + 'static;
    fn add_location_listener(
        &self,
        cb: Callback<Location>
    ) -> Option<LocationHandle>;
    fn add_navigator_listener(
        &self,
        cb: Callback<Navigator>
    ) -> Option<NavigatorHandle>;
}
Expand description

An extension to Scope that provides location information and navigator access.

You can access them on ctx.link()

§Example

Below is an example of the implementation of the Link component.

impl<R: Routable + 'static> Component for Link<R> {
    type Message = Msg;
    type Properties = LinkProps<R>;

    fn create(_ctx: &Context<Self>) -> Self {
        Self { _data: PhantomData }
    }

    fn update(&mut self, ctx: &Context<Self>, msg: Self::Message) -> bool {
        match msg {
            Msg::OnClick => {
                ctx.link()
                    .navigator()
                    .expect_throw("failed to get navigator.")
                    .push(&ctx.props().to);
                false
            }
        }
    }

    fn view(&self, ctx: &Context<Self>) -> Html {
        html! {
            <a class={ctx.props().classes.clone()}
                href={ctx.props().to.to_path()}
                onclick={ctx.link().callback(|e: MouseEvent| {
                    e.prevent_default();
                    Msg::OnClick
                })}
            >
                { ctx.props().children.clone() }
            </a>
        }
    }
}

Required Methods§

source

fn navigator(&self) -> Option<Navigator>

Returns current Navigator.

source

fn location(&self) -> Option<Location>

Returns current Location.

source

fn route<R>(&self) -> Option<R>
where R: Routable + 'static,

Returns current route.

source

fn add_location_listener( &self, cb: Callback<Location> ) -> Option<LocationHandle>

Adds a listener that gets notified when location changes.

§Note

LocationHandle works like a normal ContextHandle and it unregisters the callback when the handle is dropped. You need to keep the handle for as long as you need the callback.

source

fn add_navigator_listener( &self, cb: Callback<Navigator> ) -> Option<NavigatorHandle>

Adds a listener that gets notified when navigator changes.

§Note

NavigatorHandle works like a normal ContextHandle and it unregisters the callback when the handle is dropped. You need to keep the handle for as long as you need the callback.

Object Safety§

This trait is not object safe.

Implementations on Foreign Types§

source§

impl<COMP: Component> RouterScopeExt for Scope<COMP>

Implementors§