Javascript 无法读取未定义的属性变量。哪里有错误?

Javascript 无法读取未定义的属性变量。哪里有错误?,javascript,vue.js,Javascript,Vue.js,我试图从另一个站点获取数据并将其添加到阵列中,之后我想在select中使用数据,但出现错误: TypeError:无法读取未定义的属性“locationList” 我在创建的服务器上运行函数 created() { this.getLocations() } 通过fetch从另一个站点获取数据的简单函数 methods: { getLocations: function() { fetch('https://www.webpagetest.org/getLoca

我试图从另一个站点获取数据并将其添加到阵列中,之后我想在select中使用数据,但出现错误:

TypeError:无法读取未定义的属性“locationList”

我在创建的服务器上运行函数

created() {
    this.getLocations()
}
通过fetch从另一个站点获取数据的简单函数

methods: {
    getLocations: function() {
        fetch('https://www.webpagetest.org/getLocations.php?f=json')
            .then(function(response) {
                if (response.status === 200) {
                    return response.json()
                } else {
                    throw response;
                }
            })
            .then(function(data) {
                var list = Object.entries(data.data).map(([k, v]) => ({
                    text: v.Label,
                    value: k
                }))
                this.locationList.push(list);
            })
            .catch(function(e) {
                console.log(e)
            });
    }
}

我的错在哪里?怎样才能正确?

这似乎是
这个问题的背景

getLocations()
中,尝试在
fetch
之前添加此行:

让vm=this

然后在
fetch
函数中使用:

vm.locationList.push(列表)而不是
此.locationList.push(列表)


希望能有帮助。祝你好运。

这似乎是
这个问题的背景

getLocations()
中,尝试在
fetch
之前添加此行:

让vm=this

然后在
fetch
函数中使用:

vm.locationList.push(列表)而不是
此.locationList.push(列表)


希望能有帮助。祝您好运。

如果改用箭头函数,则无需创建变量即可访问此
。相反,来自外部上下文的
在arrow函数中可用

getLocations: function() {
    fetch('https://www.webpagetest.org/getLocations.php?f=json')
        .then((response) => {
            if (response.status === 200) {
                return response.json()
            } else {
                throw response;
            }
        })
        .then((data) => {
            var list = Object.entries(data.data).map(([k, v]) => ({
                text: v.Label,
                value: k
            }))
            this.locationList.push(list);
        })
        .catch(function(e) {
            console.log(e)
        });
}

如果改用箭头函数,则无需创建变量即可访问
。相反,来自外部上下文的
在arrow函数中可用

getLocations: function() {
    fetch('https://www.webpagetest.org/getLocations.php?f=json')
        .then((response) => {
            if (response.status === 200) {
                return response.json()
            } else {
                throw response;
            }
        })
        .then((data) => {
            var list = Object.entries(data.data).map(([k, v]) => ({
                text: v.Label,
                value: k
            }))
            this.locationList.push(list);
        })
        .catch(function(e) {
            console.log(e)
        });
}

或者将其更改为arrow function正确,arrow function也会起作用。很高兴它成功了。还有一种方法可以使用
。绑定(this)
,或者将其更改为arrow函数correct,arrow函数也可以做到这一点。很高兴它成功了。还有一种方法可以使用
.bind(this)
我想你是想编写箭头函数。OP已经在使用匿名函数了。箭头函数和常规函数都可以是匿名的,但是当它们是匿名的时候,this关键字有不同的含义。我更正了它。(你可以在发布答案后编辑答案。)我想你是想写箭头函数。OP已经在使用匿名函数了。箭头函数和常规函数都可以是匿名的,但是当它们是匿名的时候,this关键字有不同的含义。我更正了它。(在SO中发布答案后,您可以编辑答案。)