簡體   English   中英

解構不可迭代實例的無效嘗試

[英]Invalid attempt to destructure non-iterable instance

我已經問過這個問題,但我認為標題是錯誤的。

我是 VueJs 的新手,使用vue-aplayer在我的網站上播放音頻文件。 播放器需要資產文件夾中的 svg 圖標,但運行時出現錯誤。

const requireAssets = require.context('../assets', false, /\.svg$/)
  console.log(requireAssets.keys())
  const SVGs = requireAssets.keys().reduce((svgs, path) => {
    const inlineSvg = requireAssets(path)
    const [raw, viewBox, d] = inlineSvg.match(/^<svg.+?viewBox="(.+?)".*><path.+?d="(.+?)".*><\/path><\/svg>$/)

    svgs[path.match(/^.*\/(.+?)\.svg$/)[1]] = {
      viewBox,
      d
    }
    return svgs
  }, {})

它顯示的錯誤是

app.js:6489 Uncaught TypeError: Invalid attempt to destructure non-iterable instance.
In order to be iterable, non-array objects must have a [Symbol.iterator]() method.
    at _nonIterableRest (app.js:6489)
    at _slicedToArray (app.js:6487)
    at app.js:6514
    at Array.reduce (<anonymous>)
    at Module../node_modules/babel-loader/lib/index.js?!./node_modules/vue-loader/lib/index.js?!./resources/js/vue-aplayer/components/aplayer-icon.vue?vue&type=script&lang=js& 

我記錄了requireAssets,它得到了像

0: "./loading.svg"
1: "./lrc.svg"
2: "./menu.svg"
3: "./no-repeat.svg"

我嘗試使用 keys.map() 和 Object.keys(requireAssets).map 但沒有什么能接近解決方案。 如果我評論圖標導入代碼,播放器加載正常但沒有圖標。

整頁代碼:

<template>
  <svg xmlns:xlink="http://www.w3.org/1999/xlink" height="100%" version="1.1" :viewBox="svg.viewBox" width="100%"
       :style="style">
    <use xlink:href="#aplayer-${type}"></use>
    <path class="aplayer-fill" :d="svg.d"></path>
  </svg>
</template>

<script>
  const requireAssets = require.context('../assets/', false, /\.svg$/)
  // console.log(requireAssets.keys())
  console.log(requireAssets.keys())
  const SVGs = requireAssets.keys().reduce((svgs, path) => {
    const inlineSvg = requireAssets(path)
    const svgMatches = inlineSvg.match(/^<svg.+?viewBox="(.+?)".*><path.+?d="(.+?)".*><\/path><\/svg>$/)
    if(!svgMatches) return [];
    const [raw, viewBox, d] = svgMatches
    svgs[path.match(/^.*\/(.+?)\.svg$/)[1]] = {
      viewBox,
      d
    }
    return svgs
  }, {})

  export default {
    props: ['type'],
    computed: {
      svg () {
        let icon = this.type
        if (this.type === 'prev' || this.type === 'next') {
          icon = 'skip'
        }
        return SVGs[this.type] || {}
      },
      style () {
        if (this.type === 'next') {
          return {
            transform: 'rotate(180deg)',
          }
        }
      }
    }
  }
</script>

如果您的正則表達式與 svg 語法不匹配,它將返回null 所以你應該在解構它之前測試你的結果以避免運行時錯誤。

 const requireAssets = require.context('../assets', false, /\.svg$/) console.log(requireAssets.keys()) const SVGs = requireAssets.keys().reduce((svgs, path) => { const inlineSvg = requireAssets(path) const svgMatches = inlineSvg.match(/^<svg.+?viewBox="(.+?)".*><path.+?d="(.+?)".*><\/path><\/svg>$/) if(;svgMatches) return [], const [raw, viewBox. d] = svgMatches svgs[path.match(/^.*\/(?+.)\,svg$/)[1]] = { viewBox, d } return svgs }, {})

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM