API

useScript

useScript 函数的 API 文档。

此可组合项是 Unhead useScript 的包装器,在顶部添加了一些额外的 Nuxt 好处,有关完整文档,请参考此。

签名

export function useScript<T extends Record<string | symbol, any>>(input: UseScriptInput, options?: NuxtUseScriptOptions): T & { $script: Promise<T> & VueScriptInstance<T> } {}

参数

UseScriptInput

这是脚本标签输入。例如,您可以传递一个 URL 字符串或一个包含脚本标签属性的对象。

export type UseScriptInput = string | {
  src?: string
  async?: boolean
  defer?: boolean
  type?: string
  integrity?: string
  crossorigin?: string
  text?: string
  innerHTML?: string
  innerText?: string
  content?: string
  referrerPolicy?: string
  attributes?: Record<string, string>
}

有关选项的更多信息,请参阅 脚本输入 文档。

NuxtUseScriptOptions

有关选项的更多信息,请参阅 脚本选项 文档。

export type NuxtUseScriptOptions<T = any> = Omit<UseScriptOptions<T>, 'trigger'> & {
  /**
   * The trigger to load the script:
   * - `onNuxtReady` - Load the script when Nuxt is ready.
   * - `manual` - Load the script manually by calling `load()`
   * - `Promise` - Load the script when the promise resolves.
   */
  trigger?: UseScriptOptions<T>['trigger'] | 'onNuxtReady'
  /**
   * Should the script be bundled as an asset and loaded from your server. This is useful for improving the
   * performance by avoiding the extra DNS lookup and reducing the number of requests. It also
   * improves privacy by not sharing the user's IP address with third-party servers.
   * - `true` - Bundle the script as an asset.
   * - `false` - Do not bundle the script. (default)
   */
  bundle?: boolean
  /**
   * Skip any schema validation for the script input. This is useful for loading the script stubs for development without
   * loading the actual script and not getting warnings.
   */
  skipValidation?: boolean
}

返回值

有关返回值的更多信息,请参阅 了解代理函数$script 文档。