Skip to content

$components / useComponent()

Provides reactive access to OctoberCMS components registered on the current page.

Use $components in Vue templates or useComponent() in Composition API code. Components are addressed by the name or alias declared in the page or layout <october> block.

Basic Usage

vue
<october>
[sitePicker]
</october>

<template>
    <p v-if="$components.has('sitePicker')">
        {{ $components.sitePicker.get('isEnabled', false) }}
    </p>
</template>

<script lang="ts" setup>
import { useComponent } from '@ratmd/laika';

const sitePicker = useComponent('sitePicker');
console.log(sitePicker.get('isEnabled', false));
</script>

Components Facade

AccessDescription
$components.aliasReturns a reactive handle for a registered component alias.
$components.has(alias)Checks whether an alias exists in the current payload.
$components.get(alias)Returns its handle, or null when the alias is not registered.

Component Handle

Each handle exposes the current server payload together with helper methods.

PropertyDescription
componentRegistered OctoberCMS component name.
aliasComponent alias used by the page.
classServer-side component class.
optionsConfigured component properties.
propsValues exposed to the page by the component.
methodsPublic component methods available for lazy loading.
varsPublic component properties available for lazy loading.
htmlServer-rendered component markup, when loaded.
MethodDescription
get(key, fallback?)Returns a loaded property or the supplied fallback value.
exists(key)Checks whether a property, public method or public variable is available.
loaded(key)Checks whether a value is already present in props.
load(keyOrKeys)Lazily requests one or more properties from the server.
loadHtml()Lazily requests the component's server-rendered markup.
request(handler, options?)Calls an AJAX handler scoped to this component alias.

Lazy Properties

Lazy requests preserve the current Vue page state and patch only the requested component values.

vue
<script lang="ts" setup>
import { onMounted } from 'vue';
import { useComponent } from '@ratmd/laika';

const sitePicker = useComponent('sitePicker');

onMounted(async () => {
    if (sitePicker.exists('sites') && !sitePicker.loaded('sites')) {
        await sitePicker.load('sites');
    }
});
</script>

Component AJAX Handlers

request() automatically prefixes the handler with the current component alias.

ts
const result = await sitePicker.request('onRefresh', {
    data: { locale: 'en' },
});

See the <PageComponent /> component to render a handle or its server-generated HTML.

This software is not an official OctoberCMS product and is not associated with, sponsored by, or endorsed by OctoberCMS.