stop()

停止当前客户端的请求拦截。

调用签名

¥Call signature

worker.stop() 函数不接受任何参数,也不返回任何内容。

¥The worker.stop() function does not accept any arguments and doesn’t return anything.

worker.stop()

尽管它与 worker.start() 在逻辑上相反,但 worker.stop() 方法不会取消注册 Worker。相反,它指示工作程序禁用当前客户端(页面)的 API 模拟。这样,你可以拥有多个具有不同请求拦截状态的开放客户端。

¥Although it’s a logical opposite to worker.start(), the worker.stop() method does not unregister the worker. Instead, it instructs the worker to disable API mocking for the current client (page). This way you can have multiple open clients with the different state of the request interception.

此方法旨在在运行时调用以控制请求拦截流。你可以通过全局公开 worker 引用并在浏览器中随时调用 window.worker.stop() 来实现。

¥This method is designed to be called on runtime to control the request interception flow. You do so by exposing the worker reference globally and calling window.worker.stop() in any time in the browser.

// mocks/browser.js
import { setupWorker } from 'msw/browser'
import { handlers } from './handlers'
 
export const worker = setupWorker(...handlers)
 
// Expose the worker instance globally.
window.worker = worker