Home » vue » Reload page after change a value of a field [VUE.JS]

Reload page after change a value of a field [VUE.JS]

Posted by: admin November 26, 2021 Leave a comment

Questions:

I’m totally noob at web development, I’ve Vue.js project, at created() function I’m parsing data and printing it, as I guess is hooking function and called at stage of page rendering or something like that.
I want to call created() after a user modifies an input value.

I’m using v-model and watch:

watch: {
  myvalueChanged: function(newVal, oldVal){
     console.log("%s %s", newVal, oldVal);
     // I want call created() again from here
  }
}

How can I do this?
P.S. I want to call created() again, not reload page (if its possible)

Answers:

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: data observation, computed properties, methods, watch/event callbacks.

so in your situation i think you need to use methods function. methods can be called whenever you want.

Example:

watch: {
  myvalueChanged: function(newVal, oldVal){
     this.testMethod(newVal, oldVal);
  }
}

methods: {
  testMethod: function (newVal, oldVal) {
     console.log("%s %s", newVal, oldVal);
  }
}

###

You can create a method and shift all of your on create jobs to newly created method.

Then call this method in created and watch method after change data.