【问题标题】:webpack production of angular2 can't navigate with URLangular2的webpack生产无法使用URL导航
【发布时间】:2016-11-13 16:12:24
【问题描述】:

我有一个使用 angular2 路由的 angular2 应用程序,webpack 配置的 dev 和 prod 版本如 tutorial 中所述。

当我使用 dev 选项,运行 npm start 时,一切正常,并且导航可以使用网站上的链接或完整的 URL,如http://localhost:3000/dashboard。 但是当我使用 prod 选项,运行 npm run build 并将 dist 文件夹输出部署到我的 iis 时,我无法再使用 URL 进行导航,我必须首先转到 http:/MyIp:3000/,然后我只能使用网站上的链接。任何使用 URL 导航的尝试都会导致 404 错误。

此外,在 prod 模式下,每当我刷新页面时,都会返回 404 错误。

有人遇到过这个错误吗?有没有人知道如何解决它们?

更新

为 webpack.common.js 和 webpack.prod.js 添加我的代码,也许有人会发现我忽略的错误...

我的 webpack.common.js

var webpack = require('webpack');
var HtmlWebpackPlugin = require('html-webpack-plugin');
var ExtractTextPlugin = require('extract-text-webpack-plugin');
var helpers = require('./helpers');

module.exports = {
  entry: {
    'polyfills': './src/polyfills.ts',
    'vendor': './src/vendor.ts',
    'app': './src/app/main.ts'
  },

  resolve: {
    extensions: ['', '.ts', '.js']
  },

  module: {
    loaders: [
      {
        test: /\.ts$/,
        loaders: ['awesome-typescript-loader', 'angular2-template-loader']
      },
      {
        test: /\.html$/,
        loader: 'html'
      },
      {
        test: /\.(png|jpe?g|gif|svg|woff|woff2|ttf|eot|ico)$/,
        loader: 'file?name=assets/[name].[hash].[ext]'
      },
      {
        test: /\.css$/,
        exclude: helpers.root('src', 'app'),
        loader: ExtractTextPlugin.extract('style', 'css?sourceMap')
      },
      {
        test: /\.css$/,
        include: helpers.root('src', 'app'),
        loader: 'raw'
      }
    ]
  },

  plugins: [
    new webpack.optimize.CommonsChunkPlugin({
      name: ['app', 'vendor', 'polyfills']
    }),

    new HtmlWebpackPlugin({
      template: 'src/index.html'
    })
  ]
};

我的 webpack.prod.js

var webpack = require('webpack');
var webpackMerge = require('webpack-merge');
var ExtractTextPlugin = require('extract-text-webpack-plugin');
var commonConfig = require('./webpack.common.js');
var helpers = require('./helpers');

const ENV = process.env.NODE_ENV = process.env.ENV = 'production';

module.exports = webpackMerge(commonConfig, {
  devtool: 'source-map',

  output: {
    path: helpers.root('dist'),
    publicPath: '/',
    filename: '[name].[hash].js',
    chunkFilename: '[id].[hash].chunk.js'
  },
  htmlLoader: {
    minimize: false // workaround for ng2
  },

  plugins: [
    new webpack.NoErrorsPlugin(),
    new webpack.optimize.DedupePlugin(),
    new webpack.optimize.UglifyJsPlugin({ // https://github.com/angular/angular/issues/10618
      mangle: {
        keep_fnames: true
      }
    }),
    new ExtractTextPlugin('[name].[hash].css'),
    new webpack.DefinePlugin({
      'process.env': {
        'ENV': JSON.stringify(ENV)
      }
    })
  ]
});

【问题讨论】:

    标签: angular deployment webpack angular2-routing


    【解决方案1】:

    我们正在使用 angular-cli,我什至无法让开发版本正常工作(关于您的问题)。通过单击链接在应用程序中导航很好,但 F5 或直接点击深层链接不起作用。我们在 IIS 中使用以下 URL 重写规则解决了这个问题(如果您还没有 web.config,请创建一个 web.config 并将其添加到根文件夹):

    <?xml version="1.0" encoding="UTF-8"?>
    <configuration>
        <system.webServer>
        <rewrite>
          <rules>
            <rule name="Angular Routes" stopProcessing="true">
              <match url=".*" />
              <conditions logicalGrouping="MatchAll">
                <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
                <add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
              </conditions>
              <action type="Rewrite" url="/" />
            </rule>
          </rules>
        </rewrite> 
      </system.webServer>
    </configuration>
    

    我不确定是否建议在 prod 中使用它,但这是内部托管的,现在可以使用。

    【讨论】:

    • 酷,请将其标记为答案,以便其他人轻松找到,干杯。
    猜你喜欢
    • 2016-06-02
    • 1970-01-01
    • 1970-01-01
    • 2016-12-11
    • 2017-01-16
    • 2017-02-13
    • 2022-08-16
    • 2016-11-17
    • 2020-12-27
    相关资源
    最近更新 更多