lego-dom 1.3.4 → 1.4.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +55 -0
- package/main.js +24 -3
- package/package.json +1 -1
- package/vite-plugin.js +0 -14
- package/.github/workflows/deploy-docs.yml +0 -56
- package/.legodom +0 -87
- package/docs/.vitepress/config.js +0 -161
- package/docs/api/config.md +0 -95
- package/docs/api/define.md +0 -58
- package/docs/api/directives.md +0 -50
- package/docs/api/globals.md +0 -29
- package/docs/api/index.md +0 -30
- package/docs/api/lifecycle.md +0 -40
- package/docs/api/route.md +0 -37
- package/docs/api/vite-plugin.md +0 -58
- package/docs/contributing/01-welcome.md +0 -38
- package/docs/contributing/02-registry.md +0 -133
- package/docs/contributing/03-batcher.md +0 -110
- package/docs/contributing/04-reactivity.md +0 -87
- package/docs/contributing/05-caching.md +0 -59
- package/docs/contributing/06-init.md +0 -136
- package/docs/contributing/07-observer.md +0 -72
- package/docs/contributing/08-snap.md +0 -140
- package/docs/contributing/09-diffing.md +0 -69
- package/docs/contributing/10-studs.md +0 -78
- package/docs/contributing/11-scanner.md +0 -117
- package/docs/contributing/12-render.md +0 -138
- package/docs/contributing/13-directives.md +0 -243
- package/docs/contributing/14-events.md +0 -57
- package/docs/contributing/15-router.md +0 -57
- package/docs/contributing/16-state.md +0 -47
- package/docs/contributing/17-legodom.md +0 -48
- package/docs/contributing/index.md +0 -24
- package/docs/examples/form.md +0 -42
- package/docs/examples/index.md +0 -104
- package/docs/examples/routing.md +0 -409
- package/docs/examples/sfc-showcase.md +0 -34
- package/docs/examples/todo-app.md +0 -383
- package/docs/guide/cdn-usage.md +0 -354
- package/docs/guide/components.md +0 -418
- package/docs/guide/directives.md +0 -539
- package/docs/guide/directory-structure.md +0 -248
- package/docs/guide/faq.md +0 -210
- package/docs/guide/getting-started.md +0 -262
- package/docs/guide/index.md +0 -88
- package/docs/guide/lifecycle.md +0 -525
- package/docs/guide/quick-start.md +0 -49
- package/docs/guide/reactivity.md +0 -415
- package/docs/guide/routing.md +0 -334
- package/docs/guide/server-side.md +0 -134
- package/docs/guide/sfc.md +0 -464
- package/docs/guide/templating.md +0 -388
- package/docs/index.md +0 -160
- package/docs/public/logo.svg +0 -17
- package/docs/router/basic-routing.md +0 -103
- package/docs/router/cold-entry.md +0 -91
- package/docs/router/history.md +0 -69
- package/docs/router/index.md +0 -73
- package/docs/router/resolver.md +0 -74
- package/docs/router/surgical-swaps.md +0 -134
- package/docs/tutorial/01-project-setup.md +0 -152
- package/docs/tutorial/02-your-first-component.md +0 -226
- package/docs/tutorial/03-adding-routes.md +0 -279
- package/docs/tutorial/04-multi-page-app.md +0 -329
- package/docs/tutorial/05-state-and-globals.md +0 -285
- package/docs/tutorial/index.md +0 -40
- package/examples/vite-app/README.md +0 -71
- package/examples/vite-app/index.html +0 -42
- package/examples/vite-app/package.json +0 -18
- package/examples/vite-app/src/app.css +0 -3
- package/examples/vite-app/src/app.js +0 -29
- package/examples/vite-app/src/components/app-navbar.lego +0 -34
- package/examples/vite-app/src/components/customers/customer-details.lego +0 -24
- package/examples/vite-app/src/components/customers/customer-orders.lego +0 -21
- package/examples/vite-app/src/components/customers/order-list.lego +0 -55
- package/examples/vite-app/src/components/greeting-card.lego +0 -41
- package/examples/vite-app/src/components/sample-component.lego +0 -75
- package/examples/vite-app/src/components/shells/customers-shell.lego +0 -21
- package/examples/vite-app/src/components/side-menu.lego +0 -46
- package/examples/vite-app/src/components/todo-list.lego +0 -239
- package/examples/vite-app/src/components/widgets/user-card.lego +0 -27
- package/examples/vite-app/vite.config.js +0 -22
- package/tests/error.test.js +0 -74
- package/tests/main.test.js +0 -103
- package/tests/memory.test.js +0 -68
- package/tests/monitoring.test.js +0 -74
- package/tests/naming.test.js +0 -74
- package/tests/parse-lego.test.js +0 -65
- package/tests/security.test.js +0 -67
- package/tests/server.test.js +0 -114
- package/tests/syntax.test.js +0 -67
|
@@ -1,226 +0,0 @@
|
|
|
1
|
-
# Step 2: Your First Component
|
|
2
|
-
|
|
3
|
-
Now let's create a component! By the end of this page, you'll understand the `.lego` file format and have a working home page.
|
|
4
|
-
|
|
5
|
-
## Create Your First `.lego` File
|
|
6
|
-
|
|
7
|
-
Create `src/components/home-page.lego`:
|
|
8
|
-
|
|
9
|
-
```html
|
|
10
|
-
<template>
|
|
11
|
-
<div class="hero">
|
|
12
|
-
<h1>[[ title ]]</h1>
|
|
13
|
-
<p>[[ subtitle ]]</p>
|
|
14
|
-
<button @click="handleClick()">[[ buttonText ]]</button>
|
|
15
|
-
</div>
|
|
16
|
-
|
|
17
|
-
<div class="features">
|
|
18
|
-
<div class="feature" b-for="feature in features">
|
|
19
|
-
<span class="icon">[[ feature.icon ]]</span>
|
|
20
|
-
<h3>[[ feature.title ]]</h3>
|
|
21
|
-
<p>[[ feature.description ]]</p>
|
|
22
|
-
</div>
|
|
23
|
-
</div>
|
|
24
|
-
</template>
|
|
25
|
-
|
|
26
|
-
<style>
|
|
27
|
-
self {
|
|
28
|
-
display: block;
|
|
29
|
-
min-height: 100vh;
|
|
30
|
-
background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
|
|
31
|
-
color: white;
|
|
32
|
-
padding: 4rem 2rem;
|
|
33
|
-
}
|
|
34
|
-
|
|
35
|
-
.hero {
|
|
36
|
-
text-align: center;
|
|
37
|
-
max-width: 600px;
|
|
38
|
-
margin: 0 auto 4rem;
|
|
39
|
-
}
|
|
40
|
-
|
|
41
|
-
.hero h1 {
|
|
42
|
-
font-size: 3rem;
|
|
43
|
-
margin-bottom: 1rem;
|
|
44
|
-
}
|
|
45
|
-
|
|
46
|
-
.hero p {
|
|
47
|
-
font-size: 1.25rem;
|
|
48
|
-
opacity: 0.9;
|
|
49
|
-
margin-bottom: 2rem;
|
|
50
|
-
}
|
|
51
|
-
|
|
52
|
-
.hero button {
|
|
53
|
-
background: white;
|
|
54
|
-
color: #667eea;
|
|
55
|
-
border: none;
|
|
56
|
-
padding: 1rem 2.5rem;
|
|
57
|
-
font-size: 1.1rem;
|
|
58
|
-
font-weight: 600;
|
|
59
|
-
border-radius: 50px;
|
|
60
|
-
cursor: pointer;
|
|
61
|
-
transition: transform 0.2s, box-shadow 0.2s;
|
|
62
|
-
}
|
|
63
|
-
|
|
64
|
-
.hero button:hover {
|
|
65
|
-
transform: translateY(-2px);
|
|
66
|
-
box-shadow: 0 10px 20px rgba(0,0,0,0.2);
|
|
67
|
-
}
|
|
68
|
-
|
|
69
|
-
.features {
|
|
70
|
-
display: grid;
|
|
71
|
-
grid-template-columns: repeat(auto-fit, minmax(250px, 1fr));
|
|
72
|
-
gap: 2rem;
|
|
73
|
-
max-width: 900px;
|
|
74
|
-
margin: 0 auto;
|
|
75
|
-
}
|
|
76
|
-
|
|
77
|
-
.feature {
|
|
78
|
-
background: rgba(255,255,255,0.1);
|
|
79
|
-
padding: 2rem;
|
|
80
|
-
border-radius: 16px;
|
|
81
|
-
text-align: center;
|
|
82
|
-
backdrop-filter: blur(10px);
|
|
83
|
-
}
|
|
84
|
-
|
|
85
|
-
.feature .icon {
|
|
86
|
-
font-size: 3rem;
|
|
87
|
-
display: block;
|
|
88
|
-
margin-bottom: 1rem;
|
|
89
|
-
}
|
|
90
|
-
|
|
91
|
-
.feature h3 {
|
|
92
|
-
margin-bottom: 0.5rem;
|
|
93
|
-
}
|
|
94
|
-
|
|
95
|
-
.feature p {
|
|
96
|
-
opacity: 0.8;
|
|
97
|
-
font-size: 0.9rem;
|
|
98
|
-
}
|
|
99
|
-
</style>
|
|
100
|
-
|
|
101
|
-
<script>
|
|
102
|
-
export default {
|
|
103
|
-
title: 'Welcome to My App',
|
|
104
|
-
subtitle: 'Built with LegoDOM – the tiny framework that loves developers',
|
|
105
|
-
buttonText: 'Get Started',
|
|
106
|
-
|
|
107
|
-
features: [
|
|
108
|
-
{ icon: '⚡', title: 'Lightning Fast', description: 'No virtual DOM overhead' },
|
|
109
|
-
{ icon: '🧩', title: 'Component-Based', description: 'Reusable building blocks' },
|
|
110
|
-
{ icon: '🎨', title: 'Scoped Styles', description: 'CSS that never leaks' }
|
|
111
|
-
],
|
|
112
|
-
|
|
113
|
-
handleClick() {
|
|
114
|
-
// We'll wire this up to navigation soon!
|
|
115
|
-
alert('Clicked! Next: we\'ll navigate to /login');
|
|
116
|
-
},
|
|
117
|
-
|
|
118
|
-
mounted() {
|
|
119
|
-
console.log('Home page mounted!');
|
|
120
|
-
}
|
|
121
|
-
}
|
|
122
|
-
</script>
|
|
123
|
-
```
|
|
124
|
-
|
|
125
|
-
## Understanding the Structure
|
|
126
|
-
|
|
127
|
-
Every `.lego` file has three sections:
|
|
128
|
-
|
|
129
|
-
### 1. `<template>` – Your HTML
|
|
130
|
-
|
|
131
|
-
```html
|
|
132
|
-
<template>
|
|
133
|
-
<h1>[[ title ]]</h1> <!-- Reactive text -->
|
|
134
|
-
<button @click="doSomething()"> <!-- Event binding -->
|
|
135
|
-
<div b-for="item in items"> <!-- Loop directive -->
|
|
136
|
-
</template>
|
|
137
|
-
```
|
|
138
|
-
|
|
139
|
-
### 2. `<style>` – Scoped CSS
|
|
140
|
-
|
|
141
|
-
```html
|
|
142
|
-
<style>
|
|
143
|
-
self {
|
|
144
|
-
/* 'self' targets the component root (like :host) */
|
|
145
|
-
display: block;
|
|
146
|
-
}
|
|
147
|
-
|
|
148
|
-
button {
|
|
149
|
-
/* These styles ONLY affect this component */
|
|
150
|
-
}
|
|
151
|
-
</style>
|
|
152
|
-
```
|
|
153
|
-
|
|
154
|
-
### 3. `<script>` – Logic & State
|
|
155
|
-
|
|
156
|
-
```html
|
|
157
|
-
<script>
|
|
158
|
-
export default {
|
|
159
|
-
// Reactive properties
|
|
160
|
-
title: 'Hello',
|
|
161
|
-
count: 0,
|
|
162
|
-
items: [],
|
|
163
|
-
|
|
164
|
-
// Methods
|
|
165
|
-
doSomething() {
|
|
166
|
-
this.count++; // Mutation triggers re-render!
|
|
167
|
-
},
|
|
168
|
-
|
|
169
|
-
// Lifecycle
|
|
170
|
-
mounted() {
|
|
171
|
-
console.log('Component is ready');
|
|
172
|
-
}
|
|
173
|
-
}
|
|
174
|
-
</script>
|
|
175
|
-
```
|
|
176
|
-
|
|
177
|
-
## Register the Route
|
|
178
|
-
|
|
179
|
-
Now update `src/app.js` to show your component:
|
|
180
|
-
|
|
181
|
-
```javascript{7-8}
|
|
182
|
-
import { Lego } from 'lego-dom';
|
|
183
|
-
import registerComponents from 'virtual:lego-components';
|
|
184
|
-
|
|
185
|
-
registerComponents();
|
|
186
|
-
|
|
187
|
-
// Add this line:
|
|
188
|
-
Lego.route('/', 'home-page');
|
|
189
|
-
|
|
190
|
-
await Lego.init();
|
|
191
|
-
```
|
|
192
|
-
|
|
193
|
-
## See It Live
|
|
194
|
-
|
|
195
|
-
```bash
|
|
196
|
-
npm run dev
|
|
197
|
-
```
|
|
198
|
-
|
|
199
|
-
Open `http://localhost:5173` and you'll see your beautiful home page!
|
|
200
|
-
|
|
201
|
-
## What You've Learned
|
|
202
|
-
|
|
203
|
-
✅ The three-section `.lego` file structure
|
|
204
|
-
✅ Template syntax: `[[ ]]` for data, `@click` for events, `b-for` for loops
|
|
205
|
-
✅ The `self` keyword for component root styling
|
|
206
|
-
✅ How to export state and methods from `<script>`
|
|
207
|
-
✅ Connecting a component to a route
|
|
208
|
-
|
|
209
|
-
## Key Pattern: Component → Route → Display
|
|
210
|
-
|
|
211
|
-
```
|
|
212
|
-
home-page.lego → Lego.route('/', 'home-page') → <lego-router> shows it
|
|
213
|
-
```
|
|
214
|
-
|
|
215
|
-
The filename (minus `.lego`) becomes the component name. Routes map URLs to component names. The router displays the matched component.
|
|
216
|
-
|
|
217
|
-
---
|
|
218
|
-
|
|
219
|
-
<div style="display: flex; justify-content: space-between; margin-top: 3rem;">
|
|
220
|
-
<a href="./01-project-setup" style="display: inline-block; background: #eee; color: #333; padding: 0.75rem 1.5rem; border-radius: 6px; text-decoration: none; font-weight: 600;">
|
|
221
|
-
← Previous: Project Setup
|
|
222
|
-
</a>
|
|
223
|
-
<a href="./03-adding-routes" style="display: inline-block; background: #4CAF50; color: white; padding: 0.75rem 1.5rem; border-radius: 6px; text-decoration: none; font-weight: 600;">
|
|
224
|
-
Next: Adding Routes →
|
|
225
|
-
</a>
|
|
226
|
-
</div>
|
|
@@ -1,279 +0,0 @@
|
|
|
1
|
-
# Step 3: Adding Routes
|
|
2
|
-
|
|
3
|
-
Now let's make your app navigable. By the end of this page, you'll understand exactly **where routes go**, **how to define them**, and **how to navigate between pages**.
|
|
4
|
-
|
|
5
|
-
## The Golden Question: Where Do Routes Go?
|
|
6
|
-
|
|
7
|
-
**Answer: In your entry file (`app.js`), before `Lego.init()`.**
|
|
8
|
-
|
|
9
|
-
```javascript
|
|
10
|
-
// src/app.js
|
|
11
|
-
|
|
12
|
-
import { Lego } from 'lego-dom';
|
|
13
|
-
import registerComponents from 'virtual:lego-components';
|
|
14
|
-
|
|
15
|
-
// 1. Register components
|
|
16
|
-
registerComponents();
|
|
17
|
-
|
|
18
|
-
// 2. Define ALL your routes here ⭐
|
|
19
|
-
Lego.route('/', 'home-page');
|
|
20
|
-
Lego.route('/login', 'login-page');
|
|
21
|
-
Lego.route('/welcome', 'welcome-page');
|
|
22
|
-
Lego.route('/users/:id', 'user-profile'); // Dynamic route
|
|
23
|
-
|
|
24
|
-
// 3. Initialize (must come AFTER routes)
|
|
25
|
-
await Lego.init();
|
|
26
|
-
```
|
|
27
|
-
|
|
28
|
-
::: tip Why Before init()?
|
|
29
|
-
`Lego.init()` starts the router. If routes aren't defined yet, the router has nothing to match. Always define routes **before** calling `init()`.
|
|
30
|
-
:::
|
|
31
|
-
|
|
32
|
-
## Route Syntax
|
|
33
|
-
|
|
34
|
-
```javascript
|
|
35
|
-
Lego.route(path, componentName, middleware?)
|
|
36
|
-
```
|
|
37
|
-
|
|
38
|
-
| Argument | Description | Example |
|
|
39
|
-
|----------|-------------|---------|
|
|
40
|
-
| `path` | URL pattern | `'/'`, `'/login'`, `'/users/:id'` |
|
|
41
|
-
| `componentName` | The component to render (filename minus `.lego`) | `'home-page'`, `'login-page'` |
|
|
42
|
-
| `middleware` | Optional guard function | `(params, globals) => boolean` |
|
|
43
|
-
|
|
44
|
-
### Dynamic Routes
|
|
45
|
-
|
|
46
|
-
Use `:param` for URL parameters:
|
|
47
|
-
|
|
48
|
-
```javascript
|
|
49
|
-
Lego.route('/users/:id', 'user-profile');
|
|
50
|
-
Lego.route('/posts/:postId/comments/:commentId', 'comment-view');
|
|
51
|
-
```
|
|
52
|
-
|
|
53
|
-
Access them in your component:
|
|
54
|
-
|
|
55
|
-
```javascript
|
|
56
|
-
// In your .lego file's <script>
|
|
57
|
-
mounted() {
|
|
58
|
-
const userId = this.$route.params.id;
|
|
59
|
-
console.log('User ID:', userId);
|
|
60
|
-
}
|
|
61
|
-
```
|
|
62
|
-
|
|
63
|
-
## Navigating Between Pages
|
|
64
|
-
|
|
65
|
-
### Method 1: The `b-link` Attribute (Declarative)
|
|
66
|
-
|
|
67
|
-
```html
|
|
68
|
-
<!-- In any component template -->
|
|
69
|
-
<a href="/login" b-link>Go to Login</a>
|
|
70
|
-
<a href="/users/42" b-link>View User 42</a>
|
|
71
|
-
```
|
|
72
|
-
|
|
73
|
-
The `b-link` attribute turns a regular `<a>` tag into a SPA link. No page reload!
|
|
74
|
-
|
|
75
|
-
### Method 2: The `$go()` Helper (Programmatic)
|
|
76
|
-
|
|
77
|
-
```javascript
|
|
78
|
-
// In your component methods
|
|
79
|
-
handleLoginClick() {
|
|
80
|
-
this.$go('/login').get();
|
|
81
|
-
}
|
|
82
|
-
|
|
83
|
-
// Or with the global reference
|
|
84
|
-
Lego.globals.$go('/welcome').get();
|
|
85
|
-
```
|
|
86
|
-
|
|
87
|
-
### Method 3: Using `b-target` (Surgical Updates)
|
|
88
|
-
|
|
89
|
-
Want to update only a specific part of the page?
|
|
90
|
-
|
|
91
|
-
```html
|
|
92
|
-
<!-- Update just the sidebar, not the whole page -->
|
|
93
|
-
<a href="/sidebar/settings" b-target="#sidebar" b-link="false">Settings</a>
|
|
94
|
-
```
|
|
95
|
-
|
|
96
|
-
## Create a Login Page
|
|
97
|
-
|
|
98
|
-
Let's add a login page to navigate to.
|
|
99
|
-
|
|
100
|
-
Create `src/components/login-page.lego`:
|
|
101
|
-
|
|
102
|
-
```html
|
|
103
|
-
<template>
|
|
104
|
-
<div class="login-container">
|
|
105
|
-
<h1>Login</h1>
|
|
106
|
-
<form @submit="handleSubmit(event)">
|
|
107
|
-
<div class="form-group">
|
|
108
|
-
<label>Email</label>
|
|
109
|
-
<input type="email" b-sync="email" placeholder="you@example.com" required>
|
|
110
|
-
</div>
|
|
111
|
-
<div class="form-group">
|
|
112
|
-
<label>Password</label>
|
|
113
|
-
<input type="password" b-sync="password" placeholder="••••••••" required>
|
|
114
|
-
</div>
|
|
115
|
-
<button type="submit">Sign In</button>
|
|
116
|
-
</form>
|
|
117
|
-
<p class="back-link">
|
|
118
|
-
<a href="/" b-link>← Back to Home</a>
|
|
119
|
-
</p>
|
|
120
|
-
</div>
|
|
121
|
-
</template>
|
|
122
|
-
|
|
123
|
-
<style>
|
|
124
|
-
self {
|
|
125
|
-
display: flex;
|
|
126
|
-
align-items: center;
|
|
127
|
-
justify-content: center;
|
|
128
|
-
min-height: 100vh;
|
|
129
|
-
background: #f5f5f5;
|
|
130
|
-
}
|
|
131
|
-
|
|
132
|
-
.login-container {
|
|
133
|
-
background: white;
|
|
134
|
-
padding: 3rem;
|
|
135
|
-
border-radius: 16px;
|
|
136
|
-
box-shadow: 0 10px 40px rgba(0,0,0,0.1);
|
|
137
|
-
width: 100%;
|
|
138
|
-
max-width: 400px;
|
|
139
|
-
}
|
|
140
|
-
|
|
141
|
-
h1 {
|
|
142
|
-
text-align: center;
|
|
143
|
-
margin-bottom: 2rem;
|
|
144
|
-
color: #333;
|
|
145
|
-
}
|
|
146
|
-
|
|
147
|
-
.form-group {
|
|
148
|
-
margin-bottom: 1.5rem;
|
|
149
|
-
}
|
|
150
|
-
|
|
151
|
-
label {
|
|
152
|
-
display: block;
|
|
153
|
-
margin-bottom: 0.5rem;
|
|
154
|
-
font-weight: 600;
|
|
155
|
-
color: #555;
|
|
156
|
-
}
|
|
157
|
-
|
|
158
|
-
input {
|
|
159
|
-
width: 100%;
|
|
160
|
-
padding: 0.75rem 1rem;
|
|
161
|
-
border: 2px solid #e0e0e0;
|
|
162
|
-
border-radius: 8px;
|
|
163
|
-
font-size: 1rem;
|
|
164
|
-
transition: border-color 0.2s;
|
|
165
|
-
}
|
|
166
|
-
|
|
167
|
-
input:focus {
|
|
168
|
-
outline: none;
|
|
169
|
-
border-color: #667eea;
|
|
170
|
-
}
|
|
171
|
-
|
|
172
|
-
button {
|
|
173
|
-
width: 100%;
|
|
174
|
-
padding: 1rem;
|
|
175
|
-
background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
|
|
176
|
-
color: white;
|
|
177
|
-
border: none;
|
|
178
|
-
border-radius: 8px;
|
|
179
|
-
font-size: 1rem;
|
|
180
|
-
font-weight: 600;
|
|
181
|
-
cursor: pointer;
|
|
182
|
-
transition: transform 0.2s;
|
|
183
|
-
}
|
|
184
|
-
|
|
185
|
-
button:hover {
|
|
186
|
-
transform: translateY(-2px);
|
|
187
|
-
}
|
|
188
|
-
|
|
189
|
-
.back-link {
|
|
190
|
-
text-align: center;
|
|
191
|
-
margin-top: 1.5rem;
|
|
192
|
-
}
|
|
193
|
-
|
|
194
|
-
.back-link a {
|
|
195
|
-
color: #667eea;
|
|
196
|
-
text-decoration: none;
|
|
197
|
-
}
|
|
198
|
-
</style>
|
|
199
|
-
|
|
200
|
-
<script>
|
|
201
|
-
export default {
|
|
202
|
-
email: '',
|
|
203
|
-
password: '',
|
|
204
|
-
|
|
205
|
-
handleSubmit(event) {
|
|
206
|
-
event.preventDefault();
|
|
207
|
-
console.log('Login attempt:', this.email);
|
|
208
|
-
|
|
209
|
-
// Navigate to welcome page
|
|
210
|
-
this.$go('/welcome').get();
|
|
211
|
-
}
|
|
212
|
-
}
|
|
213
|
-
</script>
|
|
214
|
-
```
|
|
215
|
-
|
|
216
|
-
## Update Your Routes
|
|
217
|
-
|
|
218
|
-
```javascript
|
|
219
|
-
// src/app.js
|
|
220
|
-
import { Lego } from 'lego-dom';
|
|
221
|
-
import registerComponents from 'virtual:lego-components';
|
|
222
|
-
|
|
223
|
-
registerComponents();
|
|
224
|
-
|
|
225
|
-
Lego.route('/', 'home-page');
|
|
226
|
-
Lego.route('/login', 'login-page'); // Add this!
|
|
227
|
-
|
|
228
|
-
await Lego.init();
|
|
229
|
-
```
|
|
230
|
-
|
|
231
|
-
## Wire Up Navigation from Home
|
|
232
|
-
|
|
233
|
-
Update `home-page.lego`'s button to navigate:
|
|
234
|
-
|
|
235
|
-
```javascript
|
|
236
|
-
// In the <script> section
|
|
237
|
-
handleClick() {
|
|
238
|
-
this.$go('/login').get();
|
|
239
|
-
}
|
|
240
|
-
```
|
|
241
|
-
|
|
242
|
-
## Test It!
|
|
243
|
-
|
|
244
|
-
```bash
|
|
245
|
-
npm run dev
|
|
246
|
-
```
|
|
247
|
-
|
|
248
|
-
1. Open `http://localhost:5173` – see your home page
|
|
249
|
-
2. Click "Get Started" – navigates to `/login`
|
|
250
|
-
3. Click "← Back to Home" – navigates back to `/`
|
|
251
|
-
4. Use browser back/forward – it just works!
|
|
252
|
-
|
|
253
|
-
## What You've Learned
|
|
254
|
-
|
|
255
|
-
✅ Routes go in `app.js`, before `Lego.init()`
|
|
256
|
-
✅ `Lego.route(path, component)` maps URLs to components
|
|
257
|
-
✅ `b-link` on `<a>` tags for declarative navigation
|
|
258
|
-
✅ `$go(path).get()` for programmatic navigation
|
|
259
|
-
✅ Dynamic routes with `:param` syntax
|
|
260
|
-
|
|
261
|
-
## Quick Reference
|
|
262
|
-
|
|
263
|
-
| I want to... | Use this |
|
|
264
|
-
|--------------|----------|
|
|
265
|
-
| Define a route | `Lego.route('/path', 'component-name')` |
|
|
266
|
-
| Navigate via link | `<a href="/path" b-link>Click</a>` |
|
|
267
|
-
| Navigate via JS | `this.$go('/path').get()` |
|
|
268
|
-
| Get route params | `this.$route.params.id` |
|
|
269
|
-
|
|
270
|
-
---
|
|
271
|
-
|
|
272
|
-
<div style="display: flex; justify-content: space-between; margin-top: 3rem;">
|
|
273
|
-
<a href="./02-your-first-component" style="display: inline-block; background: #eee; color: #333; padding: 0.75rem 1.5rem; border-radius: 6px; text-decoration: none; font-weight: 600;">
|
|
274
|
-
← Previous: Your First Component
|
|
275
|
-
</a>
|
|
276
|
-
<a href="./04-multi-page-app" style="display: inline-block; background: #4CAF50; color: white; padding: 0.75rem 1.5rem; border-radius: 6px; text-decoration: none; font-weight: 600;">
|
|
277
|
-
Next: Multi-Page App →
|
|
278
|
-
</a>
|
|
279
|
-
</div>
|