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
308
votes
9 answers

Vue v-on:click does not work on component

I'm trying to use the on click directive inside a component but it does not seem to work. When I click the component nothings happens when I should get a 'test clicked' in the console. I don't see any errors in the console, so I don't know what am I…
Javier Cárdenas
  • 3,935
  • 4
  • 25
  • 35
304
votes
22 answers

How to add external JS scripts to VueJS Components?

I've to use two external scripts for the payment gateways. Right now both are put in the index.html file. However, I don't want to load these files at the beginning itself. The payment gateway is needed only in when user open a specific component…
Gijo Varghese
  • 11,264
  • 22
  • 73
  • 122
301
votes
2 answers

Difference between the created and mounted events in Vue.js

Vue.js documentation describes the created and mounted events as follows: created Called synchronously after the instance is created. At this stage, the instance has finished processing the options which means the following have been set up:…
lesssugar
  • 15,486
  • 18
  • 65
  • 115
301
votes
21 answers

'Property does not exist on type 'never'

This is similar to #40796374 but that is around types, while I am using interfaces. Given the code below: interface Foo { name: string; } function go() { let instance: Foo | null = null; let mutator = () => { instance = { name:…
Ray Booysen
  • 28,894
  • 13
  • 84
  • 111
301
votes
28 answers

Vue 2 - Mutating props vue-warn

I started https://laracasts.com/series/learning-vue-step-by-step series. I stopped on the lesson Vue, Laravel, and AJAX with this error: vue.js:2574 [Vue warn]: Avoid mutating a prop directly since the value will be overwritten whenever the parent…
Dariusz Chowański
  • 3,129
  • 2
  • 14
  • 18
297
votes
11 answers

How to call function on child component on parent events

Context In Vue 2.0 the documentation and others clearly indicate that communication from parent to child happens via props. Question How does a parent tell its child an event has happened via props? Should I just watch a prop called event? That…
jbodily
  • 3,613
  • 2
  • 20
  • 21
288
votes
12 answers

Methods vs Computed in Vue

What is the main difference between a method and a computed value in Vue.js? They seem the same and interchangeable to me.
Bootstrap4
  • 3,671
  • 4
  • 13
  • 17
283
votes
4 answers

Passing event and argument to v-on in Vue.js

I pass a parameter in v-on:input directives. If I don't pass it, I can access the event in the method. Is there any way I can still access the event when passing the parameter to the function? Note that I am using vue-router. This is without passing…
geckob
  • 7,680
  • 5
  • 30
  • 39
274
votes
32 answers

error /node_modules/node-sass: Command failed

When I tried install vue store front in my local but when I tried "yarn install" command I get following error. How can I solve this error? How can I solve this error? error /var/www/html/vue-storefront/node_modules/node-sass: Command failed. Exit…
Adil
  • 3,241
  • 3
  • 11
  • 9
273
votes
6 answers

Display unescaped HTML in Vue.js

How can I manage to get HTML interpreted inside a mustache binding? At the moment the break (
) is just displayed/escaped. Small Vue app: var logapp = new Vue({ el: '#logapp', data: { title: 'Logs', logs: [ { status: true,…
Mike
  • 5,416
  • 4
  • 40
  • 73
265
votes
15 answers

Vue Js - Loop via v-for X times (in a range)

How can I repeat a loop via v-for X (e.g. 10) times?
  • {{ item.name }} - {{ item.price }}
The documentation shows:
264
votes
13 answers

Vuex Action vs Mutations

In Vuex, what is the logic of having both "actions" and "mutations?" I understand the logic of components not being able to modify state (which seems smart), but having both actions and mutations seems like you are writing one function to trigger…
Kobi
  • 4,003
  • 4
  • 18
  • 23
259
votes
13 answers

forEach is not a function error with JavaScript array

I'm trying to make a simple loop: const parent = this.el.parentElement console.log(parent.children) parent.children.forEach(child => { console.log(child) }) But I get the following error: VM384:53 Uncaught TypeError: parent.children.forEach is…
alexchenco
  • 53,565
  • 76
  • 241
  • 413
256
votes
2 answers

Is there a way to dispatch actions between two namespaced vuex modules?

Is it possible to dispatch an action between namespaced modules? E.g. I have Vuex modules "gameboard" and "notification". Each are namespaced. I would like to dispatch an action from the gameboard module in the notification module. I thought I could…
Chris Schmitz
  • 20,160
  • 30
  • 81
  • 137
247
votes
5 answers

How to pass a value from Vue data to href?

I'm trying to do something like this: I can't figure out how to add the value of r.id to the end of the href attribute so that I can make an API call. Any…
bbennett36
  • 6,065
  • 10
  • 20
  • 37