webpack异步加载ensure

2024-05-09 16:58
文章标签 加载 异步 webpack ensure

本文主要是介绍webpack异步加载ensure,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

功能:

点击按钮1加载A.js,点击按钮2加载B.js。

目录:

 index.html:

<!DOCTYPE html>
<html lang="en">
<head><meta charset="UTF-8"><meta http-equiv="X-UA-Compatible" content="IE=edge"><meta name="viewport" content="width=device-width, initial-scale=1.0"><title>Document</title>
</head>
<body><div id="app"></div><button id="Btn1">按钮1</button><button id="Btn2">按钮2</button>
</body>
</html>

main.js:

import Vue from 'vue';// 点击按钮1再加载A.js  异步加载
document.getElementById('Btn1').onclick = function () {require.ensure([], function () {var A = require('./A.js');console.log(A.data);});
}// 点击按钮2再加载B.js  异步加载
document.getElementById('Btn2').onclick = function () {require.ensure([], function () {var B = require('./B.js');console.log(B.data);});
}

A.js:

var A = {data: '数据A'
};//Node commonJs模块规范
module.exports = A;

B.js:

var B = {data: '数据B'
};//Node commonJs模块规范
module.exports = B;

webpack.config.js:

const path = require('path');
const webpack = require('webpack');
const packagejson = require('./package.json');
const HtmlWebpackPlugin = require('html-webpack-plugin');module.exports = {entry: {'main': './src/main.js',// dependencies'util': Object.keys(packagejson.dependencies)},output: {path: path.resolve('./dist'),filename: '[name].js'},watch: true,plugins: [new webpack.optimize.CommonsChunkPlugin({// 第三方公共组件name: 'common',filename:'[name].js'}),new HtmlWebpackPlugin({// chunks用于多入口文件,编译后的index.html中有多个script标签引入js,chunks数组中的顺序就是js引入的先后顺序// common要放在前面chunks: ['common', 'util', 'main'],template: './src/index.html',  // 参照物inject: true // 注入js 默认值true   四个值:true false(一般不用) head body})]
}

 package.json:

{"name": "03es6","version": "1.0.0","description": "","main": "index.js","scripts": {"test": "echo \"Error: no test specified\" && exit 1","build": "webpack-dev-server --open --hot --inline","dev": "webpack"},"keywords": [],"author": "","license": "ISC","devDependencies": {"babel-core": "^6.26.3","babel-loader": "^7.1.5","babel-plugin-transform-runtime": "^6.23.0","babel-preset-env": "^1.7.0","css-loader": "^0.28.11","html-webpack-plugin": "^2.24.1","style-loader": "^0.23.1","vue": "^2.6.14","vue-loader": "^14.1.1","vue-template-compiler": "^2.5.17","webpack": "^3.12.0","webpack-dev-server": "^2.10.0"},"dependencies": {"vue": "^2.6.14"}
}

实现效果:

 

这篇关于webpack异步加载ensure的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/973967

相关文章

Spring Boot 中正确地在异步线程中使用 HttpServletRequest的方法

《SpringBoot中正确地在异步线程中使用HttpServletRequest的方法》文章讨论了在SpringBoot中如何在异步线程中正确使用HttpServletRequest的问题,... 目录前言一、问题的来源:为什么异步线程中无法访问 HttpServletRequest?1. 请求上下文与线

在 Spring Boot 中使用异步线程时的 HttpServletRequest 复用问题记录

《在SpringBoot中使用异步线程时的HttpServletRequest复用问题记录》文章讨论了在SpringBoot中使用异步线程时,由于HttpServletRequest复用导致... 目录一、问题描述:异步线程操作导致请求复用时 Cookie 解析失败1. 场景背景2. 问题根源二、问题详细分

Java中将异步调用转为同步的五种实现方法

《Java中将异步调用转为同步的五种实现方法》本文介绍了将异步调用转为同步阻塞模式的五种方法:wait/notify、ReentrantLock+Condition、Future、CountDownL... 目录异步与同步的核心区别方法一:使用wait/notify + synchronized代码示例关键

springboot的调度服务与异步服务使用详解

《springboot的调度服务与异步服务使用详解》本文主要介绍了Java的ScheduledExecutorService接口和SpringBoot中如何使用调度线程池,包括核心参数、创建方式、自定... 目录1.调度服务1.1.JDK之ScheduledExecutorService1.2.spring

spring-boot-starter-thymeleaf加载外部html文件方式

《spring-boot-starter-thymeleaf加载外部html文件方式》本文介绍了在SpringMVC中使用Thymeleaf模板引擎加载外部HTML文件的方法,以及在SpringBoo... 目录1.Thymeleaf介绍2.springboot使用thymeleaf2.1.引入spring

关于Spring @Bean 相同加载顺序不同结果不同的问题记录

《关于Spring@Bean相同加载顺序不同结果不同的问题记录》本文主要探讨了在Spring5.1.3.RELEASE版本下,当有两个全注解类定义相同类型的Bean时,由于加载顺序不同,最终生成的... 目录问题说明测试输出1测试输出2@Bean注解的BeanDefiChina编程nition加入时机总结问题说明

异步线程traceId如何实现传递

《异步线程traceId如何实现传递》文章介绍了如何在异步请求中传递traceId,通过重写ThreadPoolTaskExecutor的方法和实现TaskDecorator接口来增强线程池,确保异步... 目录前言重写ThreadPoolTaskExecutor中方法线程池增强总结前言在日常问题排查中,

微服务架构之使用RabbitMQ进行异步处理方式

《微服务架构之使用RabbitMQ进行异步处理方式》本文介绍了RabbitMQ的基本概念、异步调用处理逻辑、RabbitMQ的基本使用方法以及在SpringBoot项目中使用RabbitMQ解决高并发... 目录一.什么是RabbitMQ?二.异步调用处理逻辑:三.RabbitMQ的基本使用1.安装2.架构

SpringBoot项目启动后自动加载系统配置的多种实现方式

《SpringBoot项目启动后自动加载系统配置的多种实现方式》:本文主要介绍SpringBoot项目启动后自动加载系统配置的多种实现方式,并通过代码示例讲解的非常详细,对大家的学习或工作有一定的... 目录1. 使用 CommandLineRunner实现方式:2. 使用 ApplicationRunne

SpringBoot项目删除Bean或者不加载Bean的问题解决

《SpringBoot项目删除Bean或者不加载Bean的问题解决》文章介绍了在SpringBoot项目中如何使用@ComponentScan注解和自定义过滤器实现不加载某些Bean的方法,本文通过实... 使用@ComponentScan注解中的@ComponentScan.Filter标记不加载。@C