我通常做的是对要发布的数据进行包装,用于处理 api 请求并存储错误。这样,您的用户对象可以将错误记录在其自身上,如果存在任何错误,您可以在组件中使用它们。
例如:
import { fetchUsers } from '@\Common\api'
import Form from '@\Utils\Form'
const state = {
isProcessing: false,
form: new Form({
users: null
})
}
const mutations = {
setIsProcessing(state, value) {
state.isProcessing = value
},
updateForm(state, [field, value]) {
state.form[field] = value
}
}
const actions = {
async fetchUsers ({ state: { form }, commit }) {
let users = null
commit('setIsProcessing', true)
try {
users = await form.get(fetchUsers);
} catch (err) {
// - handle error
}
commit('updateForm', ['users', users])
commit('setIsProcessing', false)
}
}
export default {
namespaced: true,
state,
mutations,
actions
}
然后在组件中,您可以像这样使用包装器上的错误对象:
<template>
<div>
<div class="error" v-if="form.erros.has('users')">
{{ form.errors.get('users') }}
</div>
<ul v-if="users">
<li v-for="user in users" :key="user.id">{{ user.username }}</li>
</ul>
</div>
</template>
<script>
import { mapState } from 'vuex'
export default {
computed: {
...mapState('module' ['form']),
users () {
return this.form.users
}
}
</script>
这只是我个人的方法,我觉得它非常方便,到目前为止它对我很有帮助。不知道是否有任何标准模式,或者是否有明确的“正确方法”来做到这一点。
我喜欢包装方法,因为当来自 api 的响应返回错误时,您的错误会自动响应。
您可以在 vuex 之外重新使用它,甚至可以更进一步,将错误注入到充当包装器组件的预定义错误边界中,并使用提供/注入方法将错误数据沿组件树传播并显示在哪里您是否需要他们出现。
这是错误边界组件的示例:
<template>
<div>
<slot></slot>
</div>
</template>
<script>
export default {
props: {
module: {
type: String,
required: true,
validator: function (value) {
return ['module1', 'module2'].indexOf(value) !== -1
}
},
form: {
type: String,
default: 'form'
}
},
provide () {
return {
errors: this.$store.state[this.module][this.form].errors
}
}
}
</script>
包装应该收到错误的应用程序的某些部分:
<template>
<div id="app">
<error-boundary :module="module1">
<router-view/>
</error-boundary>
</div>
</template>
然后您可以在子组件中使用用户包装器中的错误,如下所示:
如果你有一个全局错误,比如没有来自 api 的响应,并希望在侧边栏中显示它
<template>
<div id="sidebar">
<div v-if="errors.has('global')" class="error">
{{ errors.get('global').first() }}
</div>
...
</div>
</template>
<script>
export default {
inject: [
'errors'
],
...
}
</script>
并且相同的错误对象在小部件内的某处被重新使用以用于用户对象验证的错误:
<template>
<div id="user-list">
<div v-if="errors.has('users')" class="error">
{{ errors.get('users').first() }}
</div>
...
</div>
</template>
<script>
export default {
inject: [
'errors'
],
...
}
</script>
Jeffrey Way 不久前在 Vue2 上做了一个系列,他提出了类似的建议。以下是关于可以构建的 Form 和 Error 对象的建议:https://github.com/laracasts/Vue-Forms/blob/master/public/js/app.js