Vue.js v2 Documentation

repository·master·Indexed 26 days ago

https://github.com/vuejs/v2.vuejs.org

Official documentation for Vue.js versions 1.x and 2.x. Covers core concepts including component scope, reactivity gotchas, render functions, and streaming server-side rendering. Includes guides on migrating from 1.0 to 2.0, using vue-cli for project scaffolding, and historical version updates from 0.11 through 1.0.

Tokens
90.3K
Snippets
301
Records
500
Agent score
89%

What's inside Vue.js v2 Documentation

  1. Overview of Enter/Leave and List Transitions

    master

    Vue provides several mechanisms to apply transition effects when elements are inserted, updated, or removed from the DOM. You can use these tools to:

    • Automatically apply classes for CSS transitions and animations.
    • Integrate 3rd-party CSS animation libraries (e.g., Animate.css).
    • Use JavaScript to directly manipulate the DOM during specific transition hooks.
    • Integrate 3rd-party JavaScript animation libraries (e.g., Velocity.js).

    This guide focuses on entering, leaving, and list transitions.

  2. Overview of Vue testing strategies

    master

    Testing Vue applications involves different layers of the testing pyramid:

    1. Unit Tests: The smallest unit of work. They isolate components to test specific logic, such as computed properties or data changes. They are fast and run frequently during development.
    2. Snapshot Tests: These save the component's markup and compare it against a stored version. They notify developers if the rendered output changes unexpectedly.
    3. End-to-End (e2e) Tests: High-level tests that ensure multiple components and systems (like APIs) work together correctly (e.g., a full user signup flow). These are slower and typically run before deployment.
  3. Understand the Vue Style Guide priority categories

    master

    The Vue Style Guide categorizes rules into four priority levels to help developers decide which patterns to follow based on their impact on error prevention and code quality:

    • Priority A: Essential: Rules that help prevent errors. These should be followed at all costs, as violations often lead to bugs.
    • Priority B: Strongly Recommended: Rules that improve readability and developer experience. Violations are rare and should be well-justified.
    • Priority C: Recommended: Rules for maintaining consistency when multiple valid options exist. Following these helps you align with community standards and examples.
    • Priority D: Use with Caution: Rules highlighting features designed for edge cases or legacy migrations. Overusing these can make code difficult to maintain or introduce bugs.
  4. Understand Vue.js testing categories

    master

    Testing for Vue.js applications is generally divided into three categories to ensure different levels of reliability:

    1. Unit Testing: Testing individual units of code in isolation to ensure logic remains stable during refactoring.
    2. Component Testing: Testing Vue components by mounting them to a (virtual or real) DOM, ensuring they work correctly with Vue-specific features like Vuex or Vue Router.
    3. End-to-End (E2E) Testing: Validating the entire application stack, including frontend code, backend services, and infrastructure, by simulating real user interactions.
  5. Backported Features in Vue 2.7

    master

    Vue 2.7 backports several key features from Vue 3, allowing users to use modern APIs while remaining on the Vue 2 runtime.

    Supported Features:

    • Composition API
    • SFC <script setup>
    • SFC CSS v-bind
    • defineComponent() with improved type inference
    • h(), useSlot(), useAttrs(), useCssModules()
    • set(), del(), and nextTick() (available as named exports in ESM builds)
    • emits option (for type-checking purposes only; does not affect runtime behavior)
    • ESNext syntax in template expressions (via configured Babel loaders)
  6. Understand the Virtual DOM and VNodes

    master

    Vue uses a Virtual DOM to efficiently update the real DOM. Instead of manipulating the browser's DOM directly, you use createElement to produce VNodes (Virtual Nodes).

    A VNode is a node description that contains information about what kind of node should be rendered, including its attributes, properties, and children. Vue uses these descriptions to track changes and perform minimal updates to the actual DOM.

  7. Vue components vs Web Custom Elements

    master

    Vue components are loosely modeled after the Web Components Spec (implementing the Slot API and is attribute), but offer several advantages:

    1. Browser Support: Vue components work consistently in all supported browsers (including IE9+) without polyfills, whereas native Custom Elements require modern browser support.
    2. Advanced Features: Vue provides cross-component data flow, custom event communication, and build tool integrations not available in plain custom elements.
    3. Interoperability: Vue components can be wrapped inside native custom elements, and the Vue CLI supports building Vue components that register themselves as native custom elements.
  8. Quickstart Vue via CDN (Scaling Down)

    master

    Unlike React, which typically requires a build system (like Babel) to handle JSX, Vue can be used immediately by dropping a single script tag into an HTML page. This makes it suitable for progressive integration or small projects.

    <script src="https://cdn.jsdelivr.net/npm/vue@2"></script>
  9. Bind Inline Styles using Object Syntax

    master

    Use v-bind:style with an object to apply inline CSS styles.

    Rules for style objects:

    • Property names can be camelCase (e.g., fontSize) or kebab-case (e.g., 'font-size', which requires quotes).
    • Values can be bound to data properties or computed properties.
    • Vue automatically handles vendor prefixing for properties like transform.
    • Starting in Vue 2.3.0+: You can provide an array of values for a single property to support multiple vendor prefixes; Vue will render the last supported value.
  10. Perform dynamic state transitions for SVG elements

    master

    You can create highly dynamic SVG animations by binding SVG attributes (like points for a <polygon>) to data properties that are updated via watchers.

    When the underlying data array (e.g., stats) changes, use a watcher to trigger an animation library (like TweenLite) to transition the calculated SVG attribute (e.g., points) from its old value to the new value. This allows for real-time prototyping of complex shapes and movements.

    // Example logic for animating SVG polygon points
    new Vue({
      el: '#svg-polygon-demo',
      data: function () {
        var defaultSides = 10
        var stats = Array.apply(null, { length: defaultSides }).map(function () { return 100 })
        return {
          stats: stats,
          points: generatePoints(stats),
          sides: defaultSides,
          minRadius: 50,
          interval: null,
          updateInterval: 500
        }
      },
      watch: {
        sides: function (newSides, oldSides) {
          // Logic to add or remove stats based on side count change
          var sidesDifference = newSides - oldSides
          if (sidesDifference > 0) {
            for (var i = 1; i <= sidesDifference; i++) {
              this.stats.push(this.newRandomValue())
            }
          } else {
            var absoluteSidesDifference = Math.abs(sidesDifference)
            for (var i = 1; i <= absoluteSidesDifference; i++) {
              this.stats.shift()
            }
          }
        },
        stats: function (newStats) {
          // Use TweenLite to animate the 'points' property in $data
          TweenLite.to(
            this.$data,
            this.updateInterval / 1000,
            { points: generatePoints(newStats) }
          )
        }
      }
    })
  11. Locally register components

    master

    Local registration is preferred when using build systems (like Webpack) to enable tree-shaking and reduce bundle size. Locally registered components are not available in subcomponents unless they are also registered locally within those subcomponents.

    In ES2015+ module systems, you can use shorthand property names to register components.

    import ComponentA from './ComponentA.vue'
    
    export default {
      components: {
        // Shorthand for ComponentA: ComponentA
        ComponentA
      },
      // ...
    }
  12. Migrate from Vue 2 to Vue 3

    master
    If you are starting a new project or have the capacity to upgrade, it is strongly recommended to use Vue 3.x. Migrating to Vue 3 provides better performance, smaller bundle sizes, enhanced TypeScript support, a Proxy-based reactivity system, and new built-in components like Fragment, Teleport, and Suspense.