英文:
Why doesn't let me access to the other javascript functions Vue Laravel
问题
"error.js"
export default class Errors {
constructor(){
this.errors = {};
}
has(field){
return this.errors.hasOwnProperty(field);
}
any(){
return Object.keys(this.errors).length > 0;
}
get(field){
if (this.errors[field]) {
return this.errors[field][0];
}
}
record(errors){
this.errors = errors;
}
clear(field){
delete this.errors[field];
}
}
"My form component"
import Errors from '../../errors/error.js';
export default{
data() {
return {
edit: false,
formContact: [],
errors: new Errors(),
error: []
};
},
methods: {
updateForm(){
axios.put(route('contact.update', this.contact['id']), this.formContact)
.then((res) => {
this.errors = []
console.log(res)
})
.catch((err) => {
if(err.response.status === 422){
this.errors.record(err.response.data.errors || {});
console.log(this.errors.get('linkedin')) //not work
}
})
}
}
}
Message error: caught (in promise) TypeError: _this3.errors.get is not a function
I hope to be able to make the Errors class work outside of my form component so that it can be refactored.
英文:
I found a code on the internet that used a class that did not say if Vue or JS so I put it to test, the problem is that it does not let me enter any function importing the class and nothing.
I thought it would be a good way to refactor the error messages that come from the backend to the component inside Laravel that is the form.
error.js
export default class Errors {
constructor(){
this.errors = {};
}
has(field){
return this.errors.hasOwnProperty(field);
}
any(){
return Object.keys(this.errors).length > 0;
}
get(field){
if (this.errors[field]) {
return this.errors[field][0];
}
}
record(errors){
this.errors = errors;
}
clear(field){
delete this.errors[field];
}
}
My form component
import Errors from '../../errors/error.js'
export default{
data() {
return {
edit: false,
formContact: [],
errors: new Errors(),
error: []
};
},
methods: {
updateForm(){
axios.put(route('contact.update',this.contact['id']),this.formContact)
.then((res) => {
this.errors = []
console.log(res)
})
.catch((err) => {
if(err.response.status === 422){
this.errors = err.response.data.errors || {}
console.log(this.errors.get('linkedin')) //not work
}
})
}
}
Message error: caught (in promise) TypeError: _this3.errors.get is not a function
I hope to be able to make the Errors class work outside of my form component so that it can be refactored.
答案1
得分: 0
You're defaulting this.errors
to be a new Errors
instance, but then you're overwriting this instance in your axios callback. this.errors = err.response.data.errors || {}
is overwriting your Errors
instance, not updating it.
你将this.errors
默认为一个new Errors
实例,但在axios回调中又覆盖了这个实例。this.errors = err.response.data.errors || {}
覆盖了你的Errors
实例,而不是更新它。
You'll want to use the record()
method on your Errors
instance to update your Errors
instance with the set of errors that came back from the request.
你需要使用Errors
实例上的record()
方法,将从请求返回的错误集合更新到你的Errors
实例中。
if (err.response.status === 422) {
this.errors.record(err.response.data.errors || {})
console.log(this.errors.get('linkedin'))
}
英文:
You're defaulting this.errors
to be a new Errors
instance, but then you're overwriting this instance in your axios callback. this.errors = err.response.data.errors || {}
is overwriting your Errors
instance, not updating it.
You'll want to use the record()
method on your Errors
instance to update your Errors
instance with the set of errors that came back from the request.
if (err.response.status === 422) {
this.errors.record(err.response.data.errors || {})
console.log(this.errors.get('linkedin'))
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论