关于前端:remoteLoadjs

39次阅读

共计 707 个字符,预计需要花费 2 分钟才能阅读完成。

export default function remoteLoad (url, hasCallback) {return createScript(url)

  function createScript (url) {var scriptElement = document.createElement('script')
    document.head.appendChild(scriptElement)
    var promise = new Promise((resolve, reject) => {
      scriptElement.addEventListener('load', e => {removeScript(scriptElement)
        if (!hasCallback) {resolve(e)
        }
      }, false)

      scriptElement.addEventListener('error', e => {removeScript(scriptElement)
        reject(e)
      }, false)

      if (hasCallback) {window.____callback____ = function () {resolve()
          window.____callback____ = null
        }
      }
    })

    if (hasCallback) {url += '&callback=____callback____'}

    scriptElement.src = url

    return promise
  }

  function removeScript (scriptElement) {document.body.removeChild(scriptElement)
  }
}

参考链接:http://vue-gaode.rxshc.com/

正文完
 0