Javascript 函数创建时出现意外标识符-js
在js中创建函数时,我一创建函数,它就会给出标题中所述的错误。它还写着“;”但是,我无法找到问题所在。函数是“created()”,我故意将其留空,因为它会在我得到它时立即给出错误 代码如下:Javascript 函数创建时出现意外标识符-js,javascript,node.js,vue.js,Javascript,Node.js,Vue.js,在js中创建函数时,我一创建函数,它就会给出标题中所述的错误。它还写着“;”但是,我无法找到问题所在。函数是“created()”,我故意将其留空,因为它会在我得到它时立即给出错误 代码如下: const app = new Vue({ el: '#app', data: { errors: [], name: null, age: null }, methods:{ checkForm: funct
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();
}
}
})
那是无效的
created()
是vue自己的钩子,应该在方法之外
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");
}
});
async created(){}
做什么?这是无效语法async created(){}
->async function created(){}
或将其全部删除。我已将其整理好。以下是我在浏览器中的新错误:源“null”已被CORS策略阻止:请求的资源上不存在“Access Control Allow origin”头。如果不透明响应满足您的需要,请将请求的模式设置为“no cors”,以获取禁用cors的资源。有什么帮助吗?这里有更多类似于beforeCreate
,mounted
等。请看@NiklasE。