兼容性

Nuxt Kit 提供了一组实用程序,可帮助你检查模块与不同 Nuxt 版本的兼容性。

Nuxt Kit 实用程序可用于 Nuxt 3、带有 Bridge 的 Nuxt 2,甚至不带 Bridge 的 Nuxt 2。为了确保你的模块与所有版本兼容,你可以使用 checkNuxtCompatibilityassertNuxtCompatibilityhasNuxtCompatibility 函数。它们会检查当前 Nuxt 版本是否满足你提供的约束条件。你还可以使用 isNuxt2isNuxt3getNuxtVersion 函数进行更精细的检查。

¥Nuxt Kit utilities can be used in Nuxt 3, Nuxt 2 with Bridge and even Nuxt 2 without Bridge. To make sure your module is compatible with all versions, you can use the checkNuxtCompatibility, assertNuxtCompatibility and hasNuxtCompatibility functions. They will check if the current Nuxt version meets the constraints you provide. Also you can use isNuxt2, isNuxt3 and getNuxtVersion functions for more granular checks.

checkNuxtCompatibility

检查是否满足当前 Nuxt 版本的约束条件。如果未提供,则返回一个消息数组。Nuxt 2 版本还会检查是否支持 bridge

¥Checks if constraints are met for the current Nuxt version. If not, returns an array of messages. Nuxt 2 version also checks for bridge support.

类型

¥Type

async function checkNuxtCompatibility(
  constraints: NuxtCompatibility,
  nuxt?: Nuxt
): Promise<NuxtCompatibilityIssues>;

interface NuxtCompatibility {
  nuxt?: string;
  bridge?: boolean;
  builder?: {
    // Set `false` if your module is not compatible with a builder
    // or a semver-compatible string version constraint
    vite?: false | string;
    webpack?: false | string;
  };
}

interface NuxtCompatibilityIssue {
  name: string;
  message: string;
}

interface NuxtCompatibilityIssues extends Array<NuxtCompatibilityIssue> {
  toString(): string;
}

参数

¥Parameters

constraints

类型:NuxtCompatibility

¥Type: NuxtCompatibility

默认:{}

¥Default: {}

需要检查的约束。它接受以下属性:

¥Constraints to check for. It accepts the following properties:

  • nuxt(可选)
    类型:string
    Nuxt 版本采用 semver 格式。版本可以用 Node.js 方式定义,例如:>=2.15.0 <3.0.0
  • bridge(可选)
    类型:boolean
    如果设置为 true,它将检查当前 Nuxt 版本是否支持 bridge

nuxt

类型:Nuxt

¥Type: Nuxt

默认:useNuxt()

¥Default: useNuxt()

Nuxt 实例。如果未提供,它将通过 useNuxt() 调用从上下文中检索。

¥Nuxt instance. If not provided, it will be retrieved from the context via useNuxt() call.

assertNuxtCompatibility

断言当前 Nuxt 版本满足约束条件。如果未提供,则抛出一个错误,并将问题列表作为字符串。

¥Asserts that constraints are met for the current Nuxt version. If not, throws an error with the list of issues as string.

类型

¥Type

async function assertNuxtCompatibility(
  constraints: NuxtCompatibility,
  nuxt?: Nuxt
): Promise<true>;

interface NuxtCompatibility {
  nuxt?: string;
  bridge?: boolean;
}

参数

¥Parameters

constraints

类型:NuxtCompatibility

¥Type: NuxtCompatibility

默认:{}

¥Default: {}

需要检查的约束。它接受以下属性:

¥Constraints to check for. It accepts the following properties:

  • nuxt(可选)
    类型:string
    Nuxt 版本采用 semver 格式。版本可以用 Node.js 方式定义,例如:>=2.15.0 <3.0.0
  • bridge(可选)
    类型:boolean
    如果设置为 true,它将检查当前 Nuxt 版本是否支持 bridge

nuxt

类型:Nuxt

¥Type: Nuxt

默认:useNuxt()

¥Default: useNuxt()

Nuxt 实例。如果未提供,它将通过 useNuxt() 调用从上下文中检索。

¥Nuxt instance. If not provided, it will be retrieved from the context via useNuxt() call.

hasNuxtCompatibility

检查是否满足当前 Nuxt 版本的约束条件。如果满足所有约束,则返回 true,否则返回 false。Nuxt 2 版本还会检查是否支持 bridge

¥Checks if constraints are met for the current Nuxt version. Return true if all constraints are met, otherwise returns false. Nuxt 2 version also checks for bridge support.

类型

¥Type

async function hasNuxtCompatibility(
  constraints: NuxtCompatibility,
  nuxt?: Nuxt
): Promise<boolean>;

interface NuxtCompatibility {
  nuxt?: string;
  bridge?: boolean;
}

参数

¥Parameters

constraints

类型:NuxtCompatibility

¥Type: NuxtCompatibility

默认:{}

¥Default: {}

需要检查的约束。它接受以下属性:

¥Constraints to check for. It accepts the following properties:

  • nuxt(可选)
    类型:string
    Nuxt 版本采用 semver 格式。版本可以用 Node.js 方式定义,例如:>=2.15.0 <3.0.0
  • bridge(可选)
    类型:boolean
    如果设置为 true,它将检查当前 Nuxt 版本是否支持 bridge

nuxt

类型:Nuxt

¥Type: Nuxt

默认:useNuxt()

¥Default: useNuxt()

Nuxt 实例。如果未提供,它将通过 useNuxt() 调用从上下文中检索。

¥Nuxt instance. If not provided, it will be retrieved from the context via useNuxt() call.

isNuxt2

检查当前 Nuxt 版本是否为 2.x。

¥Checks if the current Nuxt version is 2.x.

类型

¥Type

function isNuxt2(nuxt?: Nuxt): boolean;

参数

¥Parameters

nuxt

类型:Nuxt

¥Type: Nuxt

默认:useNuxt()

¥Default: useNuxt()

Nuxt 实例。如果未提供,它将通过 useNuxt() 调用从上下文中检索。

¥Nuxt instance. If not provided, it will be retrieved from the context via useNuxt() call.

isNuxt3

检查当前 Nuxt 版本是否为 3.x。

¥Checks if the current Nuxt version is 3.x.

类型

¥Type

function isNuxt3(nuxt?: Nuxt): boolean;

参数

¥Parameters

nuxt

类型:Nuxt

¥Type: Nuxt

默认:useNuxt()

¥Default: useNuxt()

Nuxt 实例。如果未提供,它将通过 useNuxt() 调用从上下文中检索。

¥Nuxt instance. If not provided, it will be retrieved from the context via useNuxt() call.

getNuxtVersion

返回当前的 Nuxt 版本。

¥Returns the current Nuxt version.

类型

¥Type

function getNuxtVersion(nuxt?: Nuxt): string;

参数

¥Parameters

nuxt

类型:Nuxt

¥Type: Nuxt

默认:useNuxt()

¥Default: useNuxt()

Nuxt 实例。如果未提供,它将通过 useNuxt() 调用从上下文中检索。

¥Nuxt instance. If not provided, it will be retrieved from the context via useNuxt() call.