简体   繁体   English

如何从 Vue.js 中的方法返回数据?

[英]How to return data from method in Vue.js?

I'm learning Vue.js, and I have trouble finding anything related to this topic.我正在学习 Vue.js,但找不到与此主题相关的任何内容。 I fetch the data from the API, which shows in console.log correctly, but I can't seem to return it to the template view.我从 API 中获取数据,该数据正确显示在console.log ,但我似乎无法将其返回到模板视图。 I am not trying to send this data to another component;我不是要将此数据发送到另一个组件; I want to display it.我想显示它。 How do you make this happen?你如何做到这一点? In React, I would map the result and return the HTML.在 React 中,我会map结果并返回 HTML。 How do you do this in Vue.js?你如何在 Vue.js 中做到这一点? Also, I am doing this in Laravel 8.另外,我在 Laravel 8 中这样做。

<template>
    <div>
        {{ data }}
    </div>
</template>
<script>
    let api = location.search.replace("?", "/");
    api = api.replace("=", "");

    export default {
        name: "Play",
        data: function () {
            return {
                data: data
            };
        },
        created() {
            this.getQuestions();
        },
        methods: {
            getQuestions() {
                let data = [];
                fetch(api)
                    .then(response => response.json())
                    .then(response => {
                        return (data = response.results);
                    })
                    .catch(err => console.log(err));
            }
        }
    };
</script>

要访问 Vue 组件上的data属性,您必须使用this访问器:

this.data = response.results

What is data here?这里的数据是什么?

data: function() {
        return {
            data: *data*
        };
    },

this data hasn't been declared anywhere, you need to set it to a default value, maybe a null like this,此数据尚未在任何地方声明,您需要将其设置为默认值,可能是这样的 null,

data: function() {
        return {
            data: null
        };
    },

then try setting it in the method like this然后尝试在这样的方法中设置它

this.data = response.data

and then you can access it in the template like the way you have done already.然后你可以像你已经做的那样在模板中访问它。

Also, it is advised to use Vue's $set method to keep the reactivity flow - so you can use此外,建议使用 Vue 的 $set 方法来保持反应性流 - 所以你可以使用

this.$set(this, 'data', response.data);

to set the data.来设置数据。

change this改变这个

 data: function() {
        return {
            data: data
        };
to 
 data: function() {
        return {
            data: null
        };

change creataed() to mounted()creataed()更改为mounted()

add添加

this.data = response.results;

in your getQuestions() in place of your return(data = response.results);在你的 getQuestions() 代替你的return(data = response.results); I think you can also kill the .then(response => response.json())我想你也可以杀死.then(response => response.json())

in your vue template you can do在你的 vue 模板中你可以做

<div>
<template v-if="data"> {{data.somefield}}<template>
</div>

The idea is you set data to null, when the component is mounted you "fetch" data and save it to the data property.这个想法是您将数据设置为空,当组件安装时,您“获取”数据并将其保存到数据属性。 Vue sees that the data is no longer null and shows you data.somefield Vue 看到数据不再为空并显示 data.somefield

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM