Vue.js v1 Documentation

website·Indexed 19 days ago

https://v1.vuejs.org/api/

Documentation for Vue.js versions 2.0, 1.0, 0.12, and 0.11. Includes the official guide on data binding syntax, components, reactivity, and installation, as well as API references, community resources, and practical examples such as TodoMVC, HackerNews clone, and Firebase integration.

Tokens
29.5K
Snippets
185
Records
238
Agent score
99%

What's inside Vue.js v1

  1. Overview of Vue.js core capabilities

    Vue.js is a library focused on the view layer for building interactive web interfaces. It provides reactive data binding and composable view components. While it is not a full-blown framework, it can be combined with tooling and supporting libraries to power sophisticated Single-Page Applications (SPAs).
  2. Manage state in large Vue.js applications

    For large-scale applications, use a Flux-inspired architecture to manage state. Recommended options include Vuex (designed specifically for Vue.js) or Redux (via bindings like revue).
  3. Initialize reactive data properties upfront

    It is recommended to declare all reactive properties in the data option during initialization rather than adding them dynamically. This serves as a schema for the component state, making the code easier to reason about, and avoids performance penalties where adding a top-level reactive property forces all watchers in that scope to re-evaluate.
    // Recommended approach
    var vm = new Vue({
      data: {
        msg: '' // declare upfront
      },
      template: '<div id="example">{{msg}}</div>'
    })
    vm.msg = 'Hello!'
  4. Use inline-template for flexible component authoring

    The inline-template attribute allows a child component to use its inner content as its template instead of treating it as distributed content. While this provides flexibility, it makes template scope harder to reason about and prevents template compilation from being cached. The best practice is to define templates inside the component using the template option.
    <my-component inline-template>
      <p>These are compiled as the component's own template</p>
      <p>Not parent's transclusion content.</p>
    </my-component>
  5. Vue.js v1 Core Features Overview

    Vue.js v1 is a progressive framework for building modern web interfaces. Key technical characteristics include:

    • Reactivity: Supports expressions and computed properties with transparent dependency tracking.
    • Component-Based: Applications are composed of decoupled, reusable components.
    • Performance: Uses precise and efficient asynchronous batch DOM updates.
    • Lightweight: Approximately 24kb (min+gzip) with no external dependencies.
    • Distribution: Available via NPM or Bower.
  6. Understand how Vue.js tracks changes

    Vue.js implements reactivity by walking through all properties in the data object during instance initialization and converting them into getters and setters using Object.defineProperty. This allows Vue to perform dependency-tracking and change-notification. For every directive or data binding in a template, a 'watcher' object is created to record touched properties as dependencies. When a dependency's setter is called, the watcher triggers a re-evaluation and updates the DOM.
  7. Access Vue.js community support and discussion channels

    Vue.js provides several official channels for developers to seek help, ask questions, and network with other developers:

    • The Forum: The primary location for asking and answering technical questions about Vue.js and its components.
    • Gitter Channel: A real-time chat platform for developers to meet and discuss the framework.
    • GitHub: The central hub for reporting bugs and submitting pull requests via forks.
  8. Bind HTML classes dynamically using Array syntax

    Use v-bind:class with an array to apply a list of classes. You can use ternary expressions for conditional classes, or embed the Object syntax inside the Array for more complex conditional logic (supported in version 1.0.19+).
    <!-- Basic array syntax -->
    <div v-bind:class="[classA, classB]"></div>
    
    <!-- Conditional class using ternary -->
    <div v-bind:class="[classA, isB ? classB : '']"></div>
    
    <!-- Mixed Array and Object syntax (v1.0.19+) -->
    <div v-bind:class="[classA, { classB: isB, classC: isC }]"></div>
    data: {
      classA: 'class-a',
      classB: 'class-b',
      isB: true,
      isC: false
    }
  9. Use computed properties for complex template logic

    In Vue.js, templates are intended for describing view structure and are limited to single expressions. For any logic requiring multiple expressions or complex operations, use a computed property. Computed properties are declarative, pure, and automatically update their dependent bindings when the underlying data changes.
    var vm = new Vue({
      el: '#example',
      data: {
        a: 1
      },
      computed: {
        // a computed getter
        b: function () {
          // `this` points to the vm instance
          return this.a + 1
        }
      }
    })
    
    // Usage:
    console.log(vm.b) // -> 2
    vm.a = 2
    console.log(vm.b) // -> 3
  10. Render raw HTML in Vue.js templates

    To output real HTML instead of plain text, use triple curly braces {{{ }}}. This inserts content as plain HTML and ignores data bindings.

    Security Warning: Dynamically rendering arbitrary HTML can lead to XSS attacks. Only use this on trusted content and never on user-provided content.

    <div>{{{ raw_html }}}</div>
  11. Register and use custom directives in Vue.js

    Custom directives allow you to map data changes to arbitrary DOM behavior. You can register a global directive using Vue.directive(id, definition) or a local directive by adding it to a component's directives option. When using the directive in a template, prefix the ID with v-.
    // Global registration
    Vue.directive('my-directive', {
      bind: function () {
        // preparation work
      },
      update: function (newValue, oldValue) {
        // handle value changes
      },
      unbind: function () {
        // cleanup work
      }
    });

    <!-- Usage in template --> <div v-my-directive="someValue"></div>

  12. Implement Single File Components (SFCs)

    Single File Components allow the encapsulation of a component's CSS styles, HTML template, and JavaScript definition in a single file. To build these components, use the following tool combinations:

    • Webpack + vue-loader
    • Browserify + vueify

    For a pre-configured build setup, the official vue-cli is recommended.