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
241
votes
4 answers

Difference between @click and v-on:click Vuejs

The questions should be enough clear. But I can see that someone use: Someone use: But really what is the difference between the two (if exists)
LorenzoBerti
  • 6,704
  • 8
  • 47
  • 89
240
votes
18 answers

vuejs update parent data from child component

I'm starting to play with vuejs (2.0). I built a simple page with one component in it. The page has one Vue instance with data. On that page I registered and added the component to html. The component has one input[type=text]. I want that value to…
Gal Ziv
  • 6,890
  • 10
  • 32
  • 43
239
votes
15 answers

What is the difference between the views and components folders in a Vue project?

I just used the command line (CLI) to initialize a Vue.js project. The CLI created a src/components and src/views folder. It has been a few months since I have worked with a Vue project and the folder structure seems new to me. What is the…
drsnark
  • 2,813
  • 2
  • 15
  • 19
236
votes
18 answers

Vue.js redirection to another page

I'd like to make a redirection in Vue.js similar to the vanilla javascript window.location.href = 'some_url' How could I achieve this in Vue.js?
Jimmy Obonyo Abor
  • 7,335
  • 10
  • 43
  • 71
234
votes
12 answers

Vuejs: Event on route change

On my main page I have dropdowns that show v-show=show by clicking on the link @click = "show=!show" and I want to set show=false when I change the route. Please advise me on how to realize this thing.
kipris
  • 2,899
  • 3
  • 20
  • 28
234
votes
12 answers

VueJS conditionally add an attribute for an element

In VueJS we can add or remove a DOM element using v-if: but is there a way to add / remove attributes of a dom element eg for the following conditionally set the required attribute: Username:…
Don Smythe
  • 9,234
  • 14
  • 62
  • 105
229
votes
18 answers

How to implement debounce in Vue2?

I have a simple input box in a Vue template and I would like to use debounce more or less like this: However the debounce property has been deprecated in Vue 2. The recommendation only says:…
MartinTeeVarga
  • 10,478
  • 12
  • 61
  • 98
228
votes
15 answers

How to set URL query params in Vue with Vue-Router

I am trying to set query params with Vue-router when changing input fields, I don't want to navigate to some other page but just want to modify url query params on the same page, I am doing like this: this.$router.replace({ query: { q1: "q1" }…
Saurabh
  • 71,488
  • 40
  • 181
  • 244
225
votes
5 answers

How to call an action from within another action in Vuex?

I have the following setup for my actions: get1: ({commit}) => { //things this.get2(); //this is my question! }, get2: ({commit}) => { //things }, I want to be able to call one action from within another, so in this example I want to be able…
muttley91
  • 12,278
  • 33
  • 106
  • 160
215
votes
21 answers

How to change port number in vue-cli project

How to change Port number in Vue-cli project so that it run's on another port instead of 8080.
salman
  • 2,287
  • 3
  • 11
  • 11
215
votes
3 answers

Vuex - passing multiple parameters to mutation

I am trying to authenticate a user using vuejs and laravel's passport.I am not able to figure out how to send multiple parameters to the vuex mutation via an action. - store - export default new Vuex.Store({ state: { isAuth:…
Schwesi
  • 4,753
  • 8
  • 37
  • 62
213
votes
4 answers

Vue 'export default' vs 'new Vue'

I just installed Vue and have been following some tutorials to create a project using the vue-cli webpack template. When it creates the component, I notice it binds our data inside of the following: export default { name: 'app', data:…
rockzombie2
  • 2,835
  • 4
  • 16
  • 20
208
votes
41 answers

Detect click outside element

How can I detect a click outside my element? I'm using Vue.js so it's gonna be outside my templates element. I know how to do it in Vanilla JS, but I'm not sure if there's a more proper way to do it, when I'm using Vue.js? This is the solution for…
user6102188
203
votes
6 answers

[Vue warn]: Cannot find element

I'm using Vuejs. This is my markup:
This is my code: var main = new Vue({ el: '#main', data: { …
dopatraman
  • 13,416
  • 29
  • 90
  • 154
202
votes
14 answers

Using Environment Variables with Vue.js

I've been reading the official docs and I'm unable to find anything on environment variables. Apparently there are some community projects that support environment variables but this might be overkill for me. So I was wondering if there's something…
Edgar Quintero
  • 4,223
  • 2
  • 34
  • 37