makeCRUDRoutes
Overview
Generate CRUD routes for a given app and model.
Call this once during application setup and register the two records it returns. Besides building them, it registers the configured checks as a beforeEach guard, so a navigation that changes the app, model, or action reruns them even when it stays inside one of the two records, and it starts watching for a change of authenticated user. When one happens while the application sits on one of these routes, it reruns the same checks against that route and redirects if the new user may not use it. Neither of those reruns the checks for a navigation that only changes the query string or the primary key, since nothing about the checked metadata depends on either.
The two records are actionrouter.detailview at /:app/:model/:action/:pk, for one object, and actionrouter.listview at /:app/:model/:action/, for a model or several objects. The list route reads pk from the query string instead, as one repeated value per primary key (?pk=4&pk=7), which a bulk action targets. Each value is one whole key and is never split, so ?pk=4,7 names the single key 4,7. Both routes pass app, model, action, and pk to the component as props. Either route may carry a returnPath query value, where an action view goes after it succeeds or is cancelled.
Signature
makeCRUDRoutes(params)
Parameters
| Name | Type | Required | Description |
|---|---|---|---|
| params | object | yes | The parameters. |
params Properties
| Name | Type | Required | Default | Description |
|---|---|---|---|---|
| actionRedirect | any | The route to redirect if model/action not found. | ||
| authRedirect | string | The route to redirect to if the user is not authenticated. | ||
| component | any | The component to use for the routes. | ||
| groups | string[] | The groups required to access the views. | ||
| groupsRedirect | any | The route to redirect to if the user is not in the required groups. Required when groups names at least one group. | ||
| pathPrefix | string | The prefix to add to the path. | ||
| pinia | Pinia | The Pinia instance. | ||
| router | Router | The Vue router instance. | ||
| vueApp | App | The Vue app instance. |
Returns
RouteLocationNormalizedGeneric[]
The generated routes.
Examples
import { createRouter, createWebHistory } from 'vue-router';
import ActionView from '@/views/ActionView.vue';
import { makeCRUDRoutes } from '@vueda/router/makeCrud.js';
const router = createRouter({ history: createWebHistory(), routes: [] });
// In your app setup (after createApp):
router.addRoute(...makeCRUDRoutes({
component: ActionView,
vueApp: app,
router,
pinia,
authRedirect: { name: 'login' },
actionRedirect: { name: 'not-found' },
}));Source
client/lib/router/makeCrud.js:177