为什么不让我访问其他的JavaScript函数Vue Laravel?

huangapple go评论65阅读模式
英文:

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'))
}

huangapple
  • 本文由 发表于 2023年5月25日 14:39:44
  • 转载请务必保留本文链接:https://go.coder-hub.com/76329526.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定