简体   繁体   English

Angular-使用route参数重新加载页面上的包错误

[英]Angular - bundle error on page reload with route parameter

Angular 4 角度4

I'm getting the Uncaught SyntaxError: Unexpected token < error from my bundles, but only in a specific situation: 我从捆绑包中收到Uncaught SyntaxError: Unexpected token <错误,但仅在特定情况下:

  • on live server only (nginx forwarding all traffic to express server, config below) 仅在实时服务器上(nginx将所有流量转发到快速服务器,在下面的配置中)
  • only when refreshing routes that end on a route param, eg chart/123 , where 123 is a chart id and the path is chart/:id in the router. 仅当刷新以路由参数结尾的路由时,例如, chart/123 ,其中123是图表ID,而路径是路由器中的chart/:id Something like /profile works fine as this isn't a param in a route. /profile类的东西可以正常工作,因为这不是路由中的参数。
  • edit - I also get this error for routes such as users/add and organisations/add , so it seems to error for any route with more than one 'part'... 编辑 -对于users/addorganisations/add等路线,我也收到此错误,因此对于包含多个“零件”的任何路线似乎都出错了...

The result is that all the *.bundle.js requests are returning the index.html file instead of their preferred *.js files. 结果是所有*.bundle.js请求都返回了index.html文件,而不是首选的*.js文件。

Any ideas? 有任何想法吗? Let me know if more context is needed. 让我知道是否需要更多背景信息。

Thanks in advance for any help. 在此先感谢您的帮助。


Here is my server.js: 这是我的server.js:

const express = require('express');
const compression = require('compression');
const morgan = require('morgan');
const path = require('path');
const rfs = require('rotating-file-stream');
const fs = require('fs');
const app = express();

/**
 * Logging
 */

 /**
 * Create a logs directory (in project root)
 */
const logDirectory = path.join(__dirname, './logs');
if (!fs.existsSync(logDirectory)) {
  fs.mkdirSync(logDirectory);
}
// Set up rotating file stream, rotate every day
const accessLogStream = rfs('access.log', {
  interval: '1d',
  path: logDirectory,
});
// winston.info(logDirectory);
app.use(morgan('short', { stream: accessLogStream }));

/**
 * /Logging
 */

app.use(compression());

const port = process.env.PORT || 8082;

app.disable('x-powered-by');

app.use(express.static(__dirname + '/dist'));

app.get('*', (req,res) => {
  try {
    res.sendFile(`${__dirname}/dist/index.html`);
  } catch (e) {
    res.status(404).send('index not found');
  }
});


app.listen(port,'127.0.0.1');
console.log(`listening on 127.0.0.1:${port}`);

Here is my angular-cli.json (pretty much boilerplate): 这是我的angular-cli.json(大致样板):

{
  "$schema": "./node_modules/@angular/cli/lib/config/schema.json",
  "project": {
    "name": "new-client"
  },
  "apps": [
    {
      "root": "src",
      "outDir": "dist",
      "assets": [
        "assets",
        "favicon.ico"
      ],
      "index": "index.html",
      "main": "main.ts",
      "polyfills": "polyfills.ts",
      "test": "test.ts",
      "tsconfig": "tsconfig.app.json",
      "testTsconfig": "tsconfig.spec.json",
      "prefix": "app",
      "styles": [
        "assets/scss/main.scss"
      ],
      "scripts": [
        "../node_modules/chart.js/dist/Chart.min.js",
         "../node_modules/moment/min/moment.min.js"
      ],
      "environmentSource": "environments/environment.ts",
      "environments": {
        "dev": "environments/environment.ts",
        "livedata": "environments/environment.livedata.ts",
        "production": "environments/environment.prod.ts"
      }
    }
  ],
  "e2e": {
    "protractor": {
      "config": "./protractor.conf.js"
    }
  },
  "lint": [
    {
      "project": "src/tsconfig.app.json"
    },
    {
      "project": "src/tsconfig.spec.json"
    },
    {
      "project": "e2e/tsconfig.e2e.json"
    }
  ],
  "test": {
    "karma": {
      "config": "./karma.conf.js"
    }
  },
  "defaults": {
    "styleExt": "scss",
    "component": {
    }
  }
}

So for me, the issue was conflicting base-href options . 所以对我来说,问题在于base-href选项相互冲突

In my index.html, I have the standard <base href="/"> option. 在我的index.html中,我具有标准的<base href="/">选项。

Then, in my build step, I had this: 然后,在我的构建步骤中,我有以下内容:

ng build --environment=production --base-href . --aot

note the --base-href . 注意--base-href . - this seems to have been overriding the / option in the index.html. -这似乎已经覆盖了index.html中的/选项。 I solved by removing --base-href from my build script. 我通过从构建脚本中删除--base-href来解决。 I could have also set it to ./dist to remove the conflict I think. 我也可以将其设置为./dist以消除我认为的冲突。

From the angular-cli docs for build , this setting overrides any option in your index/html . 用于buildangular-cli文档中 ,此设置将覆盖index/html任何选项。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM