Vue-loader: $set method doesn't seem to be working within components

Created on 12 Sep 2016  路  4Comments  路  Source: vuejs/vue-loader

Hi all.

I've been getting errors when trying to use $set from within a component. As an example, start with the webpack-simple template and edit App.vue to become:

<template>
  <div id="app">
    <button @click.prevent="checkSet">Some Text</button>
  </div>
</template>
<script>
export default {
   methods: {
      checkSet: function(){
         this.$set('test', 'testing');
      },
  } 
   data() {
      return{
         foo: 'bar'
      }
   }
}
</script>

Whenever checkSet is run I get the following:
vue.common.js?e881:1117 Uncaught TypeError: Cannot create property 'testing' on string 'test'

Am I just missing something here?
Thanks for your help.

Most helpful comment

For future reference everyone, the syntax is now:

Vue.set(object, key, value) // this.$set is now an alias to the global Vue.set

Fixed version of @matt-sanders example:

import Vue from 'vue';
...
checkSet: function(){
   Vue.set(this.data, 'test', 'testing');
}

Docs: http://rc.vuejs.org/api/#Vue-set

Hope that helps!

All 4 comments

You should use Vue.$set instead in Vue2.0.
https://github.com/vuejs/vue/issues/2873

I did try but got the same error

import Vue from 'vue';
...
checkSet: function(){
   Vue.set('test', 'testing');
}

thanks to tribex for the solution on gitter:

export default {
   methods: {
      checkSet: function(){
         this.$set(this.form, 'test', 'testing');
         //or Vue.set
      },
  } 
   data() {
      return{
         form: {
            foo: 'bar'
         }
      }
   }
}

For future reference everyone, the syntax is now:

Vue.set(object, key, value) // this.$set is now an alias to the global Vue.set

Fixed version of @matt-sanders example:

import Vue from 'vue';
...
checkSet: function(){
   Vue.set(this.data, 'test', 'testing');
}

Docs: http://rc.vuejs.org/api/#Vue-set

Hope that helps!

Was this page helpful?
0 / 5 - 0 ratings

Related issues

amorphine picture amorphine  路  3Comments

ryanelian picture ryanelian  路  3Comments

jorgy343 picture jorgy343  路  3Comments

TheVexatious picture TheVexatious  路  3Comments

chrisvfritz picture chrisvfritz  路  4Comments