Explain the new features of vue-cli 3.0 in detail (detailed tutorial)
vue-cli is a build tool launched by the vue official team for rapid development of vue projects. It can be used out of the box and provides simple custom configuration and other functions. This article mainly introduces how to quickly understand the new features of vue-cli 3.0. Friends who need it can refer to
vue-cli is a construction tool launched by the official vue team to quickly develop vue projects. It has out-of-the-box capabilities. Use and provide simple custom configuration and other functions. There are too many new things to say about the upgrade of vue-cli from 2.0 to 3.0, but it is impossible to list all the contents in this article. This article serves as a guide to compare the 2.0 upgrade functions, allowing you to quickly understand the content of the 3.0 update. .
1. Create project:
Changes in the create project command.
vue create my-project
Version 3.0 includes default preset configurations and user-defined configurations.
Custom function configuration includes the following functions:
TypeScript
- ##Progressive Web App (PWA) Support
- Router ##Vuex
- ##CSS Pre-processors
- Linter / Formatter
- Unit Testing
- E2E Testing
- You can notice that version 3.0 is directly new Added TypeScript and PWA support.
##LESS
Stylus
And the choice of eslint specification:
ESLint with error prevention only
ESLint Airbnb config
ESLint Standard config
ESLint Prettier
Finally select the storage location for custom configurations such as Babel, PostCSS, ESLint, etc.:
- In package.json
- After selecting, you can store the above configuration as the default value, and other projects created through vue-cli in the future will use the just configuration.
We compared and found that the default project directory of vue-cli 3.0 is much simpler than that of 2.0.
Removed the configuration file directory, config and build folders.- The static folder was removed, the public folder was added, and index.html was moved to public.
- Added a views folder in the src folder to classify view components and public components.
-
3. How to customize the configuration after removing the configuration file directory.
Starting from version 3.0, placing a vue.config.js file in the root directory of the project can configure many aspects of the project. vue.config.js should export an object, for example:
module.exports = { baseUrl: '/', outputDir: 'dist', lintOnSave: true, compiler: false, // 调整内部的 webpack 配置。 // 查阅 https://github.com/vuejs/vue-doc-zh-cn/vue-cli/webpack.md chainWebpack: () => {}, configureWebpack: () => {}, // 配置 webpack-dev-server 行为。 devServer: { open: process.platform === 'darwin', host: '0.0.0.0', port: 8080, https: false, hotOnly: false, // 查阅 https://github.com/vuejs/vue-doc-zh-cn/vue-cli/cli-service.md#配置代理 proxy: null, // string | Object before: app => {} } .... }The easiest way to adjust the webpack configuration is in
vue The
configureWebpackoption in .config.js
provides an object that will bewebpack-merge merged into the final webpack configuration.
Sample code: configure webpack to add a plug-in.
// vue.config.js module.exports = { configureWebpack: { plugins: [ new MyAwesomeWebpackPlugin() ] } }To modify the parameters of a plugin option you need to be familiar with the
webpack-chain
API and read some source code to understand the full trade-offs of this option Configuration items, but it gives you a more flexible and safer way than directly modifying the values in the webpack configuration. // vue.config.jsmodule.exports = { chainWebpack: config => {
config
.tap (ARGS = & GT; {
Return [/ * New ARGS to Pass to HTML-WEBPACK-PLugin's Constructor */]
}
}
注意: When we change a webpack During configuration, you can output the complete configuration list through vue inspect > output.js. Note that it does not output a valid webpack configuration file, but a serialized format for review.
View more details
4. ESLint, Babel, browserslist related configuration:
Babel can be passed through .babelrc or The babel field in package.json is configured. ESLint can be configured through the eslintConfig field in the .eslintrc or package.json file. You may have noticed that the browserslist field in package.json specifies the target browser support range of the project.
5. Regarding adjustments to the public directory.
vue 约定 public/index.html
作为入口模板会通过 html-webpack-plugin
插件处理。在构建过程中,资源链接将会自动注入其中。除此之外,vue-cli 也自动注入资源提示( preload/prefetch ), 在启用 PWA 插件时注入 manifest/icon
链接, 并且引入(inlines) webpack runtime / chunk manifest
清单已获得最佳性能。
在 JavaScript 或者 SCSS 中通过 相对路径 引用的资源会经过 webpack 处理。放置在 public 文件的资源可以通过绝对路径引用,这些资源将会被复制,而不经过 webpack 处理。
小提示:图片最好使用相对路径经过 webpack 处理,这样可以避免很多因为修改网站根目录导致的图片404问题。
六. 新增功能:
1. 对 TypeScript 的支持。
在 3.0 版本中,选择启用 TypeScript 语法后,vue 组件的书写格式有特定的规范。
示例代码:
import { Component, Emit, Inject, Model, Prop, Provide, Vue, Watch } from 'vue-property-decorator' const s = Symbol('baz') @Component export class MyComponent extends Vue { @Emit() addToCount(n: number){ this.count += n } @Emit('reset') resetCount(){ this.count = 0 } @Inject() foo: string @Inject('bar') bar: string @Inject(s) baz: string @Model('change') checked: boolean @Prop() propA: number @Prop({ default: 'default value' }) propB: string @Prop([String, Boolean]) propC: string | boolean @Provide() foo = 'foo' @Provide('bar') baz = 'bar' @Watch('child') onChildChanged(val: string, oldVal: string) { } @Watch('person', { immediate: true, deep: true }) onPersonChanged(val: Person, oldVal: Person) { } }
以上代码相当于
const s = Symbol('baz') export const MyComponent = Vue.extend({ name: 'MyComponent', inject: { foo: 'foo', bar: 'bar', [s]: s }, model: { prop: 'checked', event: 'change' }, props: { checked: Boolean, propA: Number, propB: { type: String, default: 'default value' }, propC: [String, Boolean], }, data () { return { foo: 'foo', baz: 'bar' } }, provide () { return { foo: this.foo, bar: this.baz } }, methods: { addToCount(n){ this.count += n this.$emit("add-to-count", n) }, resetCount(){ this.count = 0 this.$emit("reset") }, onChildChanged(val, oldVal) { }, onPersonChanged(val, oldVal) { } }, watch: { 'child': { handler: 'onChildChanged', immediate: false, deep: false }, 'person': { handler: 'onPersonChanged', immediate: true, deep: true } } })
更多详细内容请关注 这里 ;
2. 对 PWA 的支持。
当我们选择启用 PWA 功能时,在打包生成的代码时会默认生成 service-worker.js 和 manifest.json 相关文件。如果你不了解 PWA, 点击这里查看 ;
需要注意的是 在 manifest.json 生成的图标信息,可以在 public/img 目录下替换。
默认情况 service-worker 采用的是 precache ,可以通过配置 pwa.workboxPluginMode 自定义缓存策略。详情
配置示例
// Inside vue.config.js module.exports = { // ...其它 vue-cli 插件选项... pwa: { workboxPluginMode: 'InjectManifest', workboxOptions: { // swSrc 中 InjectManifest 模式下是必填的。 swSrc: 'dev/sw.js', // ...其它 Workbox 选项... }, }, };
总结:
vue-cli 致力于将 Vue 生态中的工具基础标准化。它确保了各种构建工具能够基于智能的默认配置即可平稳衔接,这样你可以专注在编写你的应用上,而不必花好几天去纠结配置的问题。与此同时,它也为每个工具提供了调整配置的灵活性。
上面是我整理给大家的,希望今后会对大家有帮助。
相关文章:
在angularjs中使用select 赋值 ng-options配置方法该怎么做?
在AngularJS中使用select加载数据选中默认值的方法该怎么处理?
The above is the detailed content of Explain the new features of vue-cli 3.0 in detail (detailed tutorial). For more information, please follow other related articles on the PHP Chinese website!

Both Python and JavaScript's choices in development environments are important. 1) Python's development environment includes PyCharm, JupyterNotebook and Anaconda, which are suitable for data science and rapid prototyping. 2) The development environment of JavaScript includes Node.js, VSCode and Webpack, which are suitable for front-end and back-end development. Choosing the right tools according to project needs can improve development efficiency and project success rate.

Yes, the engine core of JavaScript is written in C. 1) The C language provides efficient performance and underlying control, which is suitable for the development of JavaScript engine. 2) Taking the V8 engine as an example, its core is written in C, combining the efficiency and object-oriented characteristics of C. 3) The working principle of the JavaScript engine includes parsing, compiling and execution, and the C language plays a key role in these processes.

JavaScript is at the heart of modern websites because it enhances the interactivity and dynamicity of web pages. 1) It allows to change content without refreshing the page, 2) manipulate web pages through DOMAPI, 3) support complex interactive effects such as animation and drag-and-drop, 4) optimize performance and best practices to improve user experience.

C and JavaScript achieve interoperability through WebAssembly. 1) C code is compiled into WebAssembly module and introduced into JavaScript environment to enhance computing power. 2) In game development, C handles physics engines and graphics rendering, and JavaScript is responsible for game logic and user interface.

JavaScript is widely used in websites, mobile applications, desktop applications and server-side programming. 1) In website development, JavaScript operates DOM together with HTML and CSS to achieve dynamic effects and supports frameworks such as jQuery and React. 2) Through ReactNative and Ionic, JavaScript is used to develop cross-platform mobile applications. 3) The Electron framework enables JavaScript to build desktop applications. 4) Node.js allows JavaScript to run on the server side and supports high concurrent requests.

Python is more suitable for data science and automation, while JavaScript is more suitable for front-end and full-stack development. 1. Python performs well in data science and machine learning, using libraries such as NumPy and Pandas for data processing and modeling. 2. Python is concise and efficient in automation and scripting. 3. JavaScript is indispensable in front-end development and is used to build dynamic web pages and single-page applications. 4. JavaScript plays a role in back-end development through Node.js and supports full-stack development.

C and C play a vital role in the JavaScript engine, mainly used to implement interpreters and JIT compilers. 1) C is used to parse JavaScript source code and generate an abstract syntax tree. 2) C is responsible for generating and executing bytecode. 3) C implements the JIT compiler, optimizes and compiles hot-spot code at runtime, and significantly improves the execution efficiency of JavaScript.

JavaScript's application in the real world includes front-end and back-end development. 1) Display front-end applications by building a TODO list application, involving DOM operations and event processing. 2) Build RESTfulAPI through Node.js and Express to demonstrate back-end applications.


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

EditPlus Chinese cracked version
Small size, syntax highlighting, does not support code prompt function

Atom editor mac version download
The most popular open source editor

MinGW - Minimalist GNU for Windows
This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

Dreamweaver CS6
Visual web development tools

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.
