鍍金池/ 問答/HTML/ webpack 如何處理通過 `js` 引入的資源文件加載問題??

webpack 如何處理通過 `js` 引入的資源文件加載問題??

相關(guān)配置:

項目路徑:/var/website/test
目錄結(jié)構(gòu):
compiled
source
    components
        public
            public.vue
    static
        image
            test.png
    plugins
    // ...
static

假設(shè)在一個 source/components/public/public.vue 文件中,通過 js 引入了一個文件:

<template>
    <section>
        <img ref='test' src='' class='image'>
    </section>
</template>
<script>
export default {
    name: '' , 
    mounted () {
        this.$refs.test.src='../../static/image/test.png';
    }
};
</script>

webpack 打包后,訪問不到資源!!

webpack 如何處理通過 js 引入的資源文件加載問題??

回答
編輯回答
網(wǎng)妓

webpack引入圖片等其他資源也必須是require或者import(最終也會轉(zhuǎn)成require)

2018年6月21日 02:27
編輯回答
糖豆豆

import再使用

import src from '../../static/image/test.png';

export default {
  name: '',
  mounted() {
    this.$refs.test.src = src;
  }
};
2017年9月13日 23:26