notify.js 747 B

12345678910111213141516171819202122232425
  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.selector;
  18. if (notify) {
  19. notify.set(options);
  20. notify.show();
  21. }
  22. else {
  23. console.warn('未找到 van-notify 节点,请确认 selector 及 context 是否正确');
  24. }
  25. }