One router for the page, installed in every Vue app on it – each
component's own – as vue-router allows: $router and $route in their
templates, <router-link>, and what component libraries do with them (a
menu or a breadcrumb that navigates).
Usage
vue_router(id, history = c("hash", "memory"), routes = NULL, guard = NULL)Arguments
- id
The router's id: the route arrives as
input$<id>.- history
"hash"(the default) or"memory".- routes
NULL, or a named list ofvue_component()s, by path –list("/" = home, "/orders/:id" = order)– which a<router-view>in anyvue_app()renders for the route, as vue-router's routes do;$route.params.idreaches the path's parts. Any other path is still a route, rendering nothing.- guard
NULL;"server", for every navigation to ask the server first (see "Shiny inputs"); or aJS()function of(to, from), as vue-router'sbeforeEach:truelets it through,falsestays, a path goes there instead.
Details
The route lives in the URL's hash (#/orders), which leaves the query
string to Shiny's bookmarks; history = "memory" keeps the URL as it is.
Any path is a route: what the page shows for it is the server's choice,
with input$<id> – shiny::conditionalPanel(), shiny::renderUI(), a
navset driven by it.
Shiny inputs
input$<id> is the route's full path – "/orders",
"/orders?page=2" – on load and after every navigation that succeeds,
whether by a link, the browser's back and forward, a component or
update_vue_router(); input$<id>_params its params, list(id = "42")
for /orders/:id (NULL when it has none). A bookmark restores the
route.
With guard = "server", input$<id>_guard is each navigation's
question, list(to =, from =, params =, request =), and it waits for the
answer, vue_answer(session, "<id>", input$<id>_guard, value): TRUE
lets it through, FALSE stays where it is, a path goes there instead.
Unanswered within 30 seconds, it goes through. A guard in the browser is a
convenience, not security: the server checks what it serves for a route
as it would without one.
Examples
library(shiny)
home <- vue_component("<h3>Home</h3>")
order <- vue_component("<h3>Order {{ $route.params.id }}</h3>")
ui <- fluidPage(
vue_router(
"route",
routes = list("/" = home, "/orders/:id" = order),
guard = "server"
),
vue_app(
"nav",
htmltools::tags$nav(
htmltools::tag("router-link", list(to = "/", "Home")),
" | ",
htmltools::tag("router-link", list(to = "/orders/42", "Order 42"))
)
),
vue_app("view", htmltools::tag("router-view", list()))
)
server <- function(input, output, session) {
# order 13 is not shown
observeEvent(input$route_guard, {
q <- input$route_guard
vue_answer(session, "route", q, !identical(q$params$id, "13"))
})
}
if (interactive()) {
shinyApp(ui, server)
}