Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add hooks keyword to pt options to access all lifecycle methods on components and directives #4103

Closed
mertsincan opened this issue Jul 4, 2023 · 1 comment
Assignees
Labels
Type: New Feature Issue contains a new feature or new component request
Milestone

Comments

@mertsincan
Copy link
Member

mertsincan commented Jul 4, 2023

Users can access with on prefix + lifecycle phase name;

// Component
onBeforeCreate?(): void;
onCreated?(): void;
onBeforeMount?(): void;
onMounted?(): void;
onBeforeUpdate?(): void;
onUpdated?(): void;
onBeforeUnmount?(): void;
onUnmounted?(): void;

// Directive
onCreated?: (instance: DirectiveInstance<T, V> | undefined, options: DirectiveOptions<T, null, V>) => void;
onBeforeMount?: (instance: DirectiveInstance<T, V> | undefined, options: DirectiveOptions<T, null, V>) => void;
onMounted?: (instance: DirectiveInstance<T, V> | undefined, options: DirectiveOptions<T, null, V>) => void;
onBeforeUpdate?: (instance: DirectiveInstance<T, V> | undefined, options: DirectiveOptions<T, VNode<any, T>, V>) => void;
onUpdated?: (instance: DirectiveInstance<T, V> | undefined, options: DirectiveOptions<T, VNode<any, T>, V>) => void;
onBeforeUnmount?: (instance: DirectiveInstance<T, V> | undefined, options: DirectiveOptions<T, null, V>) => void;
onUnmounted?: (instance: DirectiveInstance<T, V> | undefined, options: DirectiveOptions<T, null, V>) => void;

Usage;

// Component
<Badge
    value="2"
    :pt="{
        hooks: {
            /* lifecycle hooks */
            onMounted() {
                console.log('mounted');
            },
            ...
        }
    }"
/>
// Directive
<i
    v-badge="{
        value: '2',
        pt: {
            hooks: {
                /* lifecycle hooks */
                onMounted(instance, options) {
                    console.log('mounted');
                },
                ...
            }
        }
    }"
    class="pi pi-bell p-overlay-badge"
    style="font-size: 2rem"
/>

Also, this feature can be used with global PT definition.

// global
const pt = {
    badge: {
        hooks: {
            /* lifecycle hooks */
            onMounted() {
                console.log('mounted');
            },
            ...
        }
    }
}

vueApp.use(PrimeVue, { pt });

....
<Badge value="2" />
<Badge value="4" />
<Badge value="6" />
@mertsincan mertsincan added the Type: New Feature Issue contains a new feature or new component request label Jul 4, 2023
@mertsincan mertsincan added this to the 3.30.0 milestone Jul 4, 2023
@mertsincan mertsincan self-assigned this Jul 4, 2023
@mertsincan
Copy link
Member Author

Fixed in 3442aca

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Type: New Feature Issue contains a new feature or new component request
Projects
None yet
Development

No branches or pull requests

1 participant