|
1 | 1 | # Custom Vue.js Components
|
2 | 2 |
|
3 |
| -In order to equip a Ruby component with some JavaScript, we associate the Ruby component with a Vue.js JavaScript component. The Ruby component therefore needs to inherit from `Matestack::Ui::VueJsComponent`. Matestack will then render a HTML component tag with some special attributes and props around the response defined in the Ruby component. The Vue.js JavaScript component \(defined in a separate JavaScript file and managed via Webpacker\) will treat the response of the Ruby component as its template. |
| 3 | +In order to equip a Ruby component with some JavaScript, we associate the Ruby component with a Vue.js JavaScript component. The Ruby component therefore needs to inherit from `Matestack::Ui::VueJsComponent`. Matestack will then render a HTML component tag with some special attributes and props around the response defined in the Ruby component. The Vue.js JavaScript component (defined in a separate JavaScript file and managed via Webpacker) will treat the response of the Ruby component as its template. |
4 | 4 |
|
5 | 5 | ## Structure, files and registry
|
6 | 6 |
|
@@ -62,6 +62,7 @@ The Vue.js JavaScript file needs to be imported by some kind of JavaScript packa
|
62 | 62 | For **Webpacker** it would look like this:
|
63 | 63 |
|
64 | 64 | `javascript/packs/application.js`
|
| 65 | + |
65 | 66 | ```js
|
66 | 67 | import { createApp } from 'vue'
|
67 | 68 | import MatestackUiVueJs from 'matestack-ui-vuejs'
|
@@ -89,12 +90,11 @@ If setup correctly, Matestack will render the component to:
|
89 | 90 | </component>
|
90 | 91 | ```
|
91 | 92 |
|
92 |
| -As you can see, the component tag is referencing the Vue.js JavaScript component via `is` and tells the JavaScript component that it should use the inner html \(coming from the `response` method\) as the template of the component. |
| 93 | +As you can see, the component tag is referencing the Vue.js JavaScript component via `is` and tells the JavaScript component that it should use the inner html (coming from the `response` method) as the template of the component. |
93 | 94 |
|
94 | 95 | `{{ vc.foo }}` will be evaluated to "bar" as soon as Vue.js has booted and mounted the component in the browser. `{{ foo }}` is not working!
|
95 | 96 |
|
96 |
| -The prefix `vc.` is short for `Vue Component` and is necessary for referencing the correct component scope. Within the JavaScript file, you still simply use `this.` |
97 |
| -The prefix is required since Vue 3 removed proper inline template support. Behind the scenes MatestackUiVueJs is using Vue's `default slot` mechanism in order to enable inline templates. |
| 97 | +The prefix `vc.` is short for `Vue Component` and is necessary for referencing the correct component scope. Within the JavaScript file, you still simply use `this.` The prefix is required since Vue 3 removed proper inline template support. Behind the scenes MatestackUiVueJs is using Vue's `default slot` mechanism in order to enable inline templates. |
98 | 98 |
|
99 | 99 | Matestack will inject JSON objects into the Vue.js JavaScript component through the `props` and `params` tags if either props or params are available. This data is injected once on initial server side rendering of the component's markup. See below, how you can pass in data to the Vue.js JavaScript component.
|
100 | 100 |
|
@@ -185,8 +185,18 @@ As we're pretty much implementing pure Vue.js components, you can refer to the [
|
185 | 185 |
|
186 | 186 | #### component $refs
|
187 | 187 |
|
188 |
| -- use `this.getRefs()` instead of `this.$refs` |
| 188 | +* use `this.getRefs()` instead of `this.$refs` |
| 189 | +* user `scoped_ref()` when applying refs to your componen template: |
| 190 | + |
| 191 | +```ruby |
| 192 | +def response |
| 193 | + # ... |
| 194 | + div ref: "some-ref" # <-- not picked up by this.getRefs() |
| 195 | + # ... |
| 196 | + div "matestack-ui-core-ref": scoped_ref('some-ref') |
| 197 | +end |
| 198 | +``` |
189 | 199 |
|
190 | 200 | #### component $el
|
191 | 201 |
|
192 |
| -- use `this.getElement()` instead of `this.$el` |
| 202 | +* use `this.getElement()` instead of `this.$el` |
0 commit comments