notify.js 776B

123456789101112131415161718192021222324252627
  1. import { isObj } from '../common/utils';
  2. const defaultOptions = {
  3. selector: '#van-notify',
  4. duration: 3000
  5. };
  6. function parseOptions(text) {
  7. return isObj(text) ? text : { text };
  8. }
  9. function getContext() {
  10. const pages = getCurrentPages();
  11. return pages[pages.length - 1];
  12. }
  13. export default function Notify(options) {
  14. options = Object.assign({}, defaultOptions, parseOptions(options));
  15. const context = options.context || getContext();
  16. const notify = context.selectComponent(options.selector);
  17. delete options.context;
  18. delete options.selector;
  19. if (notify) {
  20. notify.set(options);
  21. notify.showNotify();
  22. }
  23. else {
  24. console.warn('未找到 van-notify 节点,请确认 selector 及 context 是否正确');
  25. }
  26. }