【发布时间】:2017-06-03 05:07:01
【问题描述】:
我想要构建的是一个可以从其输入中检测错误的表单。输入在表单的一个部分中呈现(在我当前的设置中)。但工作几个小时后,它就不起作用了。
让这个概念发挥作用的最佳方法是什么?我应该为此使用插槽还是有其他方法?
这是我的代码:
//Form.vue
<template>
<form method="POST" action="/projects">
<slot></slot>
<div class="control">
<button class="button is-primary">Create</button>
</div>
</template>
<script>
import {FormHelper} from './classes/FormHelper.js';
export default {
/*
* The component's properties.
*/
props: {
fields: Object
},
data() {
return {
form: new FormHelper(this.fields) //this must be kwown in the Input.
};
},
}
</script>
//Input.vue
<template>
<div class="control">
<label for="name" class="label">{{label}}</label>
<input type="text" id="name" name="name" class="input">
<!--<span class="help is-danger" v-if="form.errors.has('name')" v-text="form.errors.get('name')"></span>-->
</div>
</template>
<script>
export default{
/*
* The component's properties.
*/
props: {
placeholder: String,
label: String,
name: String,
originalValue: String,
},
}
</script>
在浏览器中的实现:
<vue-form :fields="{'name': 'piet', 'description': 'arie'}">
<vue-input
label="The Name"
name="name"
></vue-input>
<vue-input
label="The Description"
name="description"
></vue-input>
</vue-form>
【问题讨论】:
-
组件的灵感来自:github.com/laracasts/Vue-Forms,他的目标是制作可重用的 Vue 组件。
标签: forms data-binding parent vue.js slots