【问题标题】:(VUEJS) Access methods from Axios inside created(VUEJS) 从 Axios 内部创建的访问方法
【发布时间】:2023-03-23 17:25:02
【问题描述】:

我只有一个简单的错误,让我困惑了将近 3 周。

我的问题是,我想从我的 axios API url 末尾的方法“idvideo”返回字符串,但什么也没发生。

您可以在下面的代码中看到。

我一直在寻找解决方案并多次尝试错误,但仍然没有找到任何可以帮助我的最佳答案。

    export default {

        data() {
          return {
            errors: [],
            videos: [],
            items: []

          }
        },

        methods: {
          idvideo: function() {
            const data = this.items
            const result = data.map((item) => {
              return {
                fetchId: item.snippet.resourceId.videoId
              };
            }).sort((a, b) => b.count - a.count);

            var i, len, text;
            for (i = 0, len = result.length, text = ""; i < len; i++) {
              text += result[i].fetchId + ",";
            }
            var x = text.slice(0, -1);
            return(x);
          }

        // Fetches posts when the component is created.
        created() {

          // Ini adalah API utk playlist yang dipilih
          axios.get("https://www.googleapis.com/youtube/v3/playlistItems?part=snippet&maxResults=50&playlistId=PLjj56jET6ecfmosJyFhZSNRJTSCC90hMp&key={YOUR_API_KEY}")
          .then(response => {
            // JSON responses are automatically parsed.
            this.items = response.data.items
          })
          .catch(e => {
            this.errors.push(e)
          }),

          // Ini adalah API utk data yang dipilih
          axios.get('https://www.googleapis.com/youtube/v3/videos?part=snippet%2CcontentDetails%2Cstatistics&key={YOUR_API_KEY}&id='+this.idvideo())
        .then(response => {
            // JSON responses are automatically parsed.
          this.videos = response.data.items
          })
          .catch(e => {
            this.errors.push(e)
          })
        },
        }

我非常感谢任何可以帮助我的解决方案。如果你们有最好的方法来实现这个功能,请告诉我。

抱歉我的英语不好和任何错误。这是我在这个平台上第二次发布问题。

非常感谢楼主!

【问题讨论】:

  • 看看这是否有帮助。 pastebin.com/AVFduFQB
  • 嘿@Pradeepb ...谢谢您的解决方案。为什么它在我的脑海中没有交叉?呵呵。非常感谢!
  • 如果有帮助,请接受答案:)

标签: vue.js vuejs2 youtube-data-api


【解决方案1】:

由于它们是异步请求,因此我想到了以下解决方案。

解决方案:

在第一个 axios 调用中移动下一个 axios 调用。这是因为,只有在第一次调用后,才会检索“项目”,然后将其分配给this.items,因此下一次 axios 调用将具有来自idvideo() 函数的所需数据。

export default {
    data() {
      return {
        errors: [],
        videos: [],
        items: []

      }
    },

    methods: {
      idvideo: function() {
        const data = this.items
        const result = data.map((item) => {
          return {
            fetchId: item.snippet.resourceId.videoId
          };
        }).sort((a, b) => b.count - a.count);

        var i, len, text;
        for (i = 0, len = result.length, text = ""; i < len; i++) {
          text += result[i].fetchId + ",";
        }
        var x = text.slice(0, -1);
        return(x);
      }

    // Fetches posts when the component is created.
    created() {

      // Ini adalah API utk playlist yang dipilih
      axios.get("https://www.googleapis.com/youtube/v3/playlistItems?part=snippet&maxResults=50&playlistId=PLjj56jET6ecfmosJyFhZSNRJTSCC90hMp&key={YOUR_API_KEY}")
      .then(response => {
        // JSON responses are automatically parsed.
        this.items = response.data.items

        // Ini adalah API utk data yang dipilih
      axios.get('https://www.googleapis.com/youtube/v3/videos?part=snippet%2CcontentDetails%2Cstatistics&key={YOUR_API_KEY}&id='+this.idvideo())
    .then(response => {
        // JSON responses are automatically parsed.
      this.videos = response.data.items
      })
      .catch(e => {
        this.errors.push(e)
      })
    }
      })
      .catch(e => {
        this.errors.push(e)
      }),
      ,
    }

【讨论】:

  • 我认为您需要先返回 axios.get then(这将返回承诺),以便下一个 then 可以链接到它。还是不需要?
  • 也许也可以这样做。但我不知道那个解决方案:)
猜你喜欢
  • 2017-11-26
  • 2019-04-20
  • 1970-01-01
  • 1970-01-01
  • 2022-01-16
  • 1970-01-01
  • 2017-04-28
  • 1970-01-01
  • 2018-11-26
相关资源
最近更新 更多