Angular2图片未显示 [英] Angular2 image not showing

查看:75
本文介绍了Angular2图片未显示的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

每当我在html中添加图片标签时,我都会执行以下操作:

Anytime i add a image tag in the html and i do something like this:

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

图像不显示.相反,我得到一些东西显示

The image doesn't show. Instead i get something this showing

我尝试将< base href ="/"> 放在我的index.html头中,但没有任何变化.这可能是什么原因?我还必须做些什么才能使这项工作成功?

I have tried putting <base href="/"> in my index.html head but i get no changes. What could be the cause of this? What else must i do to make this work?

我也尝试安装image-webpack-loader这是我的webpack.config.js文件的内容

I also tried installing image-webpack-loader Here is the content 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);

推荐答案

您不能脱离 base href 范围,应该将资产移动到 src 目录中并给出路径后,例如 ./resources ,它将是 src/resources .

You can't get out of base href scope, you should move your assets inside src directory and after give path e.g ./resources it will be src/resources.

这篇关于Angular2图片未显示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆