Vue.js 2 Documentation
website·Indexed 19 days ago
https://es.vuejs.org/Official documentation for Vue 2, featuring guides on components, render functions, and Single File Components (SFC). Includes a cookbook with practical examples for form validation, serverless blogs, Dockerization, and Axios API consumption, as well as migration paths from Vue 1.x and Vuex 0.6.x.
What's inside Vue.js 2
- Vue.js (pronounced /vjuː/, as in 'view') is a progressive framework for building user interfaces. It is designed to be adopted incrementally, focusing primarily on the view layer, making it easy to integrate into existing projects or use to power complex Single-Page Applications (SPAs) when combined with modern build tools and supporting libraries.
Introduction to Vue Single File Components (SFCs)
v2Single File Components (SFCs) use the.vueextension to encapsulate a component's template, logic, and styles in a single file. This approach solves several limitations of global component definitions and text-based templates, such as the lack of syntax highlighting, the need for unique global names, and the absence of modular CSS. SFCs require a build step using tools like Webpack or Browserify and thevue-loaderplugin.Overview of the Vue.js 2 Cookbook
v2The Vue.js 2 Cookbook is a collection of self-contained 'recipes' designed to provide deeper technical insights than the standard Guide. While the Guide focuses on a progressive learning path with simple examples, the Cookbook offers:
- Specific Focus: Each recipe addresses a particular aspect of Vue independently.
- Greater Depth: Includes complex examples and combinations of features.
- JavaScript Integration: Explains essential JavaScript (ES5/ES6+) functions within the context of Vue development.
- Ecosystem Exploration: Provides deeper dives into ecosystem libraries and tools (e.g., Webpack configuration for SFCs).
Choose between Vuex 1.0 and 2.0 for Vue 2.0 projects
v2Both Vuex 1.0 and 2.0 are compatible with Vue 1.0 and 2.0.
- Vuex 2.0: A radical redesign and simplification of the API. Recommended for new projects or those wanting the latest client-side state management approach.
- Vuex 1.0: Mostly backward compatible with 0.6.x. Recommended for projects with large existing codebases that require a smooth, incremental migration.
Recommended IDEs for Vue 2 and TypeScript
v2For the best development experience with Vue 2 and TypeScript, the following tools are recommended:
- Visual Studio Code: Provides strong native TypeScript support. When using Single File Components (SFCs), install the Vetur extension for TypeScript inference within
.vuefiles. - WebStorm: Offers built-in support for both TypeScript and Vue.
- Visual Studio Code: Provides strong native TypeScript support. When using Single File Components (SFCs), install the Vetur extension for TypeScript inference within
Adopt Flux architecture with Vuex for advanced state management
v2For complex applications, follow the Flux architecture convention where components do not mutate store state directly. Instead, components dispatch events (actions) to notify the store to perform mutations. This pattern enables advanced debugging tools such as mutation logs, state snapshots, and time-travel debugging. The official implementation of this pattern for Vue is Vuex.Understand Vue 2 reactivity mechanism
v2Vue 2 achieves reactivity by converting all properties in thedataobject into getters and setters usingObject.defineProperty. This allows Vue to track dependencies and notify watchers when a property is accessed or modified, triggering a component re-render. Because this relies on ES5 features, Vue 2 does not support IE8 or lower.Understand Vue's automatic dependency tracking for performance
v2Unlike React, which may require manual optimizations likePureComponentorshouldComponentUpdateto prevent unnecessary re-renders of child component subtrees, Vue automatically tracks component dependencies during render. This means Vue knows exactly which components need to re-render when state changes, effectively providing the benefits ofshouldComponentUpdateautomatically without requiring manual implementation or immutable data structures.Handle DOM template parsing restrictions with the is attribute
v2Certain HTML elements (like
<table>,<ul>,<ol>,<select>) have strict requirements about their children. If a custom component is placed directly inside these elements, it may be treated as invalid content. To solve this, use theisattribute on a valid child element (e.g.,<tr>inside<table>) to render the component.Note: This limitation does not apply when using string templates, Single File Components (.vue), or
<script type="text/x-template">.<!-- Invalid: <blog-post-row> is not a valid child of <table> --> <table> <blog-post-row></blog-post-row> </table> <!-- Valid: Use the 'is' attribute on a valid <tr> element --> <table> <tr is="blog-post-row"></tr> </table>Manage state and routing in large-scale Vue applications
v2For large-scale applications, Vue provides officially supported and maintained libraries for routing and state management, ensuring deep integration and consistent updates with the core library.
- State Management: Vuex is the official state management library inspired by Elm. However, other patterns like Redux can also be integrated into Vue applications.
- Routing: Vue provides a robust, officially supported routing solution.
Understand the Virtual DOM and VNodes in Vue 2
v2Vue uses a Virtual DOM to track changes before updating the real DOM. Instead of creating real DOM elements, thecreateElementfunction returns a Virtual Node (VNode). A VNode is a JavaScript object that describes the type of node to be rendered on the page, including its children. The entire tree of VNodes is referred to as the Virtual DOM.Use Vuex for complex state management in Vue 2
v2For large applications with complex state interactions across many components, Vue provides Vuex, a state management library inspired by Elm. Vuex is specifically designed for Vue, offering a more intuitive API and better integration than view-agnostic libraries like Redux. It also integrates with vue-devtools to provide time-travel debugging.