简体   繁体   English

在 Computed 中运行 Axios 的替代方案

[英]Alternative to Running Axios in Computed

I am working on a high school project involving chemical data.我正在从事一个涉及化学数据的高中项目。 I need to get a response from this URL and reference it in another function.我需要得到这个 URL 的响应,并在另一个 function 中引用它。 When I try to use descriptionURL in my mounted function, I get an error saying it is undefined.当我尝试在已安装的 function 中使用 descriptionURL 时,我收到一条错误消息,提示它未定义。 Any help not involving 3rd party additions would be appreciated.任何不涉及第三方添加的帮助将不胜感激。

Note: the properties.CID is coming from a request from the mounted function.注意:properties.CID 来自已安装的 function 的请求。

 computed: { description() { var descriptionURL = "https://pubchem.ncbi.nlm.nih.gov/rest/pug/compound/cid/" + this.properties.CID + "/description/json" //eslint-disable-next-line no-console console.log(descriptionURL); axios.get(this.descriptionURL) //eslint-disable-next-line no-console.then(response => {this.descriptionText = response.data}) //eslint-disable-next-line no-console.catch(function (error) { //eslint-disable-next-line no-console console.log(error); }) return descriptionURL } }, }

There are a few things that I find puzzling in your snippet.我在您的代码段中发现了一些令人费解的事情。

To begin with, there is a variable called descriptionURL that is defined within the scope of the description() computed property.首先,在description()计算属性的 scope 中定义了一个名为descriptionURL的变量。 However, axios is sending a request to this.descriptionURL , which would be a variable defined at the component level, hence not the descriptionURL variable mentioned before.但是,axios 正在向this.descriptionURL发送请求,这将是在组件级别定义的变量,因此不是前面提到的descriptionURL变量。 Is that intentional?这是故意的吗?

Also, your computed property returns descriptionURL , which might not be the content that you want to use.此外,您的计算属性返回descriptionURL ,它可能不是您想要使用的内容。

Moreover, Computed properties should not have side effects , which yours seems to do (altering the property descriptionText ).此外, Computed 属性不应该有副作用,你的似乎会这样做(改变属性descriptionText )。 I would recommend using a method instead of a computed property.我建议使用方法而不是计算属性。

If you really intended your computed property to return descriptionURL , then you can access this value by calling this.description (as opposed to this.descriptionURL ) in mounted() .如果您真的希望您的计算属性返回descriptionURL ,那么您可以通过在mounted()中调用this.description (而不是this.descriptionURL )来访问此值。

Finally, since the API call needs some time to get a response, it is unlikely that wou will be able to use its result within mounted().最后,由于 API 调用需要一些时间才能得到响应,因此不太可能在 mount() 中使用其结果。

In case you are looking for alternatives, here is how I would implement this feature with my current understanding of the situation:如果您正在寻找替代方案,以下是我将如何根据我目前对情况的理解来实现此功能:

data(){
  return{
    descriptionText: null,
  }
},
method: {
  get_description() {
    var descriptionURL = "https://pubchem.ncbi.nlm.nih.gov/rest/pug/compound/cid/" + this.properties.CID + "/description/json"
    axios.get(descriptionURL)
    .then(response => { this.descriptionText = response.data })
    .catch((error) => { console.error(error) })
  },
  other_method_using_description(){

    // Check if descriptionText actually contains a description
    if(!this.descriptionText) {
      console.error('descriptionText is still null')
      return
    }

    // use this.descriptionText
  }
}

In this example, get_description is a method used to populate the descriptionText property with the response from the HTTP request.在此示例中,get_description 是一种用于使用来自 HTTP 请求的响应填充 descriptionText 属性的方法。

You can then use this property in other functions, assuming it has received data.然后,您可以在其他函数中使用此属性,假设它已接收到数据。

Please try .get(descriptionURL) instead of .get(this.descriptionURL) , because descriptionURL is in description() scope and not on vue instance.请尝试.get(descriptionURL)而不是.get(this.descriptionURL) ,因为descriptionURLdescription() scope 中,而不是在 vue 实例上。

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

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