Skip to content
Source
Sign inSign up
TanStack/router
Open History to see the last commit

main.tsx

241 lines5.2 KB

1import { createApp, defineComponent } from 'vue'
2import {
3 ErrorComponent,
4 Link,
5 Outlet,
6 RouterProvider,
7 createRootRoute,
8 createRoute,
9 createRouter,
10} from '@tanstack/vue-router'
11import { TanStackRouterDevtools } from '@tanstack/vue-router-devtools'
12import { NotFoundError, fetchPost, fetchPosts } from './posts'
13import VueLogo from './components/VueLogo.vue'
14import type { ErrorComponentProps } from '@tanstack/vue-router'
15import './styles.css'
16​
17const rootRoute = createRootRoute({
18 component: RootComponent,
19 notFoundComponent: () => {
20 return (
21 <div>
22 <p>This is the notFoundComponent configured on root route</p>
23 <Link to="/">Start Over</Link>
24 </div>
25 )
26 },
27})
28​
29function RootComponent() {
30 return (
31 <>
32 <div class="p-2 flex gap-2 text-lg border-b">
33 <Link
34 to="/"
35 activeProps={{
36 class: 'font-bold',
37 }}
38 activeOptions={{ exact: true }}
39 >
40 Home
41 </Link>{' '}
42 <Link
43 to="/posts"
44 activeProps={{
45 class: 'font-bold',
46 }}
47 >
48 Posts
49 </Link>{' '}
50 <Link
51 to="/route-a"
52 activeProps={{
53 class: 'font-bold',
54 }}
55 >
56 Pathless Layout
57 </Link>{' '}
58 <Link
59 // @ts-expect-error
60 to="/this-route-does-not-exist"
61 activeProps={{
62 class: 'font-bold',
63 }}
64 >
65 This Route Does Not Exist
66 </Link>
67 </div>
68 <Outlet />
69​
70 <TanStackRouterDevtools router={router} position="bottom-right" />
71 </>
72 )
74const indexRoute = createRoute({
75 getParentRoute: () => rootRoute,
76 path: '/',
77 component: IndexComponent,
78})
79​
80function IndexComponent() {
81 return (
82 <div class="p-2">
83 <h3>Welcome Home!</h3>
84 <VueLogo textColor="orange" />
85 </div>
86 )
88​
89export const postsLayoutRoute = createRoute({
90 getParentRoute: () => rootRoute,
91 path: 'posts',
92 loader: () => fetchPosts(),
93}).lazy(() => import('./posts.lazy').then((d) => d.Route))
94​
95const postsIndexRoute = createRoute({
96 getParentRoute: () => postsLayoutRoute,
97 path: '/',
98 component: PostsIndexComponent,
99})
100​
101function PostsIndexComponent() {
102 return <div>Select a post.</div>
104​
105const PostComponent = defineComponent({
106 setup() {
107 const post = postRoute.useLoaderData()
108​
109 return () => (
110 <div class="space-y-2">
111 <h4 class="text-xl font-bold">{post.value.title}</h4>
112 <hr class="opacity-20" />
113 <div class="text-sm">{post.value.body}</div>
114 </div>
115 )
116 },
117})
118​
119const postRoute = createRoute({
120 getParentRoute: () => postsLayoutRoute,
121 path: '$postId',
122 errorComponent: PostErrorComponent,
123 loader: ({ params }) => fetchPost(params.postId),
124 component: PostComponent,
125})
126​
127function PostErrorComponent({ error }: ErrorComponentProps) {
128 if (error instanceof NotFoundError) {
129 return <div>{error.message}</div>
130 }
131​
132 return <ErrorComponent error={error} />
134​
135const pathlessLayoutRoute = createRoute({
136 getParentRoute: () => rootRoute,
137 id: '_pathlessLayout',
138 component: PathlessLayoutComponent,
139})
140​
141function PathlessLayoutComponent() {
142 return (
143 <div class="p-2">
144 <div class="border-b">I'm a pathless layout</div>
145 <div>
146 <Outlet />
147 </div>
148 </div>
149 )
151​
152const nestedPathlessLayout2Route = createRoute({
153 getParentRoute: () => pathlessLayoutRoute,
154 id: '_nestedPathlessLayout',
155 component: PathlessLayout2Component,
156})
157​
158function PathlessLayout2Component() {
159 return (
160 <div>
161 <div>I'm a nested pathless layout</div>
162 <div class="flex gap-2 border-b">
163 <Link
164 to="/route-a"
165 activeProps={{
166 class: 'font-bold',
167 }}
168 >
169 Go to Route A
170 </Link>
171 <Link
172 to="/route-b"
173 activeProps={{
174 class: 'font-bold',
175 }}
176 >
177 Go to Route B
178 </Link>
179 </div>
180 <div>
181 <Outlet />
182 </div>
183 </div>
184 )
186​
187const pathlessLayoutARoute = createRoute({
188 getParentRoute: () => nestedPathlessLayout2Route,
189 path: '/route-a',
190 component: PathlessLayoutAComponent,
191})
192​
193function PathlessLayoutAComponent() {
194 return <div>I'm route A!</div>
196​
197const pathlessLayoutBRoute = createRoute({
198 getParentRoute: () => nestedPathlessLayout2Route,
199 path: '/route-b',
200 component: PathlessLayoutBComponent,
201})
202​
203function PathlessLayoutBComponent() {
204 return <div>I'm route B!</div>
206​
207const routeTree = rootRoute.addChildren([
208 postsLayoutRoute.addChildren([postRoute, postsIndexRoute]),
209 pathlessLayoutRoute.addChildren([
210 nestedPathlessLayout2Route.addChildren([
211 pathlessLayoutARoute,
212 pathlessLayoutBRoute,
213 ]),
214 ]),
215 indexRoute,
216])
217​
218// Set up a Router instance
219const router = createRouter({
220 routeTree,
221 defaultPreload: 'intent',
222 defaultStaleTime: 5000,
223 scrollRestoration: true,
224})
225​
226// Register things for typesafety
227declare module '@tanstack/vue-router' {
228 interface Register {
229 router: typeof router
230 }
232​
233const rootElement = document.getElementById('app')!
234​
235if (!rootElement.innerHTML) {
236 createApp({
237 setup() {
238 return () => <RouterProvider router={router} />
239 },
240 }).mount('#app')