简体   繁体   English

API 使用 vuejs 调用

[英]API call with vuejs

Code: https://pastebin.com/uyp5VM4Z代码: https://pastebin.com/uyp5VM4Z

Can somebody help me, that when opening the page instead of {{stats. jobs}}有人可以帮助我,当打开页面而不是{{stats. jobs}} {{stats. jobs}} nothing is written. {{stats. jobs}}什么都没有写。 The problem is also described in the image and in the codepen.图像和代码笔中也描述了该问题。

https://codepen.io/Elten1/pen/XWmeBQP https://codepen.io/Elten1/pen/XWmeBQP

https://prnt.sc/sa9ioi https://prnt.sc/sa9ioi

Your mistake is in setting the data.您的错误在于设置数据。 When creating the instance of your vue, you have to set it like this:创建 vue 的实例时,您必须像这样设置它:

data : {
  status : {
  jobs : null,
  },
},

In the example you share you used:在您分享的示例中,您使用了:

    data() {
return {
stats : null,
}
}

Here there are two issues, this particular format where you are returning the object is used for components not for the Vue instance, the reason for that is, each component must have its own dataset and setting it like Vue instance, you cannot differentiate between the data of instances of same component.这里有两个问题,您返回 object 的特定格式用于不用于 Vue 实例的组件,原因是每个组件必须有自己的数据集并将其设置为 Vue 实例,您无法区分相同组件实例的数据。

Another problem is you have set stats to null, you instead have to set stats.jobs to null.另一个问题是您已将 stats 设置为 null,而您必须将 stats.jobs 设置为 null。 This is because Vue needs to know the variables for the vue instance and sort of works like map for Vue.这是因为 Vue 需要知道 vue 实例的变量,并且像 Vue 的 map 这样的工作。 There is a way to set nested values using vue's set funtion but you will have to check the documentation on how to do that.有一种方法可以使用 vue 的 set 函数设置嵌套值,但您必须查看有关如何执行此操作的文档。

Reference: https://vuejs.org/v2/guide/reactivity.html参考: https://vuejs.org/v2/guide/reactivity.html

I directly set the set the data and I didn't check if the API is working.我直接设置了数据,并没有检查API是否工作。 So do check if the API is returning the data.因此,请检查 API 是否正在返回数据。

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

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