使用 CDN

使用 Mock Service Worker 的最佳方法是从 NPM 注册表中安装它。我们强烈建议你在采用其他安装方法之前先遵循入门教程:

¥The best way to use Mock Service Worker is by installing it from the NPM registry. We highly encourage you to follow the Getting Started tutorial first before resorting to alternative installation methods:

Getting started

Three steps to get started with Mock Service Worker.

但是,如果你不能使用 NPM,还有其他方法可以安装库。

¥There are, however, other ways to install the library if you can’t use NPM.

CDN

你可以使用 CDN 将 MSW 添加到你的应用中。为此,请在你的页面上包含一个脚本标记,该标记会加载库的 IIFE 构建目标 (lib/iife/index.js)。

¥You can add MSW to your application using a CDN. To do that, include a script tag that loads the IIFE build target (lib/iife/index.js) of the library on your page.

下面,你可以找到一些如何使用流行的 CDN 提供商包含 Mock Service Worker 的示例。

¥Below, you can find some examples of how to include Mock Service Worker using popular CDN providers.

unpkg

<script src="https://unpkg.com/msw/lib/iife/index.js"></script>

jsDelivr

<script src="https://cdn.jsdelivr.net/npm/msw/lib/iife/index.js"></script>