vue.js - 在 axios 调用时,DOM 不显示 vue.js 和 laravel 中数组的更新数据
问题描述
经过几次尝试在这里找到解决方案后,我决定发布它。
我是初学者,使用 laravel + vue.js + mysql
方法
我通过 Axios 调用获取数据并更新 vue 实例中的数据。然后我想显示这些数据并更新 DOM。我使用mounted() 来获取数据并更新数组“用户”。
问题
我能够获取数据、更新数组(在控制台记录时),但是我无法在 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
});
非常感谢你的帮助。我相信这对你们大多数人来说很容易!
解决方案
如果您将整个 Array 替换为另一个完整的 Array,它应该可以正常工作。
但是分析您的代码,我发现,也许,在这种情况下您不需要使用它var vm = this
......我会尝试在该响应函数中使用箭头函数并尝试仅使用this.users = response.data
以查看它是否有效。
我想将此放在评论中,但我也是新用户。
推荐阅读
- html - 不使用 CSS 浮动复制 html 定位
- javascript - 如何制作密码认证程序
- omnet++ - 在 OMNeT++ 中使用 INET 进行仿真时出错
- java - 如何让我的 While 语句正确循环和中断,同时在 java 中输入另一个选项?
- python - python: fork 没有外部命令,分别捕获 stdout 和 stderr
- c++ - C++:成员的外线声明必须是纯虚函数的定义错误
- ruby-on-rails - 如何使用 has_and_belongs_to_many 获得自定义变形类
- java - Alluxio Web UI 中的“服务器配置检查”为 WARN
- javascript - Google API 地址字段的量角器测试
- windows - 在 Win10 上创建快捷方式失败 accessdenied