wxData.js 660 B

123456789101112131415161718192021222324252627282930
  1. class WxData {
  2. constructor(component) {
  3. this.Component = component
  4. }
  5. getData(key) {
  6. const data = this.Component.data
  7. if (!key) return data
  8. if (key.includes('.')) {
  9. let keys = key.split('.')
  10. const tmp = keys.reduce((prev, next) => {
  11. return prev[next]
  12. }, data)
  13. return tmp
  14. } else {
  15. return this.Component.data[key]
  16. }
  17. }
  18. setData(data) {
  19. return new Promise((resolve, reject) => {
  20. if (!data) return reject('no data to set')
  21. if (typeof data === 'object') {
  22. this.Component.setData(data, () => {
  23. resolve(data)
  24. })
  25. }
  26. })
  27. }
  28. }
  29. export default WxData