Api
useScriptTriggerConsent()
Last updated by Harlan Wilton in chore: lint.
Load a script once you provide consent through either a resolvable consent or the accept method.
The trigger by itself does nothing - scripts will only load when consent is granted through either the
accept() method or by providing a consent option that resolves to true.Signature
function useScriptTriggerConsent(options?: ConsentScriptTriggerOptions): UseConsentScriptTriggerApi {}
Arguments
export interface ConsentScriptTriggerOptions {
/**
* An optional reactive (or promise) reference to the consent state. You can use this to accept the consent for scripts
* instead of using the accept() method.
*/
consent?: Promise<boolean | void> | Ref<boolean> | ComputedRef<boolean> | boolean
/**
* Should the script be loaded on the `requestIdleCallback` callback. This is useful for non-essential scripts that
* have already been consented to be loaded.
*/
postConsentTrigger?: NuxtUseScriptOptions['trigger']
}
Returns
A extended promise API with an accept method to accept the consent and load the script.
interface UseConsentScriptTriggerApi extends Promise<void> {
/**
* A function that can be called to accept the consent and load the script.
*/
accept: () => void
}
Examples
Basic Usage
app.vue
<script setup lang="ts">
const trigger = useScriptTriggerConsent()
useScript('https://example.com/script.js', { trigger })
</script>
<template>
<button @click="trigger.accept">
Accept Consent
</button>
</template>