Nested components

Let’s define a parent tag <account> and with a nested tag <subscription>:

  1. <account>
  2. <subscription plan={ props.plan } show-details={ true } />
  3. </account>
  1. <subscription>
  2. <h3>{ props.plan.name }</h3>
  3. <script>
  4. export default {
  5. onMounted(props) {
  6. // Get JS handle to props
  7. const {plan, showDetails} = props
  8. }
  9. }
  10. </script>
  11. </subscription>

Note how we named the show-details attribute, it is written in dash case but it will be converted to camel case inside the this.props object.

Then we mount the account component to the page with a plan configuration object:

  1. <body>
  2. <account></account>
  3. </body>
  4. <script>
  5. riot.mount('account', { plan: { name: 'small', term: 'monthly' } })
  6. </script>

Parent component properties are passed with the riot.mount method and child component ones are passed via the tag attribute.

Nested tags should be registered via riot.register call or they can be directly imported into the parent component. If you bundle your application your <account> template might look like this:

  1. <account>
  2. <subscription/>
  3. <script>
  4. import Subscription from './subscription.riot'
  5. export default {
  6. components: {
  7. Subscription
  8. }
  9. }
  10. </script>
  11. </account>

Slots

Using the <slot> tag you can inject custom HTML templates in a child component from its parent

Child component definition

  1. <greeting>
  2. <p>Hello <slot/></p>
  3. </greeting>

The child component is placed in a parent component injecting custom HTML into it

  1. <user>
  2. <greeting>
  3. <b>{ text }</b>
  4. </greeting>
  5. <script>
  6. export default {
  7. text: 'world'
  8. }
  9. </script>
  10. </user>

Result

  1. <user>
  2. <greeting>
  3. <p>Hello <b>world</b><p>
  4. </greeting>
  5. </user>

See API docs for slots.

Slots work only in compiled components, all the inner HTML of the components placed directly in your page DOM will be ignored.

:warning: Riot if, each and is directives are not supported on slot tags