经过几次尝试在这里找到解决方案后,我决定将其发布。
我是初学者,正在使用laravel + vue.js + mysql
方法
我通过Axios调用获取数据并在vue实例中更新数据。然后,我想显示此数据并更新DOM。我使用mount()获取数据并更新数组“用户”。
问题
我能够获取数据,更新数组(在控制台记录日志时),但是我无法在DOM中显示它。它不会更新。我想念什么?
我的Vue组件:
<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>
我的app.js组件:
/**
* 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
});
非常感谢您的帮助。我相信这对大多数人来说都是一件容易的事!
如果您要用另一个整个阵列替换整个阵列,它应该可以正常工作。
但是分析您的代码后,我发现也许在这种情况下您不需要使用var vm = this
...我将尝试在该响应函数中使用箭头函数,并尝试仅使用this.users = response.data
来查看其是否有效。
我想在评论中写这个,但我也是新用户。