我在vuejs 3中遇到错误
我有这个列表items = ['alimentos', 'licores', 'varios']
生成对象列表
return {
category: item,
selected:false
}
});
// render withe v-for in template
<tr v-for="(category,idx) in categories"
:key="category"
@click="category.selected = !category selected">
<td>{{ idx + 1 }}</td>
<td class="text-start">{{ category.category }}</td>
<td class="text-success">
<i class="fa-solid fa-minus text-danger" v-if="category.selected"></i>
<i class="fa-solid fa-plus" v-else></i>
<td>
</tr>
//当我点击列表中的一个项目时,在浏览器中我得到错误
[Vue warn]: Unhandled error during execution of scheduler flush.
ERROR
parent is null
insert@webpack-internal:///./node_modules/@vue/runtime-dom/dist/runtime-dom.esm-bundler.js:171:5
mountElement@webpack-internal:///./node_modules/@vue/runtime-core/dist/runtime-core.esm-bundler.js:4755:15
processElement@webpack-internal:///./node_modules/@vue/runtime-core/dist/runtime-core.esm-bundler.js:4700:19
patch@webpack-internal:///./node_modules/@vue/runtime-core/dist/runtime-core.esm-bundler.js:4628:25
patchBlockChildren@webpack-internal:///./node_modules/@vue/runtime-core/dist/runtime-core.esm-bundler.js:4881:12
patchElement@webpack-internal:///./node_modules/@vue/runtime-core/dist/runtime-core.esm-bundler.js:4816:25
processElement@webpack-internal:///./node_modules/@vue/runtime-core/dist/runtime-core.esm-bundler.js:4702:19
patch@webpack-internal:///./node_modules/@vue/runtime-core/dist/runtime-core.esm-bundler.js:4628:25
patchKeyedChildren@webpack-internal:///./node_modules/@vue/runtime-core/dist/runtime-core.esm-bundler.j
我已经检查过了,我注意到该项目的选定值确实根据大小写而更改为true或false,但它并没有在浏览器中显示更改
如果我在Vue检查器中更改值,则不会出现错误,并且在显示模板时会显示更改后的值,但如果在模板可见的情况下完成更改,则会抛出错误,我认为问题在于v-for
完全代码组件
<template>
<div class="row mt-2">
<h5 class="mt-2">ASISTENTES DE TOMA</h5>
<div class="col bg-light">
<div class="row">
<div class="col-8 mt-2">
<input type="text" v-model="query" class="form-control" placeholder="Buscar" @keyup="filterUsers">
<table class="table table-bordered table-hover table-condesed mi_table">
<thead>
<tr>
<th>#</th>
<th>Nombre</th>
<th>Username</th>
<th><i class="fas fa-cogs"></i></th>
</tr>
</thead>
<tbody>
<tr v-for="(user, idx) in all_teams" :key="user" @click="selectUser(user)">
<td class="text-center">{{ idx+1 }}</td>
<td class="text-start">{{ user.last_name }} {{ user.first_name }}</td>
<td class="text-start">{{ user.username }}</td>
<td class="text-success"><i class="fa-solid fa-plus"></i></td>
</tr>
</tbody>
</table>
</div>
<div class="col-4 mt-2">
<div class="card">
<div class="card-body">
<h5 class="card-title">Usuarios Seleccionados</h5>
<p class="card-text">
<ul class="list-group text-start" v-for="user in selected_teams" :key="user.username">
<li class="list-group-item p-1" @click="selectUser(user,true)">
<i class="fa-solid fa-minus text-danger"></i>
{{ user.last_name }} {{ user.first_name }}
<small class="badge bg-light text-secondary border">{{ user.username }}</small>
</li>
</ul>
</p>
</div>
</div>
<br />
</div>
</div>
<div class="row mt-3">
<div class="col text-start">
<button class="btn btn-primary btn-sm" @click="showView(2)">
<i class="fa-solid fa-chevron-left"></i> Anterior
</button>
</div>
<div class="col text-end">
<button class="btn btn-success btn-sm" @click="showView(4)">
Siguiente<i class="fa-solid fa-chevron-right"></i>
</button>
</div>
</div>
<br />
</div>
</div>
</template>
<script>
export default {
name: "TeamsTab",
emits: ["showView"],
props: {
teams: {
type: Object,
required: true,
},
},data(){
return{
query:"",
all_teams: [],
selected_teams: [],
}
},methods:{
filterUsers(){
// tomamos todos los usuarios
const users = this.teams.map(item=>item);
// quitamos los seleccionados
this.all_teams = users.filter(
item=>!this.selected_teams.includes(item)
);
// aplicamos el filtro
if (this.query.length > 3){
this.all_teams = this.all_teams.filter((item)=>{
let params = this.query.toLowerCase();
let target = item.first_name + ' ' + item.last_name;
return target.toLowerCase().includes(params);
}
);
return;
}
console.log('llgeamos a la fila');
},
selectUser(user, delete_usr=false){
// agregamos un usuario a los seleccionados
if (delete_usr) {
this.selected_teams = this.selected_teams.filter(
item => item.username !== user.username
);
return;
}
this.selected_teams.push(user);
this.filterUsers();
},showView(view) {
this.$emit("showView", view);
},
},
mounted(){
// cargar todos los usuarios asistentes
this.filterUsers();
},
};
</script>
2条答案
按热度按时间zlwx9yxi1#
您有一个错误:
应该是
xiozqbni2#
问题出在v-if,但我不明白为什么会出现问题,当我不使用v-if时,错误不显示。我认为在进行更改时,变量仍然为null,从而抛出错误
可能解