How to optimize Vue projects
This time I will show you how to optimize the Vue project and what are the precautions for optimizing the Vue project. The following is a practical case, let's take a look.
The project structure of the Vue class project development is basically similar to the Vue-cli generation method. In this method of development, the most commonly used mode is to enable the agent for mock debugging or remote debugging, that is, using Configure the proxyTable set by Vue-cli or directly use the proxy option provided by Webpack-dev-server. It uses the http-proxy library, so the specific configuration can be viewed:
https://github.com/nodejitsu/node-http-proxy#options
Using these configured parameters we You can make more flexible configurations to achieve better results
Usage requirements
Assume that our local development is currently in the following status:
Local development, use local mock Server for data
Permission-related interfaces use local mock data, and all others use a designated remote machine
The permissions-related interface uses local mock data, and other data sub-interfaces use different remote machines
-
All interfaces use the same remote machine
## Solution
Let’s first look at the classic proxyTable writing method:1 2 3 4 5 6 7 8 9 10 |
|
- Local development: The target can point to a certain port of localhost. As for the verification of the host, it is definitely not necessary.
- Part of the local machine, and the other fixed remote machine: localhost and remote addresses need to be configured. Most of the remote addresses need to be verified by the host.
- Same as two, but there are multiple machines: multiple machines need to be manually configured
- The same remote machine, at this time the machine may need to be strictly verified, That is, the IP must also use the domain name, and the system host can be used only after configuring the system host.
cannot directly modify the host implementation of the requested header, that is, the domain name must be configured at the system host level.
npm run dev. If we need to add configuration at the command line level, we need to set it to
npm run dev --param=paramvalue. For commands executed using
npm's script script,
its parameters cannot be obtained through process.env, and are obtained through
process.env.npm_config_paramName ,
It is not very convenient to use the ready-made command line parameter parsing library, but to save trouble, for the time being, I will use the parsing that comes with npm.
- host: The host that needs to be pointed to when initiating the request may not be the same for each machine.
- port: The port forwarded by the proxy
- receiver: the remote address used for push, including the ip address. To save trouble, the ip address is not listed separately
- local: local address, that is, localhost
- remote: The specified remote machine
- Other custom types: used for other types that have been specified in the configuration file
- The original version of the request, such as 'http://xxx' or Object type configuration, such proxy will never process
- rd: The address of the remote machine
- #focus: Strict mode, all custom type proxies are converted to the specified rd machine , only available when the rd parameter exists
- allLocal: Custom type proxies all point to the local
- host: Request to discover whether to use host, and Not an IP address
- Situations where host is required for access: 4
需要更改 host:除 localhost 外都需要更改
需要对已有类型进行转换:1: 需要将所有自定义类型都转换为 local, 2和3:什么也不转换,4:所有的自定义类型全部转换为
remote 类型
这么一看,貌似 host 是不需要的,它的存在主要是针对某些 机器可能需要使用 host 的方式,所以还是保留一下。
实现
逻辑理清了就很简单了,配置文件设置为:
1 2 3 4 5 6 7 8 9 10 11 12 |
|
proxyTable 配置方式
1 2 3 4 5 6 7 8 |
|
获取 proxyTable 的代码:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 |
|
用法
用法中需要配置两种指向:系统 host 和浏览器代理 Host。
之所以要两种 host, 本质上是因为接口使用的域名
和我们的本地访问的域名是相同的,同一域名无法指向两个地址,所以相当于对浏览器端进行了拦截。
系统 host 推荐使用 switchHost 进行切换,浏览器推荐使用 whistle 进行切换。
本地开发
host 配置:无
whistle 配置:默认的域名
127.0.0.1 dev.example.com
启动命令:
1 2 |
|
注: 此时 proxyTable 中配置的 remote 全部转换为 local,在 allLocal 参数时将所有自定义类型转换为 local
本地 + 1 台远程
1 2 3 4 |
|
启动命令:
1 2 |
|
注: --host 表示使用访问使用 host 而非 ip,使用时需要 host 地址
本地 + n 台远程
host 配置:无
whistle 配置:默认的域名
127.0.0.1 dev1.example.com
127.0.0.1 dev2.example.com
1 2 3 4 5 6 7 8 |
|
proxyTable 配置:
启动命令:
1 |
|
远程 1 台机器
host 配置:
1 2 |
|
whistle 配置:默认的域名
1 2 |
|
启动命令:
1 |
|
组件优化
vue 的组件化深受大家喜爱,到底组件拆到什么程度算是合理,还要因项目大小而异,小型项目可以简单几个组件搞定,甚至不用 vuex,axios 等等,如果规模较大就要细分组件,越细越好,包括布局的封装,按钮,表单,提示框,轮播等,推荐看下 Element 组件库的代码,没时间写这么详细可以直接用 Element 库,分几点进行优化
•组件有明确含义,只处理类似的业务。复用性越高越好,配置性越强越好。
•自己封装组件还是遵循配置 props 细化的规则。
•组件分类,我习惯性的按照三类划分,page、page-item 和 layout,page 是路由控制的部分,page-item 属于 page 里各个布局块如 banner、side 等等,layout 里放置多个页面至少出现两次的组件,如 icon, scrollTop 等
相信看了本文案例你已经掌握了方法,更多精彩请关注php中文网其它相关文章!
推荐阅读:
The above is the detailed content of How to optimize Vue projects. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics





Using Bootstrap in Vue.js is divided into five steps: Install Bootstrap. Import Bootstrap in main.js. Use the Bootstrap component directly in the template. Optional: Custom style. Optional: Use plug-ins.

You can add a function to the Vue button by binding the button in the HTML template to a method. Define the method and write function logic in the Vue instance.

The watch option in Vue.js allows developers to listen for changes in specific data. When the data changes, watch triggers a callback function to perform update views or other tasks. Its configuration options include immediate, which specifies whether to execute a callback immediately, and deep, which specifies whether to recursively listen to changes to objects or arrays.

There are three ways to refer to JS files in Vue.js: directly specify the path using the <script> tag;; dynamic import using the mounted() lifecycle hook; and importing through the Vuex state management library.

Vue multi-page development is a way to build applications using the Vue.js framework, where the application is divided into separate pages: Code Maintenance: Splitting the application into multiple pages can make the code easier to manage and maintain. Modularity: Each page can be used as a separate module for easy reuse and replacement. Simple routing: Navigation between pages can be managed through simple routing configuration. SEO Optimization: Each page has its own URL, which helps SEO.

Vue.js has four methods to return to the previous page: $router.go(-1)$router.back() uses <router-link to="/" component window.history.back(), and the method selection depends on the scene.

There are three common methods for Vue.js to traverse arrays and objects: the v-for directive is used to traverse each element and render templates; the v-bind directive can be used with v-for to dynamically set attribute values for each element; and the .map method can convert array elements into new arrays.

There are two ways to jump div elements in Vue: use Vue Router and add router-link component. Add the @click event listener and call this.$router.push() method to jump.
