@@ -16,22 +16,20 @@ gem "js-routes"
16
16
17
17
There are several possible ways to setup JsRoutes:
18
18
19
- * [ Quick and easy] ( #quick-start ) - Recommended
19
+ 1 . [ Quick and easy] ( #quick-start ) - Recommended
20
20
* Uses Rack Middleware to automatically update routes locally
21
21
* Automatically generates routes files on javascript build
22
22
* Works great for a simple Rails application
23
- * [ Advanced Setup] ( #advanced-setup )
23
+ 1 . [ Advanced Setup] ( #advanced-setup )
24
24
* Allows very custom setups
25
25
* Automatic updates need to be customized
26
- * [ Webpacker ERB Loader] ( #webpacker ) - Legacy
26
+ 1 . [ Webpacker ERB Loader] ( #webpacker ) - Legacy
27
27
* Requires ESM module system (the default)
28
28
* Doesn't support typescript definitions
29
- * [ Sprockets] ( #sprockets ) - Legacy
29
+ 1 . [ Sprockets] ( #sprockets ) - Legacy
30
30
* Deprecated and not recommended for modern apps
31
31
32
- <div id =' quick-start ' ></div >
33
-
34
- ### Quick Start
32
+ ### Quick Start {#quick-start}
35
33
36
34
Setup [ Rack Middleware] ( https://guides.rubyonrails.org/rails_on_rack.html#action-dispatcher-middleware-stack )
37
35
to automatically generate and maintain ` routes.js ` file and corresponding
@@ -77,13 +75,10 @@ Add js-routes files to `.gitignore`:
77
75
/app/javascript/routes.d.ts
78
76
```
79
77
80
- <div id =' webpacker ' ></div >
81
-
82
- ### Webpacker ERB loader
78
+ ### Webpacker ERB loader {#webpack}
83
79
84
80
** IMPORTANT** : the setup doesn't support IDE autocompletion with [ Typescript] ( https://www.typescriptlang.org/docs/handbook/declaration-files/templates/module-d-ts.html )
85
81
86
-
87
82
#### Use a Generator
88
83
89
84
Run a command:
@@ -133,24 +128,22 @@ Create routes file `app/javascript/routes.js.erb`:
133
128
<%= JsRoutes.generate() %>
134
129
```
135
130
136
- Use routes wherever you need them:
131
+ Use routes wherever you need them:
137
132
138
133
``` javascript
139
134
import {post_path } from ' routes.js.erb' ;
140
135
141
136
alert (post_path (2 ));
142
137
```
143
138
144
- <div id =' advanced-setup ' ></div >
145
-
146
- ### Advanced Setup
139
+ ### Advanced Setup {#advanced-setup}
147
140
148
141
** IMPORTANT** : that this setup requires the JS routes file to be updates manually
149
142
150
143
Routes file can be generated with a ` rake ` task:
151
144
152
145
``` sh
153
- rake js:routes
146
+ rake js:routes
154
147
# OR for typescript support
155
148
rake js:routes:typescript
156
149
```
@@ -192,11 +185,9 @@ JsRoutes.generate!(
192
185
)
193
186
```
194
187
195
- <div id =' definitions ' ></div >
196
-
197
- #### Typescript Definitions
188
+ #### Typescript Definitions {#definitions}
198
189
199
- JsRoutes has typescript support out of the box.
190
+ JsRoutes has typescript support out of the box.
200
191
201
192
Restrictions:
202
193
@@ -208,16 +199,14 @@ More advanced setup would involve calling manually:
208
199
209
200
``` ruby
210
201
JsRoutes .definitions! # to output to file
211
- # or
202
+ # or
212
203
JsRoutes .definitions # to output to string
213
204
```
214
205
215
206
Even more advanced setups can be achieved by setting ` module_type ` to ` DTS ` inside [ configuration] ( #module_type )
216
207
which will cause any ` JsRoutes ` instance to generate defintions instead of routes themselves.
217
208
218
- <div id =" sprockets " ></div >
219
-
220
- ### Sprockets (Deprecated)
209
+ ### Sprockets (Deprecated) {#sprockets}
221
210
222
211
If you are using [ Sprockets] ( https://github.com/rails/sprockets-rails ) you may configure js-routes in the following way.
223
212
0 commit comments