Questions tagged [vue.js]

Vue.js is an open-source, progressive JavaScript framework for building user interfaces that aims to be incrementally adoptable. Vue.js is mainly used for front-end development and requires an intermediate level of HTML and CSS. Vue.js questions are highly version specific and should always be tagged with [vuejs2] or [vuejs3] in addition to this tag.

What is Vue.js?

Vue.js is an open-source, progressive JavaScript framework for building user interfaces that aims to be incrementally adoptable.

Instead of performing manual updates to the DOM, which can be repetitive and error prone (think jQuery), Vue embraces the idea of "data-driven views," where changes in data drive changes in the DOM.

This idea forms the core of Vue.js: a reactive data-binding system that is designed to make it extremely simple to keep your data and the DOM in sync.

Vue data-binding diagram

What makes Vue particularly powerful, however, is that it can be built upon, increasing its functionality from a simple view-model library to that of a fully fledged JavaScript framework capable of powering entire SPA's via supporting plugins and libraries such as Vue Router, Vue Resource, and Vuex.


Features

Vue.js includes:

2.0 also includes:

Vue 3.0 is currently in development, and includes some notable changes and improvements detailed here on VueJS.org.

Questions that are version specific should be tagged with or respectively.

Vue is compatible with all ES5-compliant browsers. As such, IE8 and below are unsupported.

To check out an in-depth guide with live examples or the official docs, visit vuejs.org.


Code Example

All that is required to use Vue in a project is to create a Vue instance, and tell it where to render in the DOM. Vue handles the rest!

The code below forms a simple, fully functioning Vue app:

<html>
<div id="demo">
  <p>{{ message }}</p>
  <input v-model="message">
</div>

<script src="https://unpkg.com/vue@2"></script>
<script>
  new Vue({
    el: '#demo', //Tells Vue to render in HTML element with id "demo"
    data() {
      return {
        message: 'Hello Vue.js!'
      }
    }
  });
</script>
</html>

See a live and editable version here.


Related Tags

UI Frameworks built with Vue.js

Tooling

Resources


Official Logo:


Using Vue in Stack Snippets

It is often useful to share runnable snippets of code in questions regarding Vue components. Since Stack Snippets can't natively run Vue single file components (.vue files), it's necessary to do a little conversion work.

To create a Stack Snippet containing Vue in a Stack Overflow post, make sure you:

  1. Import Vue into the HTML section of the snippet
    • You can do this through the sidebar of the snippet editor itself:
      Vue version select in snippet editor sidebar
    • Or by manually adding a <script> tag
      (eg. <script src="https://unpkg.com/vue@2"></script>)
    • NOTE: It's important that the Vue import is before other imports of libraries that depend on Vue (eg. Vue Router)
  2. Wrap component JavaScript with new Vue({...})
    • Replace default export with new Vue, and don't forget the parenthesis () around your brackets {}
    • Note that you won't be able to use import statements, so you should trim everything out of the snippet that's not strictly necessary to understand your question and the problem at hand
    • You may also need to add example data if your component relied on props or APIs
  3. Tell the new Vue instance where to render in your HTML
    • Use the el property of the new Vue instance to indicate what HTML container Vue should render in (see example below)

You may also want to replace your <template> HTML elements with some alternative (ie. <div>) if it causes issues.

Starting with something like this:

HelloWorld.vue

<template>
  <div>
    <p>{{ message }}</p>
    <input v-model="message">
  </div>
</template>

<script>
  export default {
    data() {
      return {
        message: 'Hello World!'
      }
    }
  }
</script>

Will end up with something like this:

HelloWorld.html

<html>
<div id="app">
  <p>{{ message }}</p>
  <input v-model="message">
</div>

<!-- Don't forget to include Vue from CDN! -->
<script src="https://unpkg.com/vue@2"></script>
<script>
  new Vue({
    el: '#app', //Tells Vue to render in HTML element with id "app"
    data() {
      return {
        message: 'Hello World!'
      }
    }
  });
</script>
</html>

You can drop this straight into the HTML section of a snippet, or split it into the HTML and JavaScript sections accordingly. Bear in mind that splitting it may make the code clearer to readers!

105350 questions
16
votes
1 answer

Uncaught ReferenceError: Vue is not defined when put vue setting in Index.html

i recently learning about vue I have this file main.js import Vue from 'vue/dist/vue.js' import Buefy from 'buefy' import 'buefy/lib/buefy.css' Vue.use(Buefy) var App = new Vue({ el: '#app', data: { message : "It's working" …
YVS1102
  • 2,658
  • 5
  • 34
  • 63
16
votes
2 answers

How to publish a library of Vue.js components?

I am working on a project containing a Vuex module and an abstract components that users can extend from. I would love to publish this on NPM to clean up my codebase and pull this away from my project as a solid well tested module. I have specified…
Stephan-v
  • 19,255
  • 31
  • 115
  • 201
16
votes
2 answers

VueJS @click with anchor tags

I have a single page application with a side navigation bar which has a set of anchor tags in a list. I want to show which content to show in the page depending on the value of the selectedPage variable. As well as change the value of selectedPage…
excedion
  • 327
  • 2
  • 5
  • 15
16
votes
3 answers

How to evaluate Vue.js component props in the data property?

I have 2 components: Post and Comments. Inside Post component, there is Comments component that has 3 props: postId, numCom (number of comments) and comments (array). I get comments and I pass the array with props, and now I want to retrieve the…
Miha Vidakovic
  • 393
  • 1
  • 4
  • 13
16
votes
1 answer

vue-router and Express

I've build a simple app using Vue.js and their vue-cli webpack. I've used vue-router to control navigation around the site with the different views. This works fine when running locally. I'm now wanting to deploy this onto Heroku, however the urls…
James Clare
  • 553
  • 1
  • 4
  • 11
16
votes
1 answer

How to Properly Use Vue Router beforeRouteEnter or Watch to trigger method in Single File Component?

I'm working on an app in Vue.js using Single File Components and Vue Router. I have a Search component where I need to execute a method to re-populate search results each time a user visits the route. The method executes correctly the first time the…
Adam Sweeney
  • 161
  • 1
  • 1
  • 4
16
votes
1 answer

VueJS - How to make models and collections?

I am attempting to learn VueJS and I'm finding it hard to understand how to make models and collections. For example, I want to have a collection (or list) of Employees and each Employee is a model. But I'm not sure how to accomplish this in…
zardon
  • 1,601
  • 4
  • 23
  • 46
16
votes
1 answer

How do I register a Vue component?

I have the following files. All I want to do is to be able to create different components that are injected. How do I achieve this using require.js? Here are my files: main.js define(function(require) { 'use strict'; var Vue =…
KingKongFrog
  • 13,946
  • 21
  • 75
  • 124
16
votes
3 answers

csrf_token of Django into Vuejs when seperate them

I am using ajax request to send POST but it got response 403 because of csrf_token. I divide the frontend just using Vuejs and backend using Django to just reponse API only so I can't use Django template to render {% csrf_token %} or having…
John T
  • 329
  • 1
  • 3
  • 13
16
votes
3 answers

How to propagate a Vue.js event up the components chain?

I do have three components. I don't have any influence on what the Datatable component does cause I have it from npm. Now I want to send an event from EditButton to my Zonelist. Zonelist component: