【问题标题】:angular index.html script shown显示的角度 index.html 脚本
【发布时间】:2021-12-24 13:01:29
【问题描述】:

我之前在服务器中部署了一个 Angular 应用程序,直到现在都没有问题。 Angular 版本是 6.1.1,Angular CLI 版本是 6.2.9,npm 版本是 6.13.4,node 版本是 10.18.0 本地和服务器。服务器是 Ubuntu 20.04。

基本上,我在本地 (Windows) 中处理代码,在 localhost 上对其进行测试,将代码推送到 git,从 git 拉取到服务器,然后将其部署到服务器中。

像往常一样,我对组件、服务和服务器端代码进行了一些更改(我根本没有接触过 index.html),并检查了它在 localhost 中是否正常工作。但是,这一次我部署时,该站点实际上显示 index.html 脚本是这样的。

<!doctype html>
<html lang="en">
<head>
<link href="https://fonts.googleapis.com/icon?family=Material+Icons" rel="stylesheet">
<link href="https://fonts.googleapis.com/css?family=Roboto:300,400,500" rel="stylesheet">
  <meta charset="utf-8">
  <meta name="viewport"
        content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=0" />
...

我在 StackOverflow 上搜索,有人提到 base href 应该匹配代码中的任何其他 base url,所以我确保它都匹配以防万一。

在服务器上,当我部署时,我使用代码 'npm run start:prod' 和 'start:prod' 运行以下代码:

npm install && npm run clean:dist && ng build --aot --prod && tsc -p ./server && nohup node bin/www &

这段代码是由前同事创建的,从那时起,我就毫无问题地使用了这段代码。

只是为了检查,我恢复到在服务器中工作的提交,部署它,它工作正常。然后,我再次取消了最近的提交,我注意到一个警告,上面写着

错误:您对以下文件的本地更改将被合并覆盖:package-lock.json。请在合并之前提交您的更改或存储它们。

我把它藏起来是因为我认为在 localhost 中工作的 package-lock.json 也应该在服务器中工作,但这就是问题所在吗?或者是别的什么?

【问题讨论】:

  • 请仔细检查 是否包含在您的任何 app.html 中。 angular.io/guide/deployment了解更多详情
  • @RED.Skull 感谢您的快速回复。 app.html 是指 index.html 加上 *.component.html?

标签: html node.js angular git npm


【解决方案1】:

对于任何可能正在经历我所经历的人,

我的问题出在 express.ts 中。

我添加了以下代码来修复 cors 问题,但它以某种方式摆脱了 index.html。

app.use(function (req, res, next) {
   res.setHeader('Access-Control-Allow-Origin', '*');
   res.setHeader('Access-Control-Allow-Methods', 'GET, POST, OPTIONS');
   res.setHeader('Access-Control-Allow-Headers', 'Content-Type');
   res.setHeader('Access-Control-Allow-Credentials', 'true');
   res.setHeader('Content-Type', 'application/json');

   next();
});

在我评论这些行之后,index.html 在服务器中正常工作。

如果有人遇到此类问题,请考虑“base href”以外的其他可能性,例如我的情况。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-08-18
    • 1970-01-01
    • 2022-01-21
    • 1970-01-01
    • 2019-12-13
    相关资源
    最近更新 更多