if (node.data.type !== 'category' || node.data.type !== 'topic') { createMessage.error(errorText); return; }优化语句
时间: 2024-02-02 20:03:13 浏览: 101
可以将逻辑运算符 `||` 改为逻辑运算符 `&&`,并将两个条件的逻辑取反,即:
```
if (!(node.data.type === 'category' && node.data.type === 'topic')) {
createMessage.error(errorText);
return;
}
```
这样,只有当 `node.data.type` 同时不是 `'category'` 和 `'topic'` 时,才会显示错误信息。这样可以避免原先的逻辑错误,同时也使代码更加清晰易懂。
相关问题
if (node.data.type === 'category' || node.data.type === 'topic') { if (node.children.length) { // 服务节点移动,且被移动的节点成为子节点,node为其父节点:传当前id、结束时的父节点id和下一个id payload.afterId = node.children[0]?.key; payload.parentId = node.data.id; } else { // 当node中的children没有值时,表示拖拽结束时(拖动至另一个分类)的分类节点中没有服务节点 // 此时当前被拖拽的节点成为其唯一一个节点,此时只需传值父节点 payload.parentId = node.data.id; } } else { createMessage.error(errorText); return; }优化
可以使用逻辑运算符 `&&` 将两个 if 语句合并为一个 if 语句,从而避免代码重复,例如:
```
if (node.data.type !== 'category' && node.data.type !== 'topic') {
createMessage.error(errorText);
return;
}
if (node.children.length) {
// 服务节点移动,且被移动的节点成为子节点,node为其父节点:传当前id、结束时的父节点id和下一个id
payload.afterId = node.children[0]?.key;
payload.parentId = node.data.id;
} else {
// 当node中的children没有值时,表示拖拽结束时(拖动至另一个分类)的分类节点中没有服务节点
// 此时当前被拖拽的节点成为其唯一一个节点,此时只需传值父节点
payload.parentId = node.data.id;
}
```
这样,当 `node.data.type` 不是 `'category'` 或 `'topic'` 时,就会显示错误信息并直接返回,否则就会执行后续的代码逻辑。这样可以使代码更加简洁明了,也可以避免代码重复。
报错Invalid prop: type check failed for prop "loading". Expected Boolean, got Array
抱歉,我在之前的代码中犯了一个错误。在List组件的props中,`loading`应该是一个Boolean类型的属性,而不是一个Array类型的属性。请使用以下修正后的代码:
```vue
<template>
<div class="list-container">
<van-list
v-model="list"
:finished="finished"
:error="error"
:offset="offset"
:loading="loading"
:loading-text="loadingText"
:finished-text="finishedText"
:error-text="errorText"
:immediate-check="immediateCheck"
:direction="direction"
@load="onLoad"
>
<template v-slot:item="{ item }">
<!-- 这里是列表项的展示内容 -->
<div class="list-item">{{ item }}</div>
</template>
</van-list>
</div>
</template>
<script>
export default {
name: 'List',
props: {
value: {
type: Array,
default: () => []
},
finished: {
type: Boolean,
default: false
},
error: {
type: Boolean,
default: false
},
offset: {
type: Number,
default: 300
},
loading: {
type: Boolean,
default: false
},
loadingText: {
type: String,
default: '加载中...'
},
finishedText: {
type: String,
default: '没有更多了'
},
errorText: {
type: String,
default: '加载失败,请重试'
},
immediateCheck: {
type: Boolean,
default: true
},
direction: {
type: String,
default: 'down'
}
},
data() {
return {
list: this.value
}
},
methods: {
onLoad() {
// 触发load事件,外部可以监听该事件进行数据加载
this.$emit('load')
}
},
watch: {
value(newVal) {
this.list = newVal
}
}
}
</script>
<style scoped>
/* 这里是组件的样式 */
.list-container {
/* 样式代码 */
}
.list-item {
/* 样式代码 */
}
</style>
```
请将上述修正后的代码替换掉之前的List组件代码,并在使用案例中相应地修改props的名称为`loading`。
非常抱歉给你带来了困扰,希望这次的修正能够解决问题。如果还有其他疑问,请随时提出。