人力资源管理后台 === 首页+部署

目录

1.首页-echarts图表的应用

2.首页-echarts图表的按需导入

3.路由模式-将路由改成history模式

[4. 打包分析-分析](#4. 打包分析-分析)

5.CDN加速

6.项目打包-安装nginx

7.mac/windows环境下nginx部署启动项目

8.nginx解决history的404问题

9.nginx配置代理解决生产环境跨域问题


1.首页-echarts图表的应用

  • 安装echarts包

    npm i echarts yarn add echarts

  • 放置两个图表的div,并给定高宽,代码位置(src/views/dashboard/index.vue)

  • 在mounted中初始化图表-代码位置(src/views/dashboard/index.vue)

这里为什么要用watch,因为获取数据在created,初始化图表在mounted,执行mouted时,数据并不能保证能够获取到,所以采用获取watch监听数据变化,只要数据变化,就设置图表的options
为什么 this.social和this.provident 并没有在data中声明,注意,在data中声明的表示它是响应式数据,即它的变化要引起template模板的刷新,但是这里我们只是记录一下当前图表的实例,实例本身会有setOption来影响图表的动态渲染,所以这里并没有必要在data中声明这两个变量

2.首页-echarts图表的按需导入

  • echarts图表的按需导入-代码位置(src/views/dashboard/index.vue)

    import * as echarts from 'echarts/core' // 引入核心包
    import { LineChart } from 'echarts/charts' // 引入折线图
    import { GridComponent } from 'echarts/components' // 引入组件
    import { CanvasRenderer } from 'echarts/renderers'
    echarts.use([
    LineChart,
    GridComponent,
    CanvasRenderer
    ])

3.路由模式-将路由改成history模式

  • hash模式带#,#后面的地址变化不会引起页面的刷新

  • history没有#,地址变化会引起页面刷新,更符合页面地址的规范(开发环境不刷新-webpack配置)

  • 将路由模式修改成history模式-代码位置(src/router/index.js)

    const createRouter = () => new Router({
    mode: 'history', // require service support
    scrollBehavior: () => ({ y: 0 }),
    routes: constantRoutes // 默认引入静态路由
    })

4. 打包分析-分析

  • 打包分析代码

    $ npm run preview -- --report

  • 去除main.js中对于mock.js的引用

5.CDN加速

将几个比较大的多在打包时排除,这样可以缩小整体打包的大小,保证js的加载速度,排除的包采用cdn的方式用外链去引入,cdn本名为分发服务器,意为更近的访问区间更快的访问速度将所需要的文件返回给客户端

  • webpack排除打包-代码位置(vue.config.js)

    configureWebpack: {
    // provide the app's title in webpack's name field, so that
    // it can be accessed in index.html to inject the correct title.
    name: name,
    resolve: {
    alias: {
    '@': resolve('src')
    }
    },
    // 配置需要排出的包
    externals: {
    'vue': 'Vue',
    'element-ui': 'ELEMENT',
    'cos-js-sdk-v5': 'COS'
    }
    },

  • 在html中采用外链引入排除的文件-代码位置(public/index.html)

    <%= webpackConfig.name %>
    复制代码
    </body>

6.项目打包-安装nginx

  • 执行打包命令

    npm run build:prod yarn build:prod

得到dist文件包

  • 安装nginx-mac

    $ brew install nginx # mac安装nginx

  • 查看版本-mac

    $ nginx -v # 查看版本

  • 查看nginx-mac

    $ brew info nginx #查看nginx

  • nginx-windows版本

直接解压就可以直接使用

注意:mac安装可能遇到的问题

遇到某个包发生错误,直接使用brew安装这个包,再安装nginx

复制代码
$ brew install pcre2  # 安装出错的包
$ brew install nginx  # 安装nginx

遇到这个错误,可以直接执行该命令,安装对应的工具,再安装nginx

复制代码
$  xcode-select --install  # 安装对应工具
$  brew install nginx  # 安装nginx

7.mac/windows环境下nginx部署启动项目

  • mac查看nginx的相关目录

    brew info nginx #查看nginx

mac-nginx安装目录-/opt/homebrew/Cellar/nginx/1.23.3

mac-配置文件路-/opt/homebrew/etc/nginx/nginx.conf

  • 将打包的文件放置到安装目录/html下
  • mac-启动服务命令

    $ /opt/homebrew/Cellar/nginx/1.23.3/bin/nginx #启动命令

  • mac-重启服务

    $ /opt/homebrew/Cellar/nginx/1.23.3/bin/nginx -s stop #停止命令

注意: mac版本的nginx的默认端口为8080

  • windows版本启动服务

  • windows下停止服务

    $ ./nginx -s stop #停止命令

注意: nginx默认的访问端口为80

8.nginx解决history的404问题

  • 修改mac-windows配置文件

    location / {
    try_files uri uri/ /index.html;
    }

设置不论请求什么地址,都返回index.html

windows配置文件

  • mac重启服务

    $ /opt/homebrew/Cellar/nginx/1.23.3/bin/nginx -s reload #重启

  • windows重启服务

    $ ./nginx -s reload #重启

9.nginx配置代理解决生产环境跨域问题

  • nginx解决生产环境跨域
  • 修改配置文件

    location /prod-api {
    proxy_pass https://heimahr-t.itheima.net;
    }

  • mac重启服务

    $ /opt/homebrew/Cellar/nginx/1.23.3/bin/nginx -s reload #重启

  • windows重启服务

    $ ./nginx -s reload #重启

相关推荐
掘金安东尼几秒前
人工智能AI
前端
kyriewen7 分钟前
AI 面试官筛前端简历,先出局的不是写代码慢的
前端·面试·程序员
BigTopOne36 分钟前
10 引用与借用:& 和 &mut
前端
粥里有勺糖38 分钟前
视野修炼-技术周刊第139期 | AI 不会让时间变多
前端·github·ai编程
ZJPRENO2 小时前
WorkBuddy 从零到一|混元4 Flash 实战手记-提醒小工具开发
前端
用户302822530682 小时前
AI 视频图片运镜为什么会抖?帧率拉高也救不了多次取整
前端·人工智能·typescript
前端码农人生2 小时前
前端转 Agent:真正要重写的不是接口层,是交互层
前端
孟陬3 小时前
Foobar 技术周刊 #003:黄仁勋的三段吃瓜和回归,抛开事实不谈(嘿嘿)让全人类 AGI 进程得以继续
前端·后端·程序员
涛涛ing3 小时前
别再用 AI 写计算逻辑了:你的页面卡死,连鼠标都点不动
前端
OpenTiny社区4 小时前
当 AI 开始写 UI,我们缺的不是模型,是"设计词汇"
前端·github·ai编程