// 封装异步加载资源的方法 /** * 加载外部资源 * @param url 地址 例如 https://xx.com/xx.js * @param type js 或 css * @returns {Promise} */ export function loadExternalResource (url, type) { return new Promise((resolve, reject) => { let tag if (type === 'css') { tag = document.createElement('link') tag.rel = 'stylesheet' tag.href = url } else if (type === 'js') { tag = document.createElement('script') tag.src = url } if (tag) { tag.onload = () => resolve(url) tag.onerror = () => reject(url) document.head.appendChild(tag) } }) } /** * 查询url中的query参数 * @param {}} variable * @returns */ export function getQueryVariable (variable) { const query = window.location.search.substring(1) const vars = query.split('&') for (let i = 0; i < vars.length; i++) { const pair = vars[i].split('=') if (pair[0] === variable) { return pair[1] } } return (false) } /** * 深度合并两个对象 * @param target * @param sources */ export function mergeDeep (target, ...sources) { if (!sources.length) return target const source = sources.shift() if (isObject(target) && isObject(source)) { for (const key in source) { if (isObject(source[key])) { if (!target[key]) Object.assign(target, { [key]: {} }) mergeDeep(target[key], source[key]) } else { Object.assign(target, { [key]: source[key] }) } } } return mergeDeep(target, ...sources) } /** * 对象检查 * @param item * @returns {boolean} */ export function isObject (item) { return (item && typeof item === 'object' && !Array.isArray(item)) }