0

我正在编写一个 Ember.js 插件,我需要弃用一个功能。我可以使用与 Ember 相同的弃用系统吗?我希望我的用户能够使警告静音。

理想情况下,我还需要能够从我的测试套件中测试弃用消息是否在正确的条件下工作。

我正在使用 Ember 3.x。

4

1 回答 1

2

通过以下方法,插件可以使用与 Ember 相同的弃用警告样式deprecate

import { deprecate } from '@ember/application/deprecations';

deprecate接受三个参数:作为消息的字符串,一个布尔测试,其中 falsey 表示将显示弃用,以及一个包含更多信息的选项对象。

例如:

import { deprecate } from '@ember/application/deprecations';
// ...
deprecate(
   "message about your addon that mentions the addon by name", 
   false,
   {
      id: 'send-component-hash',
      until: '2.0.0',
      url: 'some url goes here'
    }
);
// ...

控制台中的消息将如下所示:

带有警告图标和说明的黄色警告框

可以使用ember-qunit-assert 之类的库来测试弃用。安装后,如果您已经在测试中使用,expectDeprecation它将可用:assertqunit

assert.expectDeprecation(/expected deprecation message/);
于 2019-06-05T22:50:53.853 回答