API 浏览器
应用扩展提示 API

本页引用 src/prompts.js 文件,该文件处理安装应用扩展时的提示。并非所有应用扩展都需要提示 - 这是一个可选步骤。

¥This page refers to src/prompts.js file which handles the prompts when installing the App Extension. Not all App Extensions will need prompts – this is an optional step.

用户的答案存储在 /quasar.extensions.json(项目文件夹的根目录)中,除非你真正了解自己在做什么,否则不应对其进行篡改。

¥The user’s answers are stored into /quasar.extensions.json (root of project folder), which should not be tampered with unless you really know what you are doing.

文件基本结构示例:

¥Example of basic structure of the file:

// For @quasar/app-vite 1.6+ and @quasar/app-webpack v3.11+
//   1. It can be async
//   2. It receives the "api" param
export default function (api) {
  return [
    // questions
  ]
}

你将可以在 安装索引卸载 中访问 api.prompts(其中包含你的应用扩展程序的答案)。

¥You will have access to api.prompts (which holds your App Extension’s answers) in Install, Index and Uninstall.

现在让我们关注返回的数组的结构,该数组定义了问题。以下部分提供了最常用问题类型的示例。

¥Let’s now focus on the structure of the returned Array which defines the questions. The sections below offer examples for the most used types of questions.

问题格式(Questions format)

¥Questions format

警告

以下并非可能的问题类型的详尽列表,也绝不是描述可用 API 的完整列表。查看 Inquirer.js 了解更多信息(Quasar CLI 在后台使用它)。

¥The following is not an exhaustive list of possible types of questions and by no means it describes the full API available. Check out Inquirer.js for that (which is used by Quasar CLI under the hood).

字符串(String)

¥String

{
  // "description" will be the variable
  // storing the answer
  name: 'description'
  type: 'input',
  required: false, // optional
  message: 'Project description',
  default: 'A Quasar Framework app', // optional
}
{
  name: 'source_build',
  type: 'input',
  required: true, // optional
  message:
    'If you want a separate file to be the source image during production, please specify it here: ',
  validate: (input) => {
    // ...do something ...
  },
  default: (answers) => {
    return answers.source_dev || defaultImg
  }
}

确认(Confirm)

¥Confirm

{
  // "featureX" will be the variable
  // storing the answer
  name: 'featureX',
  type: 'confirm',
  message: 'Use Feature X?',
  default: true // optional
}

选项列表(List of choices)

¥List of choices

{
  // "iconSet" will be the variable
  // storing the answer
  name: 'iconSet',
  type: 'list',
  message: 'Choose Icon Set',
  choices: [
    {
      name: 'Material Icons (recommended)',
      value: 'material-icons', // value of the answer variable
      short: 'Material Icons' // Short name displayed after user picks this
    },
    {
      name: 'Fontawesome v6',
      value: 'fontawesome-v6', // value of the answer variable
      short: 'Fontawesome v6' // Short name displayed after user picks this
    }
    // ... all other choices
  ]
}

API 参数
@quasar/app-vite 1.6+
@quasar/app-webpack 3.11+
(The API param
@quasar/app-vite 1.6+
@quasar/app-webpack 3.11+
)

¥The API param

@quasar/app-vite 1.6+
@quasar/app-webpack 3.11+

COMPATIBILITY WARNING

api 参数已在 @quasar/app-vite v1.6+ 和 @quasar/app-webpack v3.11+ 版本中引入。

¥The api param has been introduced in @quasar/app-vite v1.6+ and @quasar/app-webpack v3.11+.

这些 CLI 的旧版本不提供任何参数。

¥Older versions of these CLIs will not supply any param.

api.engine(api.engine)

包含正在使用的 Quasar CLI 引擎(以字符串形式)。示例:@quasar/app-vite@quasar/app-webpack

¥Contains the Quasar CLI engine (as String) being used. Examples: @quasar/app-vite or @quasar/app-webpack.

api.hasVite(api.hasVite)

布尔值 - 是否在 @quasar/app-vite 上运行。

¥Boolean - is running on @quasar/app-vite or not.

api.hasWebpack(api.hasWebpack)

布尔值 - 是否在 @quasar/app-webpack 上运行。

¥Boolean - is running on @quasar/app-webpack or not.

api.extId(api.extId)

包含此应用扩展的 ext-id(字符串)。

¥Contains the ext-id (String) of this App Extension.

api.resolve(api.resolve)

解析此应用扩展程序正在运行的应用内的路径。无需导入 path 并自行解析路径。

¥Resolves paths within the app on which this App Extension is running. Eliminates the need to import path and resolve the paths yourself.

// resolves to root of app
api.resolve.app('src/my-file.js')

// resolves to root/src of app
api.resolve.src('my-file.js')

// resolves to root/public of app
// (@quasar/app-webpack v3.4+ or @quasar/app-vite v1+)
api.resolve.public('my-image.png')

// resolves to root/src-pwa of app
api.resolve.pwa('some-file.js')

// resolves to root/src-ssr of app
api.resolve.ssr('some-file.js')

// resolves to root/src-cordova of app
api.resolve.cordova('config.xml')

// resolves to root/src-electron of app
api.resolve.electron('some-file.js')

// resolves to root/src-bex of app
api.resolve.bex('some-file.js')

api.appDir(api.appDir)

包含运行此应用扩展程序的应用根目录的完整路径(字符串)。

¥Contains the full path (String) to the root of the app on which this App Extension is running.

api.hasTypescript(api.hasTypescript)

/**

 * @return {Promise<boolean>} host project has Typescript active or not
 */
await api.hasTypescript()

api.hasLint(api.hasLint)

/**

 * @return {Promise<boolean>} host project has ESLint or not
 */
await api.hasLint()

api.getStorePackageName(api.getStorePackageName)

/**

 * @return {Promise<string|undefined>} 'pinia' | 'vuex' | undefined
 */
await api.getStorePackageName()

api.getNodePackagerName(api.getNodePackagerName)

/**

 * @return {Promise<'npm' | 'yarn' | 'pnpm' | 'bun'>}
 */
await api.getNodePackagerName()

api.compatibleWith(api.compatibleWith)

通过语义版本条件确保应用扩展与主机应用中安装的软件包兼容。

¥Ensure the App Extension is compatible with a package installed in the host app through a semver condition.

如果不满足语义版本条件,Quasar CLI 将报错并停止执行。

¥If the semver condition is not met, then Quasar CLI errors out and halts execution.

语义版本条件示例:'1.x || >=2.5.0 || 5.0.0 - 7.2.3'

¥Example of semver condition: '1.x || >=2.5.0 || 5.0.0 - 7.2.3'.

/**

 * @param {string} packageName

 * @param {string} semverCondition
 */
api.compatibleWith(packageName, '1.x')
A more complex example:

if (api.hasVite === true) {
  api.compatibleWith('@quasar/app-vite', '^2.0.0')
}
else {
  api.compatbileWith('@quasar/app-webpack', '^4.0.0')
}

api.hasPackage(api.hasPackage)

通过语义版本条件确定主机应用中是否安装了某些软件包。

¥Determine if some package is installed in the host app through a semver condition.

语义版本条件示例:'1.x || >=2.5.0 || 5.0.0 - 7.2.3'

¥Example of semver condition: '1.x || >=2.5.0 || 5.0.0 - 7.2.3'.

/**

 * @param {string} packageName

 * @param {string} (optional) semverCondition

 * @return {boolean} package is installed and meets optional semver condition
 */
if (api.hasPackage('vuelidate')) {
  // hey, this app has it (any version of it)
}
if (api.hasPackage('quasar', '^2.0.0')) {
  // hey, this app has Quasar UI v2 installed
}

api.hasExtension(api.hasExtension)

检查是否已通过 npm 安装其他应用扩展,并且 Quasar CLI 是否已调用它。

¥Check if another app extension is npm installed and Quasar CLI has invoked it.

/**

 * Check if another app extension is installed

 *  * @param {string} extId

 * @return {boolean} has the extension installed & invoked
 */
if (api.hasExtension(extId)) {
  // hey, we have it
}

api.getPackageVersion(api.getPackageVersion)

获取宿主应用包的版本。

¥Get the version of a host app package.

/**

 * @param {string} packageName

 * @return {string|undefined} version of app's package
 */
console.log( api.getPackageVersion(packageName) )
// output examples:
//   1.1.3
//   undefined (when package not found)