来自 setup() 函数的 REST 调用 - ionic - vue.js

REST Call from setup() function - ionic - vue.js

我目前正在努力通过我的 Ionic Vue 应用程序的设置功能对外部 API 进行 REST 调用。 我尝试了以下方法:

const oResponse = fetch(
   "url"
     ).then((Response) => Response.json());

return { Items: [{ name: 'Milch', Flag: '*' }, { name: 'Wasser', Flag: '*' },{name: 'BraTee', Flag: '*'}], currentRec: oResponse.recipes }

Return 结构如下所示:

{
  recipes": [
    { ... },
    { ... }
 ]
}

我无法让它工作,因为 Ionic 说“oResponse 没有名为 'recipes' 的组件。

我该如何处理这个错误并正确获取 REST 调用? 提前致谢!

也许您应该重新编写 api 请求。我通常使用 axios 库来进行 api 调用。类似于:

axios
      .post("url")  // Methods you can use also: .get , .put
      .then((response) => {
        return response.data;
      })
      .catch((error) => {
       console.log(
          "an error occurred"
        );
      });

npm install axios --> 安装 axios 库

import axios from "axios"; ---> 在组件中导入它