ThinkChat🤖让你学习和工作更高效,注册即送10W Token,即刻开启你的AI之旅 广告
Installation [[vue官方网站 install]](https://vuejs.org/v2/guide/installation.html) [TOC] ## Direct`<script>`Include Simply download and include with a script tag.`Vue`will be registered as a global variable. >[info] Don’t use the minified version during development. You will miss out on all the nice warnings for common mistakes! ## CDN For prototyping or learning purposes, you can use the latest version with: ~~~ <script src="https://cdn.jsdelivr.net/npm/vue"></script> ~~~ For `production`, we recommend linking to a specific version number and build to avoid unexpected breakage from newer versions: ~~~ <script src="https://cdn.jsdelivr.net/npm/vue@2.6.10/dist/vue.js"></script> ~~~ If you are using native ES Modules, there is also an ES Modules compatible build: ~~~ <script type="module"> import Vue from 'https://cdn.jsdelivr.net/npm/vue@2.6.10/dist/vue.esm.browser.js' </script> ~~~ You can browse the source of the NPM package at [cdn.jsdelivr.net/npm/vue](https://cdn.jsdelivr.net/npm/vue/). Vue is also available on [unpkg](https://unpkg.com/vue@2.6.10/dist/vue.js) and [cdnjs](https://cdnjs.cloudflare.com/ajax/libs/vue/2.6.10/vue.js)(cdnjs takes some time to sync so the latest release may not be available yet). Make sure to read about [the different builds of Vue](https://vuejs.org/v2/guide/installation.html#Explanation-of-Different-Builds) and use the **production version** in your published site, replacing `vue.js` with `vue.min.js`. This is a smaller build optimized for speed instead of development experience. ## NPM NPM is the recommended installation method when building large scale applications with Vue. It pairs nicely with module bundlers such as [Webpack](https://webpack.js.org/) or [Browserify](http://browserify.org/). Vue also provides accompanying tools for authoring `Single File Components`. ~~~ # latest stable $ npm install vue ~~~ ## CLI Vue provides an [official CLI](https://github.com/vuejs/vue-cli) for quickly scaffolding ambitious Single Page Applications. It provides batteries-included build setups for a modern frontend workflow. It takes only a few minutes to get up and running with hot-reload, lint-on-save, and production-ready builds. See [the Vue CLI docs](https://cli.vuejs.org/) for more details. >[info] The CLI assumes prior knowledge of Node.js and the associated build tools. If you are new to Vue or front-end build tools, we strongly suggest going through [the guide](https://vuejs.org/v2/guide/) without any build tools before using the CLI. ## Explanation of Different Builds In the `dist/` (directory of the NPM package) you will find many different builds of Vue.js. | | UMD | CommonJS | ES Module (for bundlers) | ES Module (for browsers) | | --- | --- | --- | --- | --- | | **Full** | vue.js | vue.common.js | vue.esm.js | vue.esm.browser.js | | **Runtime-only** | vue.runtime.js | vue.runtime.common.js | vue.runtime.esm.js | \- | | **Full (production)** | vue.min.js | \- | \- | vue.esm.browser.min.js | | **Runtime-only (production)** | vue.runtime.min.js | \- | * **Full**: builds that contain both the compiler and the runtime. * **Compiler**: code that is responsible for compiling template strings into JavaScript render functions. * **Runtime**: code that is responsible for creating Vue instances, rendering and patching virtual DOM, etc. Basically everything minus the compiler. * **[UMD](https://github.com/umdjs/umd)**: UMD builds can be used directly in the browser via a`<script>`tag. The default file from jsDelivr CDN at [https://cdn.jsdelivr.net/npm/vue](https://cdn.jsdelivr.net/npm/vue) is the Runtime + Compiler UMD build (`vue.js`). * **[CommonJS](http://wiki.commonjs.org/wiki/Modules/1.1)**: CommonJS builds are intended for use with older bundlers like `browserify` or `webpack 1`. The default file for these bundlers (`pkg.main`) is the Runtime only CommonJS build (`vue.runtime.common.js`). * **[ES Module](http://exploringjs.com/es6/ch_modules.html)**: starting in 2.6 Vue provides two ES Modules (ESM) builds: * ESM for bundlers: intended for use with modern bundlers like `webpack 2`. ESM format is designed to be statically analyzable so the bundlers can take advantage of that to perform “tree-shaking” and eliminate unused code from your final bundle. The default file for these bundlers (`pkg.module`) is the Runtime only ES Module build (`vue.runtime.esm.js`). * ESM for browsers (2.6+ only): intended for direct imports in modern browsers via`<script type="module">`. * **Runtime + Compiler vs. Runtime-only** If you need to compile templates on the client (e.g. passing a string to the `template` option, or mounting to an element using its in-DOM HTML as the template), you will need the compiler and thus the full build: ~~~ // this requires the compiler new Vue({ template: '<div>{{ hi }}</div>' }) // this does not new Vue({ render (h) { return h('div', this.hi) } }) ~~~ When using `vue-loader` or `vueify`, templates inside `*.vue` files are pre-compiled into JavaScript at build time. You don’t really need the compiler in the final bundle, and can therefore use the runtime-only build. Since the runtime-only builds are roughly 30% lighter-weight than their full-build counterparts, you should use it whenever you can. If you still wish to use the full build instead, you need to configure an alias in your bundler: `webpack.config.js` ~~~ module.exports = { // ... resolve: { alias: { 'vue$': 'vue/dist/vue.esm.js' // 'vue/dist/vue.common.js' for webpack 1 } } } ~~~ * **Development vs. Production Mode** Development/production modes are hard-coded for the UMD builds: the un-minified files are for development, and the minified files are for production. CommonJS and ES Module builds are intended for bundlers, therefore we don’t provide minified versions for them. You will be responsible for minifying the final bundle yourself. CommonJS and ES Module builds also preserve raw checks for `process.env.NODE_ENV` to determine the mode they should run in. You should use appropriate bundler configurations to replace these environment variables in order to control which mode Vue will run in. Replacing`process.env.NODE_ENV`with string literals also allows minifiers like UglifyJS to completely drop the development-only code blocks, reducing final file size. `webpack.config.js` In Webpack 4+, you can use the`mode`option: ~~~ module.exports = { mode: 'production' } ~~~ But in Webpack 3 and earlier, you’ll need to use [DefinePlugin](https://webpack.js.org/plugins/define-plugin/): ~~~ var webpack = require('webpack') module.exports = { // ... plugins: [ // ... new webpack.DefinePlugin({ 'process.env': { NODE_ENV: JSON.stringify('production') } }) ] } ~~~ ## [CSP environments](https://vuejs.org/v2/guide/installation.html#CSP-environments "CSP environments") Some environments, such as Google Chrome Apps, enforce Content Security Policy (CSP), which prohibits the use of `new Function()` for evaluating expressions. The full build depends on this feature to compile templates, so is unusable in these environments. On the other hand, the runtime-only build is fully CSP-compliant. When using the runtime-only build with [Webpack + vue-loader](https://github.com/vuejs-templates/webpack-simple) or [Browserify + vueify](https://github.com/vuejs-templates/browserify-simple), your templates will be precompiled into`render`functions which work perfectly in CSP environments.