熊浩宇
发布于 2023-11-30 / 0 阅读 / 0 评论 / 0 点赞

js动态判断是否需要导入模块

js动态判断是否需要导入模块

// 判断是否为ie浏览器来导入 docx-preview中的renderAsync 
import('docx-preview').then(({ renderAsync }) => {
	if (isIeOrEdge()) {
		return renderAsync
	} else {
		return null
	}
})

// 判断是否为ie浏览器
function isIeOrEdge() {
		const ua = window.navigator.userAgent
		const msie = ua.indexOf('MSIE ')

		if (msie > 0) {
			// IE 10及以下
			return parseInt(ua.substring(msie + 5, ua.indexOf('.', msie)), 10)
		}

		const trident = ua.indexOf('Trident/')
		if (trident > 0) {
			// IE 11
			const rv = ua.indexOf('rv:')
			return parseInt(ua.substring(rv + 3, ua.indexOf('.', rv)), 10)
		}

		const edge = ua.indexOf('Edge/')
		if (edge > 0) {
			// Edge (IE 12+)
			return parseInt(ua.substring(edge + 5, ua.indexOf('.', edge)), 10)
		}

		// 其他浏览器类型
		return false
	}


评论