【发布时间】:2020-02-27 06:26:49
【问题描述】:
我的组件 Checkbox 对包含所需规则验证的未初始化表单(注册)正常工作。我继续编辑用户表单,但未能对其进行初始化。布尔属性已设置,但没有效果 - 未选中复选框。您可以在这里自己尝试:https://codesandbox.io/s/falling-tree-6g8qy。我已经用谷歌搜索过,有人建议将 v-model 设置为某些属性。但是我使用了一个带有 vee-validate 的组件,并且 v-model 需要发出一个信号。
复选框.vue
<template>
<ValidationProvider
tag="span"
v-model="innerValue"
:vid="vid"
:rules="rules"
:name="name || label"
v-slot="{ errors, required }"
>
<input :id="identifier" v-model="innerValue" :value="identifier" type="checkbox" ref="input">
<label :for="identifier">
<span>{{label}}</span>
</label>
</ValidationProvider>
</template>
<script>
import {ValidationProvider} from "vee-validate";
export default {
props: {
vid: {
type: String,
default: undefined
},
identifier: {
type: String,
default: undefined
},
name: {
type: String,
default: ""
},
label: {
type: String,
default: ""
},
rules: {
type: [Object, String],
default: ""
},
value: {
type: null,
default: ""
},
checked: {
type: Boolean,
default: false
}
},
components: {
ValidationProvider
},
data: () => ({
innerValue: null
}),
watch: {
innerValue(value) {
this.$emit("input", value);
}
}
};
</script>
store.js
export default new Vuex.Store({
actions: {
GET_USER_PROFILE_BY_ID: async (context, payload) => {
return {
driving: {
vehicles: ['car']
}
};
},
},
});
App.vue
<ValidationObserver ref="form" v-slot="{ passes, invalid }">
<form @submit.prevent="passes(submitForm)">
<label for="vehicle">Vehicles</label>
<Checkbox v-model="car" label="car" name="vehicle" identifier="car"/>
<Checkbox v-model="bus" label="bus" name="vehicle" identifier="bus"/>
<Checkbox v-model="van" label="van" name="vehicle" identifier="van"/>
<Checkbox v-model="truck" label="truck" name="vehicle" identifier="truck"/>
<div>
<button type="button" :disabled="invalid" @clicked="submitForm()">Submit</button>
</div>
</form>
</ValidationObserver>
<script>
export default {
name: "App",
components: {
Checkbox,
ValidationObserver
},
data: () => ({
car: null,
bus: null,
van: null,
truck: null,
error: null,
success: null
}),
created() {
this.getProfile(1);
},
methods: {
async getProfile(id) {
try {
const response = await this.$store.dispatch("GET_USER_PROFILE_BY_ID", {
id
});
console.log(response);
this.car = response.driving.vehicles.includes("car");
this.bus = response.driving.vehicles.includes("bus");
this.van = response.driving.vehicles.includes("van");
this.truck = response.driving.vehicles.includes("truck");
console.log(this.car);
} catch (err) {
console.log(err);
}
}
}
}
</script>
【问题讨论】:
-
您在最后一个片段中丢失了
<script>标签。使用块来指定代码的语言 - 语法着色有很大帮助。
标签: vue.js