6

这是我的目录结构:

- public
- src
  - app.js
  - assets
    - images
      - logo-b-green.png 
    - stylesheets
      - nav.scss

和:

// webpack.config.js

module.exports = {
  entry: './src/app.js',
  output: {
    path: './public',
    filename: 'bundle.js'
  },
  module: {
    loaders: [
      {
        test: /.jsx?$/,
        exclude: /node_modules/,
        loader: 'babel-loader'
      },
      {
        test: /\.scss$/,
        loaders: ['style', 'css', 'sass']
      },
      {
          test: /\.(eot|svg|ttf|woff|woff2)$/,
          loader: 'file?name=fonts/[name].[ext]'
      },
      {
        test: /\.(png|jpg|gif)$/,
        loader: "file-loader?name=images/img-[hash:6].[ext]"
      }
    ]
  },
  resolve: {
    extensions: ['', '.js', '.json']
  }
};

和:

/* nav.scss */

#nav-logo {
  height: 26px;
  width: 26px;
  background-size: 100%;
  background-image: url('../images/logo-b-green.png');
  float: left;
  margin: 16px 0 0 23px;
}

加载单级路由时,我的图像链接正常工作。

例如,/search在图像链接被渲染为url(images/img-75fa3d.png)和作品。

但是,如果我转到嵌套路由(通过 React 路由器),例如/properties/1图像链接是相同的,并且无法找到正确的图像位置,因为它是一个相对链接:url(images/img-75fa3d.png).

此示例中的图像是logo-b-green.png.

编辑:

我将其添加publicPath到该output部分并且它起作用了,现在网址进入了根目录。有人可以确认这是处理此问题的正确方法吗?

output: {
 path: './public',
 filename: 'bundle.js',
 publicPath: '/'
}, ...
4

1 回答 1

2

我将其添加publicPath到该output部分并且它起作用了,现在网址进入了根目录。

output: {
 path: './public',
 filename: 'bundle.js',
 publicPath: '/'
}, ...
于 2016-09-01T18:29:33.950 回答