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.
What's inside Vue.js v1
- 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).
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).Initialize reactive data properties upfront
It is recommended to declare all reactive properties in thedataoption 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!'Use inline-template for flexible component authoring
Theinline-templateattribute 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 thetemplateoption.<my-component inline-template> <p>These are compiled as the component's own template</p> <p>Not parent's transclusion content.</p> </my-component>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.
Understand how Vue.js tracks changes
Vue.js implements reactivity by walking through all properties in thedataobject during instance initialization and converting them into getters and setters usingObject.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.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.
Bind HTML classes dynamically using Array syntax
Usev-bind:classwith 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 }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) // -> 3Render 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>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 usingVue.directive(id, definition)or a local directive by adding it to a component'sdirectivesoption. When using the directive in a template, prefix the ID withv-.// 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>
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.