vue @/src/assets/images/1.jpg: 尚未转译错误

huangapple go评论53阅读模式
英文:

vue @/src/assets/images/1.jpg: hasn't been transpiled yet error

问题

I'm getting this error - vue @/src/assets/images/1.jpg: hasn't been transpiled yet error. I'm looping through the App component static array. The src is specified correctly though. Using require vue method.

我遇到了这个错误 - vue @/src/assets/images/1.jpg: 还没有被转译的错误。我正在循环遍历 App 组件的静态数组。虽然 src 已经正确指定了。我正在使用 require vue 方法。

英文:

**I'm getting this error - vue @/src/assets/images/1.jpg: hasn't been transpiled yet error. I'm looping through tha App component static array. The src is specified correctly though. Using require vue method.

https://codesandbox.io/s/eloquent-grass-j1usw8?file=/src/components/v-carousel-item.vue
**

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

// APP

&lt;template&gt;
  &lt;v-carousel :carousel_data=&quot;sliderItems&quot; /&gt;
&lt;/template&gt;
&lt;script&gt;
import vCarousel from &quot;./components/v-carousel.vue&quot;;
export default {
  name: &quot;App&quot;,
  data() {
    return {
      sliderItems: [
        { id: 1, name: &quot;img1&quot;, img: &quot;1.jpg&quot; },
        { id: 2, name: &quot;img2&quot;, img: &quot;2.jpg&quot; },
        { id: 3, name: &quot;img3&quot;, img: &quot;3.jpg&quot; },
      ],
    };
  },
  components: {
    vCarousel,
  },
};
&lt;/script&gt;

// Parent

&lt;template&gt;
  &lt;div class=&quot;container&quot;&gt;
    &lt;div class=&quot;v-carousel&quot;&gt;
      &lt;v-carousel-item
        v-for=&quot;item in carousel_data&quot;
        :key=&quot;item.id&quot;
        :item_data=&quot;item&quot;
      /&gt;
    &lt;/div&gt;
  &lt;/div&gt;
&lt;/template&gt;
&lt;script&gt;
import vCarouselItem from &quot;./v-carousel-item.vue&quot;;
export default {
  components: {
    vCarouselItem,
  },
  props: {
    carousel_data: {
      type: Array,
      default: () =&gt; [],
    },
  },
};
&lt;/script&gt;


// Child 

&lt;template&gt;
  &lt;div class=&quot;v-carousel-item&quot;&gt;
    &lt;img :src=&quot;require(`../assets/images/` + item_data.img)&quot; alt=&quot;&quot; /&gt;
  &lt;/div&gt;
&lt;/template&gt;
&lt;script&gt;
export default {
  props: {
    item_data: {
      type: Object,
      default: () =&gt; {},
    },
  },
};
&lt;/script&gt;

<!-- end snippet -->

答案1

得分: 0

你想要提前加载这些图片。

export default {
  name: "App",
  data() {
    return {
      sliderItems: [
        { id: 1, name: "img1", img: require("@/assets/images/1.jpg") },
        { id: 2, name: "img2", img: require("@/assets/images/2.jpg") },
        { id: 3, name: "img3", img: require("@/assets/images/3.jpg") },
      ],
    };
  },

然后更新轮播项目组件。

<div class="v-carousel-item">
  <img :src="item_data.img" alt="" />
</div>

示例: 链接

英文:

You want to require the images upfront.

export default {
  name: &quot;App&quot;,
  data() {
    return {
      sliderItems: [
        { id: 1, name: &quot;img1&quot;, img: require(&quot;@/assets/images/1.jpg&quot;) },
        { id: 2, name: &quot;img2&quot;, img: require(&quot;@/assets/images/2.jpg&quot;) },
        { id: 3, name: &quot;img3&quot;, img: require(&quot;@/assets/images/3.jpg&quot;) },
      ],
    };
  },

Then update the carousel item component.

  &lt;div class=&quot;v-carousel-item&quot;&gt;
    &lt;img :src=&quot;item_data.img&quot; alt=&quot;&quot; /&gt;
  &lt;/div&gt;

Example: https://codesandbox.io/s/little-bush-ino5zc?file=/src/components/v-carousel-item.vue:11-91

huangapple
  • 本文由 发表于 2023年2月8日 18:02:59
  • 转载请务必保留本文链接:https://go.coder-hub.com/75384118.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定