【问题标题】:Uncaught ReferenceError: "xxx" is not defined (VUE)Uncaught ReferenceError: "xxx" is not defined (VUE)
【发布时间】:2021-06-16 20:11:20
【问题描述】:

Vue 新手,试图弄清楚一些事情。 我正在使用这个“数据”:

data() {
        return {
            currentPage: 0,
            nextPage: '',
            previousPage: '',
            left: '',
            opacity: '',
            scale: '',
        }
    },

尝试在方法中使用数据变量如下所示:

methods: {
        isStageOneDone: function () {
            var animating;
            if(animating) return false;
            animating = true;
            this.currentPage;
            console.log("CurrentPage =>", currentPage);
   }
}

但我不断收到此错误:

Uncaught ReferenceError: currentPage is not defined

我错过了什么?我查看了 Vue 文档,我认为似乎还可以

编辑: 有没有可能是因为return()而发生错误?

【问题讨论】:

  • 括号必须包裹在返回的对象周围,否则将无法正确解释。 (编辑:这是因为return { 将被解释为return {;,因为所谓的Automatic Semicolon Insertion。有关更多信息,请参阅stackoverflow.com/questions/8528557/…
  • 我不明白你的意思,对不起
  • 函数中的代码没有多大意义。为了让任何人都能够帮助您,我们至少需要了解您想要实现的目标。作为旁注,您的代码确实尝试使用尚未在您的函数范围内定义的变量(currentPage)(因为this.currentPage 不是currentPage)。但是,即使您在使用它之前确实定义了它,您也只会删除错误,它不会做任何事情。那么,您想用该代码做什么?

标签: javascript vue.js


【解决方案1】:

您必须添加“this”键才能访问定义的内部数据属性。例如:

data(){
return{
  variable:'example'
}
},
methods:{
  
  exampleFunc(){
  
  
  return this.variable;
  }
}

【讨论】:

  • 嘿,我想知道你能不能帮我做点别的事情?
【解决方案2】:

您好,您应该像下面这样定义您的方法

methods: {
 isStageOneDone(path, data) {
        var animating;
        if(animating) return false;
        animating = true;
        this.currentPage;
        console.log("CurrentPage =>", this.currentPage);
 }
}

让我们尝试一下,我认为它对你有用

【讨论】:

  • 是的,因为您忘记在 currentPage 之前提及“this”
  • 和我一样,我更新了答案,请查看
  • 哦,坏习惯..我会学习这个功能
猜你喜欢
  • 2023-03-30
  • 1970-01-01
  • 2020-05-05
  • 2015-12-24
  • 1970-01-01
  • 1970-01-01
  • 2021-08-24
  • 2021-12-08
  • 2019-01-22
相关资源
最近更新 更多