【问题标题】:Unexpected identifier on function creation - js函数创建时出现意外标识符 - js
【发布时间】:2020-06-23 09:08:49
【问题描述】:

在js中创建函数时,一旦创建函数,它就会给我标题中所述的错误。它还说';'预期,但是,我找不到问题。该函数是“created()”,我故意将其留空,因为它一有它就会给出错误。

代码如下:

const app = new Vue({
    el: '#app',
    data: {
        errors: [],
        name: null,
        age: null
    },
    methods:{
        checkForm: function (e) {
            if (this.name && this.age) {
                console.log(this.name);
                return true;
            }
            

            console.log(this.name);

            async created() {

            }

            this.errors = [];

            if (!this.name) {
                this.errors.push('Name required.');
            }
            if (!this.age) {
                this.errors.push('Age required.');
            }

            e.preventDefault();
        }
    }
})

【问题讨论】:

  • async created() {} 是做什么的?语法无效
  • async created() { } -> async function created() { } 或完全删除。
  • 我整理出来了。这是我在浏览器中的新错误:CORS 策略已阻止 from origin 'null':请求的资源上不存在 'Access-Control-Allow-Origin' 标头。如果不透明的响应满足您的需求,请将请求的模式设置为“no-cors”以获取禁用 CORS 的资源。有什么帮助吗?

标签: javascript node.js vue.js


【解决方案1】:

那是无效的。 created() 是 vue 自己的钩子,应该在 methods 之外。

const app = new Vue({
  el: "#app",
  data: {
    errors: [],
    name: null,
    age: null
  },
  methods: {
    checkForm: function(e) {
      if (this.name && this.age) {
        console.log(this.name);
        return true;
      }

      console.log(this.name);

      this.errors = [];

      if (!this.name) {
        this.errors.push("Name required.");
      }
      if (!this.age) {
        this.errors.push("Age required.");
      }

      e.preventDefault();
    }
  },
  async created() {
    console.log("instance created");
  }
});

【讨论】:

猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2017-09-03
  • 1970-01-01
  • 2021-06-01
  • 1970-01-01
  • 2019-02-01
  • 2021-02-20
  • 1970-01-01
相关资源
最近更新 更多