Nested components
Let’s define a parent tag <account>
and with a nested tag <subscription>
:
<account>
<subscription plan={ props.plan } show-details={ true } />
</account>
<subscription>
<h3>{ props.plan.name }</h3>
<script>
export default {
onMounted(props) {
// Get JS handle to props
const {plan, showDetails} = props
}
}
</script>
</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:
<body>
<account></account>
</body>
<script>
riot.mount('account', { plan: { name: 'small', term: 'monthly' } })
</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:
<account>
<subscription/>
<script>
import Subscription from './subscription.riot'
export default {
components: {
Subscription
}
}
</script>
</account>
Slots
Using the <slot>
tag you can inject custom HTML templates in a child component from its parent
Child component definition
<greeting>
<p>Hello <slot/></p>
</greeting>
The child component is placed in a parent component injecting custom HTML into it
<user>
<greeting>
<b>{ text }</b>
</greeting>
<script>
export default {
text: 'world'
}
</script>
</user>
Result
<user>
<greeting>
<p>Hello <b>world</b><p>
</greeting>
</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.
Riot if
, each
and is
directives are not supported on slot tags