我有一个API(流明)抛出的这个对象错误
{
"title":["The title field is required."],
"body":["The body field is required."],
"days_after":["The days after field is required."]
}
然后我将此错误分配给mess
变量,我试图将此错误输出为具有此代码的单个警报
<v-alert v-for="me in mess" v-if="mess" error dismissible v-model="mess">
{{me[0]}}
</v-alert>
然而,当我解除1个错误时,所有这些都被解雇/删除
答案 0 :(得分:3)
donMateo 有一个很好的解决方案,但是如果您的意图是解雇消息会清除来自errors对象的消息,那么您实际上可以这样做(某种程度上)。您只需要v-model
单个行,而不是整个mess
对象。 (另外,您can't v-model
an alias因此不能v-model="me"
。)
new Vue({
el: '#app',
data() {
return {
mess: {
"title": ["The title field is required."],
"body": ["The body field is required."],
"days_after": ["The days after field is required."]
}
}
}
});
<script src="//cdnjs.cloudflare.com/ajax/libs/vue/2.5.0/vue.min.js"></script>
<script src="//unpkg.com/vuetify@0.17.6/dist/vuetify.min.js"></script>
<link href="https://fonts.googleapis.com/css?family=Roboto:300,400,500,700|Material+Icons" rel="stylesheet" />
<link href="//unpkg.com/vuetify@0.17.6/dist/vuetify.min.css" rel="stylesheet" />
<div id="app">
<v-app>
<div>
<v-alert v-for="me, index in mess" color="info" icon="info" dismissible v-model="mess[index]">
{{me[0]}}
</v-alert>
{{JSON.stringify(mess)}}
</div>
</v-app>
</div>
答案 1 :(得分:2)
v-model绑定负责警报的可见性,因此您需要一个布尔属性。让我们稍微改变一下数据的结构:
new Vue({
el: '#app',
data () {
return {
errors: [
{text: "The title field is required.", show:true},
{text: "The body field is required.", show:true},
{text: "The days after field is required.", show:true}
]
}
}
})
<div id="app">
<v-app>
<div>
<v-alert v-for="me in errors" color="info" icon="info" dismissible v-model="me.show">
{{me.text}}
</v-alert>
</div>
</v-app>
</div>
瞧,瞧! CodePen demo