|
2 | 2 | // does each window have its own router? probably, lol
|
3 | 3 |
|
4 | 4 | use crate::cfg::RouterCfg;
|
5 |
| -use dioxus_core::{ScopeId, ScopeState}; |
| 5 | +use dioxus_core::{ScopeId, ScopeState, VirtualDom}; |
6 | 6 | use std::any::Any;
|
7 | 7 | use std::sync::Weak;
|
8 | 8 | use std::{
|
@@ -103,6 +103,13 @@ impl RouterCore {
|
103 | 103 | svc
|
104 | 104 | }
|
105 | 105 |
|
| 106 | + /// Push a new route with no custom title or serialized state. |
| 107 | + /// |
| 108 | + /// This is a convenience method for easily navigating. |
| 109 | + pub fn navigate_to(&self, route: &str) { |
| 110 | + self.push_route(route, None, None); |
| 111 | + } |
| 112 | + |
106 | 113 | /// Push a new route to the history.
|
107 | 114 | ///
|
108 | 115 | /// This will trigger a route change event.
|
@@ -221,6 +228,20 @@ impl RouterCore {
|
221 | 228 | }
|
222 | 229 | }
|
223 | 230 |
|
| 231 | +/// Get the router service from an existing VirtualDom. |
| 232 | +/// |
| 233 | +/// Takes an optional target_scope parameter to specify the scope to use if ScopeId is not the component |
| 234 | +/// that owns the router. |
| 235 | +/// |
| 236 | +/// This might change in the future. |
| 237 | +pub fn get_router_from_vdom( |
| 238 | + dom: &VirtualDom, |
| 239 | + target_scope: Option<ScopeId>, |
| 240 | +) -> Option<Arc<RouterCore>> { |
| 241 | + dom.get_scope(target_scope.unwrap_or(ScopeId(0))) |
| 242 | + .and_then(|scope| scope.consume_context::<Arc<RouterCore>>()) |
| 243 | +} |
| 244 | + |
224 | 245 | fn clean_route(route: String) -> String {
|
225 | 246 | if route.as_str() == "/" {
|
226 | 247 | return route;
|
|
0 commit comments