Я знаю, что такие вопросы есть везде, но у меня проблема с поиском хорошего ответа (я новичок в VueJS). Я сожалею об этом.
Я пытаюсь передать данные с помощью vform.
То, что я пытаюсь сделать, это добавить данные пользователя через vform в таблицу.
Вот мой код Users.vue
<div class = "modal fade" id = "addNew" tabindex = "-1" role = "dialog" aria-labelledby = "addNew" aria-hidden = "true">
<div class = "modal-dialog modal-dialog-centered" role = "document">
<div class = "modal-content">
<div class = "modal-header">
<h5 class = "modal-title" id = "exampleModalLabel">Add New</h5>
<button type = "button" class = "close" data-dismiss = "modal" aria-label = "Close">
<span aria-hidden = "true">×</span>
</button>
</div>
<div class = "modal-body">
<div class = "form-group">
<input v-model = "form.name" type = "text" name = "name"
class = "form-control" :class = "{ 'is-invalid': form.errors.has('name') }">
<has-error :form = "form" field = "name"></has-error>
</div>
</div>
<div class = "modal-footer">
<button type = "button" class = "btn btn-danger" data-dismiss = "modal">Close</button>
<button type = "submit" class = "btn btn-primary">Save changes</button>
</div>
</div>
</div>
</div>
<script>
export default {
data(){
return{
form: new Form({
name: ''
})
}
},
}
</script>
Вот мой app.js
require('./bootstrap');
window.Vue = require('vue');
import { Form, HasError, AlertError } from 'vform';
window.form = Form;
Vue.component(HasError.name, HasError)
Vue.component(AlertError.name, AlertError)
import VueRouter from 'vue-router'
Vue.use(VueRouter)
let routes = [
{ path: '/dashboard', component: require('./components/Dashboard.vue') },
{ path: '/profile', component: require('./components/Profile.vue') },
{ path: '/users', component: require('./components/Users.vue') }
]
const router = new VueRouter({
mode: 'history',
routes // short for `routes: routes`
})
Ваша проблема, скорее всего, связана с этим:
<input v-model = "form.name" type = "text" name = "name"
Вы можете видеть, что это единственное место, где называется .name
. Это означает, что ваш объект form
является undefined
, и код пытается вызвать атрибут name
для него, что выдает вам ошибку.
Если вы посмотрите на vform README, вы увидите, что оператор import
на самом деле находится внутри компонента. Проблема с вашим кодом заключается в том, что импорт внутри app.js
волшебным образом не появляется внутри вашего компонента, поэтому вы не можете сделать new Form()
в компоненте Vue. Хотя вы назначаете его объекту window
в app.js
, вы не используете его повторно позже, и IMO, лучше явно импортировать свои зависимости.
Итак, в заключение, что вам нужно сделать, это: добавить этот оператор импорта сразу после открывающего тега <script>
в вашем компоненте Vue.
import { Form } from 'vform';
Джастин Хо Туан Дуонг Большое спасибо за ваше время и ответ.