Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/395.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript TypeError:无法读取未定义的属性“$emit”-VueJS_Javascript_Authentication_Vue.js - Fatal编程技术网

Javascript TypeError:无法读取未定义的属性“$emit”-VueJS

Javascript TypeError:无法读取未定义的属性“$emit”-VueJS,javascript,authentication,vue.js,Javascript,Authentication,Vue.js,我正在尝试在vuejs中实现一个简单的身份验证。我有一个拥有真实用户名和密码的对象列表。我正在迭代这个列表,并使用输入的用户名和密码进行检查。如果存在匹配项,则我将发出一个事件并更新已验证的变量。但是问题是在forEarch循环中的登录中,我无法访问emit 这是我的Login.vue文件 登录 提交 导出默认值{ 名称:'登录', 资料{ 返回{ 输入:{ 用户名:, 密码: } } }, 方法:{ 登录{ var enteredUsername=this.input.username; v

我正在尝试在vuejs中实现一个简单的身份验证。我有一个拥有真实用户名和密码的对象列表。我正在迭代这个列表,并使用输入的用户名和密码进行检查。如果存在匹配项,则我将发出一个事件并更新已验证的变量。但是问题是在forEarch循环中的登录中,我无法访问emit

这是我的Login.vue文件

登录 提交 导出默认值{ 名称:'登录', 资料{ 返回{ 输入:{ 用户名:, 密码: } } }, 方法:{ 登录{ var enteredUsername=this.input.username; var enteredPassword=this.input.password; ifenteredUsername!=&&entereduserpassword!=={ 此.$parent.mockAccount.forEachfunction元素{ 如果enteredUsername===element.username&&entereduserpassword===element.password{ 这是真的 此.$router.replace{name:secure} } } } } } } 登录{ 宽度:500px; 边框:1px实心中交; 背景色:FFFFFF; 保证金:自动; 利润上限:200px; 填充:20px; }
ES5函数有自己的这一点,所以要改变

this.$parent.mockAccount.forEach(function (element) {
  if (enteredUsername === element.username && enteredPassword === element.password) {
    this.$emit("authenticated", true)
    this.$router.replace({name: "secure"})
  }
})
或者是一个ES6箭头函数,该函数与它们在其中定义的上下文相同

this.$parent.mockAccount.forEach((element) => {
  if (enteredUsername === element.username && enteredPassword === element.password) {
    this.$emit("authenticated", true)
    this.$router.replace({name: "secure"})
  }
})
或者对Function.prototype.bind ES5使用显式绑定:

或使用闭包:

const self = this;
this.$parent.mockAccount.forEach(function (element) {
  if (enteredUsername === element.username && enteredPassword === element.password) {
    self.$emit("authenticated", true)
    self.$router.replace({name: "secure"})
  }
})

ES5函数有自己的这一点,所以要改变

this.$parent.mockAccount.forEach(function (element) {
  if (enteredUsername === element.username && enteredPassword === element.password) {
    this.$emit("authenticated", true)
    this.$router.replace({name: "secure"})
  }
})
或者是一个ES6箭头函数,该函数与它们在其中定义的上下文相同

this.$parent.mockAccount.forEach((element) => {
  if (enteredUsername === element.username && enteredPassword === element.password) {
    this.$emit("authenticated", true)
    this.$router.replace({name: "secure"})
  }
})
或者对Function.prototype.bind ES5使用显式绑定:

或使用闭包:

const self = this;
this.$parent.mockAccount.forEach(function (element) {
  if (enteredUsername === element.username && enteredPassword === element.password) {
    self.$emit("authenticated", true)
    self.$router.replace({name: "secure"})
  }
})

14小时后对给出的答案没有反馈?效果很好。非常感谢你的帮助!!14小时后对给出的答案没有反馈?效果很好。非常感谢你的帮助!!