Image is n...">

Angular2 image not showing

Anytime I add an image tag in html and I do something like this:

<img src="../resources/img/myimg.png"> 

Image is not displayed. Instead, I get something like this blank image

I tried putting <base href="/"> in my head index.html, but I am not getting any changes. What could be the reason for this? What else should I do to make this work?

I also tried installing image-webpack-loader. Here is the contents of my webpack.config.js file

 var webpack = require('webpack'); var path = require('path'); // Webpack Config var webpackConfig = { entry: { 'polyfills': './src/polyfills.browser.ts', 'vendor': './src/vendor.browser.ts', 'main': './src/main.browser.ts', }, output: { path: __dirname + '/dist', }, plugins: [ new webpack.optimize.OccurenceOrderPlugin(true), new webpack.optimize.CommonsChunkPlugin({ name: ['main', 'vendor', 'polyfills'], minChunks: Infinity }), ], module: { loaders: [ // .ts files for TypeScript { test: /\.ts$/, loaders: ['awesome-typescript-loader', 'angular2-template-loader'] }, { test: /\.css$/, loaders: ['to-string-loader', 'css-loader'] }, { test: /\.html$/, loader: 'raw-loader' }, { test: /.*\.(gif|png|jpe?g|svg)$/i, loaders: [ 'file?hash=sha512&digest=hex&name=[hash].[ext]', 'image-webpack?{progressive:true, optimizationLevel: 7, interlaced: false, pngquant:{quality: "65-90", speed: 4}}' ] } ] } }; // Our Webpack Defaults var defaultConfig = { devtool: 'cheap-module-source-map', cache: true, debug: true, output: { filename: '[name].bundle.js', sourceMapFilename: '[name].map', chunkFilename: '[id].chunk.js' }, resolve: { root: [ path.join(__dirname, 'src') ], extensions: ['', '.ts', '.js'] }, devServer: { historyApiFallback: true, watchOptions: { aggregateTimeout: 300, poll: 1000 } }, node: { global: 1, crypto: 'empty', module: 0, Buffer: 0, clearImmediate: 0, setImmediate: 0 } }; var webpackMerge = require('webpack-merge'); module.exports = webpackMerge(defaultConfig, webpackConfig); 
+5
source share
1 answer

You cannot leave the base href , you must transfer your resources to the src directory and after specifying the path, for example ./resources , it will be src/resources .

+7
source

All Articles