1

after several attempts to find a solution here I decided to post it.

I am a beginner and using laravel + vue.js + mysql

Approach

I fetch data via an Axios call and update the data within the vue instance. Then I would like to show this data and update the DOM. I use mounted() to get the data and update the array "users".

Problem

I am able to get the data, update the array (when console logging), however I am not able to show it in the DOM. It wont update. What am I missing?

My vue component:

<template>
  <div class="container">
    <form class="form" v-on:submit.prevent="submituser">
      <div class="row">
        <label id="username">Username</label>
        <input type="text" v-model="username" name="username" />
      </div>
      <div class="row">
        <label id="password">Password</label>
        <input type="password" name="password" v-model="password" v-on:keyup="CheckPrint" />
      </div>
      <div class="row">
        <button type="submit" class="mt-2 btn btn-primary">Sign up</button>
      </div>
    </form>
    <h2>All Users</h2>
    <p>{{users}}</p>
    <ul>
      <li v-for="user in users" :key="user.name">{{ user.name }}</li>
    </ul>
  </div>
</template>

<script>
import axios from "axios";

export default {
  data: function() {
    return {
      password: "",
      username: "",
      users: []
    };
  },

  mounted() {
    var vm = this;
    axios.get("api/user").then(function(response) {
      vm.users = response.data;
      console.log(vm.users, "User updated");
    });
  }
};
</script>

My app.js component:

/**
 * First we will load all of this project's JavaScript dependencies which
 * includes Vue and other libraries. It is a great starting point when
 * building robust, powerful web applications using Vue and Laravel.
 */

require("./bootstrap");

window.Vue = require("vue");

/**
 * Next, we will create a fresh Vue application instance and attach it to
 * the page. Then, you may begin adding components to this application
 * or customize the JavaScript scaffolding to fit your unique needs.
 */
import Vue from "vue";
import Vuex from "vuex";
import store from "../store/store";

Vue.use(Vuex);

Vue.component("Signup", require("./components/User/Signup.vue"));

const app = new Vue({
  el: "#app",
  store
});

Thanks a lot for your help. I am sure its an easy one for most of you!

3

1 Answer 1

0

If you're replacing the entire Array with another entire Array, it should work properly.

But analysing your code, I've figured that, maybe, you don't need to use that var vm = this in this case... I'd try to use arrow function in that response function and try using only this.users = response.data to see if it works.

i'd like to put this in a comment but i'm a new user too.

1
  • Thanks but this is what I already tried. I did take a look at the $set function however I am still not getting it fully and it still did not work. Commented Dec 11, 2019 at 17:56

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.