Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

image src get 404 error with vuejs

Tags:

webpack

vue.js

I'm new to vue.js, and trying to render a list of images using v-for directive. Instead, I get the server responded with a status of 404 (Not Found). I'm using the template generated by vue-cli.

The webpack url-loader should change the url of the image corresponding to item.src to some [name].[ext]?[hash] format, but it simply leave them alone (actually the result url is http://localhost:8080/assets/computer.jpg ).

And here's my code:

<template>
  <h2 class="ui header">
		<div class="content">
			Recent Research
			<div class="sub header">
				recent research issue
			</div>
		</div>
	</h2>
  <div v-for="item in items" class="ui three column centered grid">
    <div class="column">
      <img class="ui centered medium image" :src="item.src" />
      <h3 class="ui header">{{item.text}}</h3>
      <p class="teal meta">{{item.extra}}</p>
    </div>
  </div>
</template>

<script>
  export
  default {
    data() {
      return {
        items: [{
            src: '../assets/computer.jpg',
            text: 'fundamentals of search engine',
            extra: 'with Google'
          }, {
            src: '../assets/computer.jpg',
            text: 'fundamentals of search engine',
            extra: 'with Google'
          }, {
            src: '../assets/computer.jpg',
            text: 'fundamentals of search engine',
            extra: 'with Google'
          }, {
            src: '../assets/computer.jpg',
            text: 'fundamentals of search engine',
            extra: 'with Google'
          }, {
            src: '../assets/computer.jpg',
            text: 'fundamentals of search engine',
            extra: 'with Google'
          }, {
            src: '../assets/computer.jpg',
            text: 'fundamentals of search engine',
            extra: 'with Google'
          },

        ]
      }
    }
  }
</script>

And the related webpack config.

var path = require('path')

module.exports = {
  entry: {
    app: './src/main.js'
  },
  output: {
    path: path.resolve(__dirname, '../dist/static'),
    publicPath: '/static/',
    filename: '[name].js'
  },
  resolve: {
    extensions: ['', '.js', '.vue'],
    alias: {
      'src': path.resolve(__dirname, '../src')
    }
  },
  resolveLoader: {
    root: path.join(__dirname, 'node_modules'),
  },
  module: {
    loaders: [{
      test: /\.vue$/,
      loader: 'vue'
    }, {
      test: /\.js$/,
      loader: 'babel',
      exclude: /node_modules/
    }, {
      test: /\.json$/,
      loader: 'json'
    }, {
      test: /\.(png|jpg|gif|svg)$/,
      loader: 'url',
      query: {
        limit: 10000,
        name: '[name].[ext]?[hash]'
      }
    }]
  },
  vue: {
    loaders: {
      js: 'babel'
    }
  }
  /*
  eslint: {
    formatter: require('eslint-friendly-formatter')
  }
  */
}
like image 276
温发琥 Avatar asked Dec 19 '22 20:12

温发琥


1 Answers

You are passing a string to the 'src' field of your items array, no way webpack can tell if that string is a resource.

I'd suggest you try to change it to something like:

 items: [{
    src: require('../assets/computer.jpg'),
    text: 'fundamentals of search engine',
    extra: 'with Google'
  }, {    
....

That way the output may be processed by webpack.

like image 181
TiagoLr Avatar answered Jan 02 '23 15:01

TiagoLr