关键词搜索

源码搜索 ×
×

Web前端笔记-使用Webpack调用echarts画图

发布2020-11-19浏览734次

详情内容

有如下几个步骤,在此记录下!

1. 安装npm;

2. 安装cnpm;

3. 初始化webpack项目:

npm init -y

3. 下载依赖:

cnpm i -D webpack webpack-cli

4. 下载echarts依赖:

cnpm i -S echarts

5. 这个时候会出现node_modules的文件夹,新建src目录以及src/index.js,新建/index.html及webpack.config.js

这里将package.json中的:

"test": "echo \"Error: no test specified\" && exit 1"

改为:

"build": "webpack"

完整代码如下:

  1. {
  2. "name": "webpack",
  3. "version": "1.0.0",
  4. "description": "",
  5. "main": "index.js",
  6. "scripts": {
  7. "build": "webpack"
  8. },
  9. "keywords": [],
  10. "author": "",
  11. "license": "ISC",
  12. "devDependencies": {
  13. "webpack": "^5.5.0",
  14. "webpack-cli": "^4.2.0"
  15. },
  16. "dependencies": {
  17. "echarts": "^4.9.0"
  18. }
  19. }

6. 将webpack.config.js配置好,打包的文件改如何输出:

  1. const path = require('path')
  2. module.exports = {
  3. entry: path.resolve(__dirname, './src/index.js'),
  4. output: {
  5. path: path.resolve(__dirname, './dist'),
  6. filename: "index-bundle.js"
  7. }
  8. }

7. 对应的文件如下:

index.js

  1. import EChars from 'echarts'
  2. const charDom = document.getElementById('chart')
  3. const chart = EChars.init(charDom)
  4. chart.setOption({
  5. title: {
  6. text: 'test'
  7. },
  8. xAxis: {
  9. data: ['一个', '两个', '三个', '四个']
  10. },
  11. yAxis: {},
  12. series: {
  13. type: 'bar',
  14. data: [100, 200, 300, 400]
  15. }
  16. })

index.html

  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta charset="utf-8" />
  5. <style>
  6. #chart{
  7. width: 600px;
  8. height: 600px;
  9. }
  10. </style>
  11. </head>
  12. <body>
  13. <div id="chart"></div>
  14. <script src="dist/index-bundle.js"></script>
  15. </body>
  16. </html>

8. 构建项目:

npm run build

这里要注意

这个生成的目录和文件都是和webpack.config.js相关的

比如这个index-bundle.js

 

程序运行截图如下:

源码打包下载地址:

https://github.com/fengfanchen/frontUI/tree/master/webpackEcharts

 

相关技术文章

点击QQ咨询
开通会员
返回顶部
×
微信扫码支付
微信扫码支付
确定支付下载
请使用微信描二维码支付
×

提示信息

×

选择支付方式

  • 微信支付
  • 支付宝付款
确定支付下载