Vue.js
This article has multiple issues. Please help improve it or discuss these issues on the talk page. (Learn how and when to remove these messages)
|
Original author(s) | Evan You |
---|---|
Initial release | February 2014[1] |
Stable release | 2.2.6
/ March 27, 2017[2] |
Repository | |
Written in | JavaScript |
Platform | Cross-platform |
Size | 76 KB production 240 KB development |
Type | JavaScript library |
License | MIT License[3] |
Website | vuejs |
Vue.js (commonly referred to as Vue; pronounced /vjuː/, like view) is an open-source progressive JavaScript framework for building user interfaces.[4] Integration into projects that use other JavaScript libraries is made easy with Vue because it is designed to be incrementally adoptable. Also, Vue is a capable web application framework that is able to power advanced single-page applications.
According to a 2016 JavaScript survey, Vue has an 89% developer satisfaction rating. Vue accumulates around 80 GitHub stars per day,[5][6] and is the 14th most starred project on GitHub of all time.[7]
Overview
Vue.js is a popular JavaScript front-end framework that was built to organize and simplify web development.
It serves as V and lightly the C in an MVC architecture by allowing users to view live data updates and interact with the data through component specific methods (much like a controller).
The project focuses on making ideas in web UI development (components, declarative UI, hot-reloading, time-travel debugging, etc.) more approachable. It attempts to be less opinionated and thus easier for developers to pick up.
Less opinionated allows for progressive adoptable: Vue.js core is a drop-in library that can be used in existing pages. It can be used in existing projects to add simple interactivity, or to replace jQuery and other templating languages and frameworks entirely. Sister libraries can allow Vue to be the base of the entire front end architecture.[8]
History
Vue was created by Evan You after working for Google on AngularJS. You later summed up his thought process, "I figured, what if I could just extract the part that I really liked about Angular and build something really lightweight without all the extra concepts involved?"[9]
Vue was originally released in February 2014 by Evan You. The project was posted on Hacker News, Echo JS, and the /r/javascript subreddit the day of its initial release. Within one day the project reached the frontpage of all three sites.[10]
More recently Vue was featured as a Rising star in GitHub having gained the most stars of any Open Source Project on the popular website. It recently clocked 2,793 Watchers, 48,505 Stars 6,340 Forks[11] which makes it among the most popular open source projects on GitHub in general having far surpassed other popular libraries such as Backbone.js (26,178) Angular 2 (22,471)[12] or even jQuery (44,104).[13]
Features
Templates
Vue uses an HTML-based template syntax that allows you to declaratively bind the rendered DOM to the underlying Vue instance’s data. All Vue templates are valid HTML that can be parsed by spec-compliant browsers and HTML parsers. Under the hood, Vue compiles the templates into Virtual DOM render functions. Combined with the reactivity system, Vue is able to intelligently figure out the minimal number of components to re-render and apply the minimal amount of DOM manipulation when the app state changes.
In Vue, you can use the template syntax or choose to directly write render functions using JSX. In order to do so just replace the template option with a render function.[14] Render functions open up possibilities for powerful component-based patterns — for example, the new transition system is now completely component-based, using render functions internally.[15]
Reactivity
One of Vue’s most distinct features is the unobtrusive reactivity system. Models are just plain JavaScript objects. When you modify them, the view updates. It makes state management very simple and intuitive. Vue provides optimized re-rendering out of the box without you having to do anything. Each component keeps track of its reactive dependencies during its render, so the system knows precisely when to re-render, and which components to re-render.[16]
Components
Components are one of the most powerful features of Vue. In a large application, it is necessary to divide the whole app into small, self-contained, and often reusable components to make development manageable. Components extend basic HTML elements to encapsulate reusable code. At a high level, components are custom elements that Vue’s compiler attaches behavior to. In Vue, a component is essentially a Vue instance with pre-defined options.[17] The code snippet below contains an example of a Vue component. The component presents a button and prints of the number of times the button is clicked :
Vue.component('buttonclicked', {
props: ["initial_count"],
data: function() {var q = {"count": 0}; return q;} ,
template: '<button v-on:click="onclick">Clicked 0 times</button>'
,
methods: {
"onclick": function() {
this.count = this.count + 1;
}
},
mounted: function() {
this.count = this.initial_count;
}
});
Transitions
Vue provides a variety of ways to apply transition effects when items are inserted, updated, or removed from the DOM. This includes tools to:
- automatically apply classes for CSS transitions and animations
- integrate 3rd-party CSS animation libraries, such as Animate.css
- use JavaScript to directly manipulate the DOM during transition hooks
- integrate 3rd-party JavaScript animation libraries, such as Velocity.js
When an element wrapped in a transition component is inserted or removed, this is what happens:
- Vue will automatically sniff whether the target element has CSS transitions or animations applied. If it does, CSS transition classes will be added/removed at appropriate timings.
- If the transition component provided JavaScript hooks, these hooks will be called at appropriate timings.
- If no CSS transitions/animations are detected and no JavaScript hooks are provided, the DOM operations for insertion and/or removal will be executed immediately on next frame.[18][19]
Routing
In a Single Page Application (SPA) one initial disadvantage was the inability to share links to the exact "sub" page within a specific web page. Because SPAs serve their users only one url based response from the server (it typically serves index.html or index.vue), saving bookmarks, or sharing links to a specific article would be impossible. To solve this problem front end routers provide artificial hash based URLs originally split by a hash-bang (#!) page.com/#!/ with html5 most modern browsers support routing without the use of a hashbang. JavaScript Libraries like Vue provide an easy Interface to change what is displayed on the page based on the current URL path -- regardless of how it was changed (whether by emailed link, refresh, or in page links). Additionally, using a front-end router allows for the intentional transition of the browser path when certain browser events (i.e. clicks) occur on buttons or links. Vue itself doesn’t come with front-end hashed routing. But the open source "vue-router" package provides this API to change browser URL, use the back button (hash history), and email password reset or email verification links with authentication parameters provided in the URL. It supports mapping nested routes to nested components and offers fine-grained transition control. Creating a front end routed Single-Page Application with Vue + vue-router is made simple. With Vue, developers are already composing applications with small building blocks building larger components. With vue-router, added to the mix, components must merely be mapped to the routes they belong to, and parent/root routes must indicate where children should render.[20]
<div id="app">
<router-view></router-view>
</div>
const User = {
template: '<div>User [[:Template:$route.params.id]]</div>'
}
const router = new VueRouter({
routes: [
{ path: '/user/:id', component: User }
]
})
The code above:
- Sets a front-end route at `websitename.com/user/__insert an id here__`.
- Which will render in the User component defined in (const User...)
- Allows the User component to pass in the particular id of the user which was typed into the URL using the $route object's params key: `$route.params.id`.
- This template (varying by the params passed in to the router) will be rendered into `<router-view></router-view>` inside the DOM's div#app.
- The finally generated HTML for someone typing in: `websitename.com/user/1` will be:
<div id="app">
<div>
<div>User 1</div>
</div>
</div>
Supporting libraries
See also
- Comparison of JavaScript frameworks
- React (JavaScript library)
- AngularJS
- JavaScript Framework
- JavaScript Library
References
- ^ "First Week of Launching Vue.js". Evan You.
- ^ "Vue.js Releases". GitHub.
- ^ "vue/LICENSE". GitHub. Retrieved 17 April 2017.
- ^ "Introduction — Vue.js". Retrieved 2017-03-11.
- ^ "State Of JavaScript Survey Results: Front-end Frameworks". Retrieved 2017-03-11.
- ^ "Trending JavaScript Frameworks". Retrieved 2017-03-11.
{{cite web}}
: Cite has empty unknown parameter:|dead-url=
(help) - ^ "Most Starred Repositories". GitHub. Retrieved 2017-03-11.
{{cite web}}
: Cite has empty unknown parameter:|dead-url=
(help) - ^ "Evan is creating Vue.js | Patreon". Patreon. Retrieved 2017-03-11.
- ^ "Between the Wires | Evan You". Between the Wires. 2016-11-03. Retrieved 2017-03-11.
- ^ "First Week of Launching Vue.js". Evan You. Retrieved 2017-03-11.
- ^ You, Evan. "GitHub: VueJS/Vue". Vue JS GitHub Page. Retrieved 29 March 2017.
- ^ GitHub https://github.com/search?utf8=✓&q=angular&type=. Retrieved 29 March 2017.
{{cite web}}
: Missing or empty|title=
(help) - ^ "jQuery GitHub Page". GitHub. Retrieved 29 March 2017.
- ^ "Template Syntax — Vue.js". Retrieved 2017-03-11.
- ^ "Vue 2.0 is Here!". The Vue Point. 2016-09-30. Retrieved 2017-03-11.
{{cite web}}
: Cite has empty unknown parameter:|dead-url=
(help) - ^ "Reactivity in Depth — Vue.js". Retrieved 2017-03-11.
- ^ "Components — Vue.js". Retrieved 2017-03-11.
- ^ "Transition Effects — Vue.js". Retrieved 2017-03-11.
- ^ "Transitioning State — Vue.js". Retrieved 2017-03-11.
- ^ "Routing — Vue.js". Retrieved 2017-03-11.
- ^ You, Evan. "Vue Nested Routing (2)". Vue Home Page (subpage). Retrieved 10 May 2017.
- ^ "vue-router". router.vuejs.org. Retrieved 2017-03-11.
{{cite web}}
: Cite has empty unknown parameter:|dead-url=
(help) - ^ "vuex". vuex.vuejs.org. Retrieved 2017-03-11.
{{cite web}}
: Cite has empty unknown parameter:|dead-url=
(help) - ^ "vue-loader". vue-loader.vuejs.org. Retrieved 2017-03-11.
{{cite web}}
: Cite has empty unknown parameter:|dead-url=
(help) - ^ "vueify". GitHub. Retrieved 2017-03-11.
{{cite web}}
: Cite has empty unknown parameter:|dead-url=
(help) - ^ "vue-cli". GitHub. Retrieved 2017-03-11.
{{cite web}}
: Cite has empty unknown parameter:|dead-url=
(help)