Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
487 views
in Technique[技术] by (71.8m points)

webpack中使用art-template渲染图片路径出错

使用art-template渲染图片,不会被url-loader处理

  • 项目结构

1.png

  • index.js
const render = require('./template/product.art')
// 配置数据
const data = {
  product: [
    {
      imgSrc: '1.png',
      name: '产品1',
      price: 100,
    },
    {
      imgSrc: '2.png',
      name: '产品2',
      price: 200,
    },
  ],
}
// 数据渲染
const html = render(data)
console.log(html)

// 渲染到页面
const proList = document.querySelector('.pro-list')
proList.innerHTML = html
  • product.art
{{each product}}
<li>
  <img src="~@img/{{$value.imgSrc}}" />
  <span>{{$value.name}}</span>
  <span>{{$value.price}}</span>
</li>
{{/each}}
  • index.html
<body>
  <div id="app">
    <ul class="pro-list"></ul>
  </div>
</body>
  • webpack.dev.config.js
...

module: {
  rules: [
    {
      test: /.art$/,
      loader: 'art-template-loader',
    },
    {
      test: /.html$/,
      loader: 'html-loader',
    },
    {
      test: /.(jpg|png|gif)$/,
      loader: 'url-loader',
      options: {
        limit: 8 * 1024,
        name: '[hash:10].[ext]',
        // 关闭es6模块化
        esModule: false,
        outputPath: 'imgs',
      },
    },
  ],
},

...

resolve: {
  alias: {
    '@': resolve(__dirname, '../src'),
    '@css': resolve(__dirname, '../src/css'),
    '@img': resolve(__dirname, '../src/images'),
  }
}

...
  • 图片路径出错,好像url-loader没有处理

2.png


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
等待大神解答

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...