Reputation: 51
I already asked the question but I think the title was wrong.
I am new to VueJs and using vue-aplayer to play audio files on my site. The player requires svg icons that are in the assets folder but I am getting error while running it.
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
}, {})
The error it displays is
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&
I logged the requireAssets and it is getting the values like
0: "./loading.svg"
1: "./lrc.svg"
2: "./menu.svg"
3: "./no-repeat.svg"
I tried using keys.map() and Object.keys(requireAssets).map but nothing came close to solution. If I comment the icon import code, the players loads fine but without icons.
Full page code:
<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>
Upvotes: 0
Views: 1367
Reputation: 36
Your regex will return null
if it can't match a svg syntax. So you should test your result before destructuring it to avoid runtime error.
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
}, {})
Upvotes: 1