Skip to page content

其他工具

TIP

关于 UMD 版本的用法请看这里

打开 URL

import { openURL } from 'quasar'

openURL('http://...')

// 完整的语法
openURL(
  String url,
  Function rejectFn, // 可选参数;如果窗口未能打开时会调用的函数
  Object windowFeatures // 可选参数;为新窗口请求的特性
)

它将处理在 Cordova、 Electron 或浏览器上运行时出现的问题,包括通知用户确认弹出窗口。

当在 Cordova (或 Capacitor) 中使用时,最好 (但不是必须的)也安装 InAppBrowser Cordova 插件,这样 openURL 就可以连接到这个插件。

如果在 IOS 上运行,并且安装了 cordova-plugin-safariviewcontroller 插件,那么 openURL 首先会连接到它。

其中可选参 windowFeatures 应该是一个对象,它的字段是 window.open() windowFeatures 中的字段加上一个布尔值(见下方介绍)。 请注意,当 openURL 不使用 window.open() 时,这些特性将不会被考虑在内。

// 一个使用了 windowFeatures 的 openURL 示例:

openURL(
  "http://...",
  undefined, // 在下面的示例中,我们不关心 rejectFn()

  // 这里是 windowFeatures 中的对象参数:
  {
    noopener: true, // 出于安全的原因,默认开启
    // 如果您想禁用它将其设置为 false
    menubar: true,
    toolbar: true,
    noreferrer: true,
    // .....其他的 window features
  },
);
TIP

如果您想在 Cordova 应用中打开拨号应用,不要使用 openURL(),直接使用一个 <a href="tel:123456789"> 标签或者 <QBtn href="tel:123456789">

复制到剪切板

下面是一个复制一些文本内容到剪切板的工具,这个函数返回一个 Promise。

import { copyToClipboard } from "quasar";

copyToClipboard("some text")
  .then(() => {
    // 成功!
  })
  .catch(() => {
    // 失败
  });

导出文件

下面是一个触发浏览器下载指定内容文件的工具。

/**
 * 强制浏览器下载指定内容的文件
 *
 * @param {*} fileName - String
 * @param {*} rawData - String | ArrayBuffer | ArrayBufferView | Blob
 * @param {*} opts - String (mimeType) or Object
 *                   Object form: { mimeType?: String, byteOrderMark?: String | Uint8Array, encoding?: String }
 * @returns Boolean | Error
 */

其中的可选参数 opts 可以是一个字符串(mimeType)或者一个以下格式的对象:

示例:

import { exportFile } from "quasar";

const status = exportFile("important.txt", "some content");

if (status === true) {
  // 浏览器允许
} else {
  // 浏览器不允许
  console.log("Error: " + status);
}
import { exportFile } from "quasar";

const status = exportFile("file.csv", "éà; ça; 12\nà@€; çï; 13", {
  encoding: "windows-1252",
  mimeType: "text/csv;charset=windows-1252;",
});

if (status === true) {
  // 浏览器允许
} else {
  // 浏览器不允许
  console.error("Error: " + status);
}

runSequentialPromises
v2.8.4+

下面是一个可以顺序运行多个 Promises 的工具。可选,使用多个线程。

/**
 * 按顺序运行一组 Promise,可选择使用多个线程
 *
 * @param {*} sequentialPromises - 一个函数数组或者一个对象,对象的值都是函数
 *                          函数数组格式: [ (resultAggregator: Array) => Promise<any>, ... ]
 *                          对象格式: { [key: string]: (resultAggregator: object) => Promise<any>, ... }
 * @param {*} opts - 可选对象参数
 *                   对象格式:{ threadsNumber?: number, abortOnFail?: boolean }
 *                   默认值:{ threadsNumber: 1, abortOnFail: true }
 *                   threadsNumber 必须是正整数;无效值会回退为 1
 *                   当配置了 threadsNumber 并使用了 http 请求,
 *                   此时最大可用线程数量取决于浏览器支持(通常为 5);
 *                   上面的任何数字都不会产生额外增益
 * @returns Promise<Array<Object> | Object>
 *    With opts.abortOnFail set to true (which is default):
 *        When sequentialPromises param is Array:
 *          The Promise resolves with an Array of Objects of the following form:
 *             [ { key: number, status: 'fulfilled', value: any }, ... ]
 *          The Promise rejects with an Object of the following form:
 *             { key: number, status: 'rejected', reason: Error, resultAggregator: array }
 *        When sequentialPromises param is Object:
 *          The Promise resolves with an Object of the following form:
 *             { [key: string]: { key: string, status: 'fulfilled', value: any }, ... }
 *          The Promise rejects with an Object of the following form:
 *             { key: string, status: 'rejected', reason: Error, resultAggregator: object }
 *    With opts.abortOnFail set to false:
 *       The Promise is never rejected (no catch() needed)
 *       The Promise resolves with:
 *          An Array of Objects (when sequentialPromises param is also an Array) of the following form:
 *             [ { key: number, status: 'fulfilled', value: any } | { status: 'rejected', reason: Error }, ... ]
 *          An Object (when sequentialPromises param is also an Object) of the following form:
 *             { [key: string]: { key: string, status: 'fulfilled', value: any } | { key: string, status: 'rejected', reason: Error }, ... }
 */

注意:

  • sequentialPromises 参数可以是一个函数数组(每个函数都返回一个 Promise)
  • sequentialPromises 中的每个函数都可以接受一个 resultAggregator 参数,所以,您可以使用前面的 Promise 的结果来决定如何处理当前的 Promise;resultAggregator 中尚未完成的项目都会被标记为 null。
  • opts 参数是可选的。

通用示例(使用数组作为 sequentialPromises 参数):

import { runSequentialPromises } from "quasar";

runSequentialPromises([
  (resultAggregator) =>
    new Promise((resolve, reject) => {
      /* do some work... */
    }),
  (resultAggregator) =>
    new Promise((resolve, reject) => {
      /* do some work... */
    }),
  // ...
])
  .then((resultAggregator) => {
    // resultAggregator 的顺序与上方的 promises 相同
    console.log("第一个 Promise 的结果:", resultAggregator[0].value);
    console.log("第二个 Promise 的结果:", resultAggregator[1].value);
    // ...
  })
  .catch((errResult) => {
    console.error(`作业中遇到的错误 #${errResult.key}:`);
    console.error(errResult.reason);
    console.log("获取此错误发生之前的结果:");
    console.log(errResult.resultAggregator);
  });

通用示例(使用对象作为 sequentialPromises 参数):

import { runSequentialPromises } from "quasar";

runSequentialPromises({
  phones: (resultAggregator) =>
    new Promise((resolve, reject) => {
      /* do some work... */
    }),
  laptops: (resultAggregator) =>
    new Promise((resolve, reject) => {
      /* do some work... */
    }),
  // ...
})
  .then((resultAggregator) => {
    console.log("第一个 Promise 的结果:", resultAggregator.phones.value);
    console.log("第二个 Promise 的结果:", resultAggregator.laptops.value);
    // ...
  })
  .catch((errResult) => {
    console.error(`作业中遇到的错误 (${errResult.key}):`);
    console.error(errResult.reason);
    console.log("获取此错误发生之前的结果:");
    console.log(errResult.resultAggregator);
  });

使用上一个结果的示例:

import { runSequentialPromises } from "quasar";

runSequentialPromises({
  phones: () =>
    new Promise((resolve, reject) => {
      /* do some work... */
    }),
  vendors: (resultAggregator) => {
    new Promise((resolve, reject) => {
      // 您可以在这里使用 resultAggregator.phones.value 做一些事情...
      // 由于默认设置了 abortOnFail,所以结果肯定是存在的
      // 所以您不需要考虑 resultAggregator.phones 为 "null" 的情况。
    });
  },
  // ...
});

使用 Fetch API 的示例:

import { runSequentialPromises } from "quasar";

const keyList = ["users", "phones", "laptops"];

runSequentialPromises([
  () => fetch("https://some-url.com/users").then((res) => res.json()),
  () =>
    fetch("https://some-other-url.com/items/phones").then((res) => res.json()),
  () =>
    fetch("https://some-other-url.com/items/laptops").then((res) => res.json()),
])
  .then((resultAggregator) => {
    // resultAggregator 的顺序与上方的 promises 相同
    resultAggregator.forEach((result) => {
      console.log(keyList[result.key], result.value); // example: users {...}
    });
  })
  .catch((errResult) => {
    console.error(`请求时发生了错误 ${keyList[errResult.key]}:`);
    console.error(errResult.reason);
    console.log("获取此错误发生之前的结果:");
    console.log(errResult.resultAggregator);
  });

// **等价于** 使用对象作为 sequentialPromises 参数:

runSequentialPromises({
  users: () => fetch("https://some-url.com/users").then((res) => res.json()),
  phones: () =>
    fetch("https://some-other-url.com/items/phones").then((res) => res.json()),
  laptops: () =>
    fetch("https://some-other-url.com/items/laptops").then((res) => res.json()),
})
  .then((resultAggregator) => {
    console.log("users:", resultAggregator.users.value);
    console.log("phones:", resultAggregator.phones.value);
    console.log("laptops:", resultAggregator.laptops.value);
  })
  .catch((errResult) => {
    console.error(`请求时发生了错误 ${errResult.key}:`);
    console.error(errResult.reason);
    console.log("获取此错误发生之前的结果:");
    console.log(errResult.resultAggregator);
  });

abortOnFail 设置为 false 的示例:

import { runSequentialPromises } from "quasar";

// 注意,没有 "catch()",runSequentialPromises() 总是会 resolve
runSequentialPromises(
  {
    users: () => fetch("https://some-url.com/users").then((res) => res.json()),
    phones: () =>
      fetch("https://some-other-url.com/items/phones").then((res) =>
        res.json(),
      ),
    laptops: () =>
      fetch("https://some-other-url.com/items/laptops").then((res) =>
        res.json(),
      ),
  },
  { abortOnFail: false },
).then((resultAggregator) => {
  Object.values(resultAggregator).forEach((result) => {
    if (result.status === "rejected") {
      console.log(`请求失败${result.key}:`, result.reason);
    } else {
      console.log(`请求成功 ${result.key}:`, result.value);
    }
  });
});

当配置了线程数量 (opts > threadsNumber) 并使用了 http 请求,此时最大可用线程数量取决于浏览器支持(通常为 5);上面的任何数字都不会产生额外增益。

import { runSequentialPromises } from "quasar";

runSequentialPromises([/* ... */], { threadsNumber: 3 })
  .then((resultAggregator) => {
    resultAggregator.forEach((result) => {
      console.log(result.value);
    });
  })
  .catch((errResult) => {
    console.error(`发生错误:`);
    console.error(errResult.reason);
    console.log("获取此错误发生之前的结果:");
    console.log(errResult.resultAggregator);
  });

防抖

如果您的应用程序使用 JavaScript 来完成繁重的任务,那么防抖 (debounce) 函数是必不可少的,它可以确保给定的任务不会频繁地触发,从而影响浏览器性能。防抖函数会限制函数的触发速率。

防抖功能强制一个函数在经过一定时间后才再次被调用。例如: “执行一个函数前,确保此函数在过去的 100 毫秒内没有被调用过才会执行它”。

当 immediate 为 true 时,等待周期在回调运行之前就开始计时,因此从该回调内部发起的调用同样会被防抖。

防抖函数的签名如下:

function debounce<F extends (...args: any[]) => any>(
  fn: F,
  wait?: number, // 默认值:250
  options?:
    // 默认值:false
    // 传入 true 等价于 { leading: true, trailing: false }
    | boolean
    // v2.34+
    | {
        leading?: boolean // 默认值:false
        trailing?: boolean // 默认值:true
        maxWait?: number // 默认值:无
      }
): ((this: ThisParameterType<F>, ...args: Parameters<F>) => void) & {
  cancel(): void
  flush(): void // v2.34+
  readonly isPending: boolean // v2.34+
}

默认情况下,防抖函数会在「后沿」触发 fn:当调用停止了 wait 毫秒后,用最后一次调用的参数触发。options 参数(自 v2.34 起为对象)用于调整这一行为:

  • leading 会在一组连续调用的第一次调用时也触发 fn,用该次调用的参数。此时只有在首次调用之后仍有调用发生,才会再触发后沿的那一次。
  • trailing 设为 false 则丢弃后沿的那次触发。
  • maxWait 保证在持续调用期间至少每 maxWait 毫秒触发一次 fn(用最新的参数),这样即使一组永不停止的连续调用也能得到响应。它需要有后沿触发才生效。一次 maxWait 触发会结束当前等待周期:下一次调用将开启属于它自己的新一轮。

传入 true 而不是对象,是 { leading: true, trailing: false } 的简写:fn 只在第一次调用时触发,接下来 wait 毫秒内的调用都会被吞掉(每次都会重启等待)。注意 { leading: true } 单独使用时仍保留后沿触发。

等待周期在 fn 运行之前就开始计时,因此从 fn 内部发起的调用同样会被防抖。

一个简单的示例:您有一个监听窗口大小变化的侦听器,他会重新计算一些元素的大小和位置。这本身并不是一个繁重的任务,但是在无数次调整大小之后重复触发您的侦听器会明显降低您的应用速度。那么为什么不限制函数的触发速率呢?

import { debounce } from 'quasar'

// 示例:
window.addEventListener(
  'resize',
  debounce(function() {
    // .... things to do ...
  }, 300 /*等待的毫秒数*/)
)

防抖后的函数带有两个方法和一个标志位:cancel() 丢弃正在等待的调用,flush() 立即运行它,isPending 在有调用正在等待时为 true(请当作只读)。如果没有后沿触发(传入 true,或者 leading 配合 trailing: false),则不会有任何调用处于等待:isPending 保持 false,flush() 什么也不做,cancel() 结束等待周期,使下一次调用立即运行。两边沿都没有(单独传 { trailing: false })时,fn 永远不会运行。

const search = debounce(query => {
  // ...
}, 300)

search('qua')
search('quasar')
search.isPending // v2.34+;此时为 true
search.flush() // v2.34+;立即用 'quasar' 运行
search.cancel() // 已经没有等待的调用,空操作

// 最后一次编辑后最多 300ms 保存一次
// 编辑过程中至少每 5s 保存一次
const saveDraft = debounce(save, 300, { maxWait: 5000 })
TIP

在组件内部,建议优先使用 useDebounce 组合式函数:它返回的是同一个防抖函数,但会在组件销毁时丢弃正在等待的调用,并提供一个响应式的 isPending。

Quasar 还提供了一个 frameDebounce 函数,可以延迟到下一个浏览器渲染帧时再调用您的函数(参考 requestAnimationFrame)。它只带有 cancel():下一帧距离现在从来不会超过几毫秒,所以没有什么需要 flush 的。

function frameDebounce<F extends (...args: any[]) => any>(
  fn: F
): ((this: ThisParameterType<F>, ...args: Parameters<F>) => void) & {
  cancel(): void
}

import { frameDebounce } from 'quasar'
window.addEventListener(
  'resize',
  frameDebounce(function () {
    /* ... */
  })
)
TIP

在组件内部,建议优先使用 useAnimationFrame 组合式函数:它同样会在下一帧安排一个函数执行,并在组件销毁时丢弃尚未执行的那一帧。

节流

节流(throttle)会强制限制一段时间内可以调用函数的最大次数。如:“在 X 毫秒内最多只能执行此函数一次”。

节流函数的签名如下:

function throttle<F extends (...args: any[]) => any>(
  fn: F,
  limit?: number, // 默认值:250
  trailing?: boolean // v2.34+
): F & {
  cancel(): void // v2.34+
  flush(): void // v2.34+
}

import { throttle } from 'quasar'

// 示例:
window.addEventListener(
  'resize',
  throttle(function() {
    // .... things to do ...
  }, 300 /* 每 0.3s 内最多只能执行一次 */)
)

第一次调用会立即运行,接下来 limit 毫秒内的调用都会被丢弃,所以一组连续调用最终停留在其首次调用的状态上。将 trailing 设为 true 后,一个时间窗内发生的最后一次调用会在窗口结束时触发(用它自己的参数),这样最后一次调用的状态总能得到响应;而这一次触发又会开启属于它自己的新窗口。

节流后的函数带有两个方法:cancel() 丢弃正在等待的调用并关闭当前窗口,使下一次调用立即运行;flush() 立即运行正在等待的调用,就好像窗口刚刚结束一样,于是一个新窗口随即开启、速率限制依然成立;若没有等待中的调用则什么也不做。

TIP

在组件内部,建议优先使用 useThrottle 组合式函数:它返回的是同一个节流函数,但会在组件销毁时丢弃正在等待的调用。

或者在一个 .vue 文件中作为方法(vue 中的 methods)调用:

methods: {
  myMethod () { .... }
},

created () {
  this.myMethod = throttle(this.myMethod, 500)
}
WARNING

通过类似 myMethod: throttle(function () { // Code }, 500) 这种方式来给一个方法(vue 中的 methods)添加节流,意味着此组件中所有渲染实例访问此方法时获得的都是被节流处理过的。应该避免使用上述风格的代码。

或者在一个 .vue 文件中作为方法(vue 中的 methods)调用:

methods: {
  myMethod () { .... }
},

created () {
  this.myMethod = throttle(this.myMethod, 500)
}
WARNING

通过类似 myMethod: throttle(function () { // Code }, 500) 这种方式来给一个方法(vue 中的 methods)添加节流,意味着此组件中所有渲染实例访问此方法时获得的都是被节流处理过的。应该避免使用上述风格的代码。

继承 - 对象的深度克隆

jQuery.extend() 的一个基础复刻版本。接收以下参数:

import { extend } from 'quasar'

let newObject = extend([Boolean deepCopy], targetObj, obj, ...)

当心对象中的方法。

uid - 生成 UID

生成唯一标识符:

import { uid } from "quasar";

let uid = uid();
// 示例:501e7ae1-7e6f-b923-3e84-4e946bff31a8

testPattern

针对特定模式进行测试。

import { patterns } from "quasar";

const { testPattern } = patterns;

testPattern.email("foo@bar.com"); // true
testPattern.email("foo"); // false

testPattern.hexColor("#fff"); // true
testPattern.hexColor("#ffffff"); // true
testPattern.hexColor("#FFF"); // true
testPattern.hexColor("#gggggg"); // false

完整的 patterns 列表请参考这里。