vue.js - V-If 和 V-else 问题(Vue 和 Firebase 实时数据库)
问题描述
我正在使用 Vue 和 Firebase-Real-time Database 开发一个 Web 应用程序。在 Youtube 中的几个教程之后,我设法包含了所有 CRUD 功能。现在的问题是,当通过 v-if/v-else 更改用户的 {Edit:} 条件时,我无法显示或隐藏某个元素。下面是代码;
<template>
<div class="row">
<div class="container">
<table class="table table-striped">
<thead>
<tr>
<th>Name</th>
<th>Email</th>
<th>ID</th>
<th>Actions</th>
</tr>
</thead>
<tbody v-for="userProfile in Users" :key="userProfile.key">
<tr v-if="userProfile.edit">
<td>{{userProfile.username}}</td>
<td>{{userProfile.email}}</td>
<td>{{userProfile.userId}}</td>
<td>
<button @click.prevent="updateUser(userProfile.key)" class="btn btn-primary"> Update </button>
<button @click.prevent="removeUser(userProfile.key)" class="btn btn-danger">Delete</button>
</td>
</tr>
<tr v-else>
<td> <input type="text" v-model="userProfile.username"> </td>
<td> <input type="text" v-model="userProfile.email"> </td>
<td> <input type="text" v-model="userProfile.userId"> </td>
<td>
<button @click.prevent="saveUser(userProfile)" class="btn btn-primary"> Save </button>
<button @click.prevent="cancel(userProfile.key)" class="btn btn-danger"> Cancel </button>
</td>
</tr>
</tbody>
</table>
</div>
</div>
至于我的剧本
import { usersRef } from '../main.js';
export default {
data() {
return {
Users: [],
}
},
created() {
usersRef.once('value', (Users) => {
Users.forEach((doc) => {
this.Users.push({
key: doc.key,
email: doc.child('email').val(),
username: doc.child('username').val(),
userId: doc.child('userId').val(),
})
})
})
},
methods:{
removeUser: function(key) {
usersRef.child(key).remove()
.then(function() {
console.log("deleted Sucessfully");
alert("Sucessfully Deleted");
}).catch(function(error) {
console.log(error);
});
},
updateUser: function(key) {
usersRef.child(key).update({edit:false})
.then(function() {
console.log("update Sucessfully");
alert("Sucessfully Update");
}).catch(function(error) {
console.log(error);
});
},
cancel:function(key){
usersRef.child(key).update({edit:false})
},
saveUser:function(userProfile) {
const key = userProfile.key;
usersRef.child(key).set({username:userProfile.username,email:userProfile.email,userId:userProfile.userId,edit:true})
.then(function() {
console.log("Saved Sucessfully");
alert("Successfully Saved");
}).catch(function(error) {
console.log(error);
});
}
}
}
难道我做错了什么?提前致谢!
解决方案
推荐阅读
- javascript - 为什么放置一个 div 标签允许这个 React 代码编译?
- windows-installer - 拥有第三方 msi 的 ProductCode 和 UpgradeCode,如何在安装我的产品时强制卸载 msi
- r - 在 R 中打开和关闭多个 .csv 文件
- postgresql - 关于从 .NET CORE 中的 DB 加载支持的区域性
- javascript - 如何在函数完成后运行 return
- python - 如何修复 Pandas 中的解析错误读取 python 中的 csv?
- php - 对带有 ID 的产品发表评论
- java - java - 如何完全停止/终止已经提交给Java ExecutorService 的任务?
- python-3.x - 迭代 Pandas DataFrame 列中的列表
- vhdl - 在 VHDL 中使用 modelsim 编程加法器时出错