Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angular/29.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Angular 角度9-始终添加一个“/&引用;在index.html中styles.css路径前面,在构建--prod之后_Angular - Fatal编程技术网

Angular 角度9-始终添加一个“/&引用;在index.html中styles.css路径前面,在构建--prod之后

Angular 角度9-始终添加一个“/&引用;在index.html中styles.css路径前面,在构建--prod之后,angular,Angular,我正在寻找一种方法来告诉angular,当在生产环境中构建时,它应该始终在index.html中的样式路径前面放一条斜线。 我需要它来修复程序包中的一个bug,否则它将无法工作。所以现在我正在手动修复它,就像这样 ... <base href="/"> <link rel="stylesheet" href="styles.67b074ecf30df99829ee.css"> </head>

我正在寻找一种方法来告诉angular,当在生产环境中构建时,它应该始终在index.html中的样式路径前面放一条斜线。 我需要它来修复程序包中的一个bug,否则它将无法工作。所以现在我正在手动修复它,就像这样

...
  <base href="/">
  <link rel="stylesheet" href="styles.67b074ecf30df99829ee.css">
</head>
。。。
。。。

如何在为生产构建时自动执行此操作?

我相信您可以使用
包中的.json来完成此操作,但可能有更好的角度/网页包方式

package.json
中:

{
  ...
  "scripts": {
    "ng": "ng",
    "start": "ng serve",
    "build": "ng build",
    "my-build": "ng build --prod && node prepend_slashes.js"
  }
}
然后在
prepend_slashes.js
中,您可以使用类似的方法来操作HTML文件并替换您要查找的所有事件

然后可以使用
npm my build
运行脚本

下面是一个小示例,让您开始学习:

const cheerio=require('cheerio');
常数fs=要求('fs');
const indexFilePath='./path/to/build/index.html';
//阅读'index.html`
readFile(indexFilePath,'utf8',函数(err,data){
如果(错误){
返回console.log(err);
}
//将'index.html'加载到cheerio对象中。
const$=cheerio.load(数据);
//使用类似jquery的语法来操作ChereIO对象。
//查找href以`style'开头的所有链接`
$(“链接[href^='styles.]”)。每个(函数(){
//预编/
$(this.attr('href','/'+$(this.attr('href'));
})
//覆盖索引文件。
writeFile(indexFilePath,$.html(),函数(err){
if(err)返回console.log(err);
log('Successfully rewrite',indexFilePath);
});
}

非常感谢。但我认为缺少了一些内容。我正在用/记录实际更改,但之后的文件仍然缺少它。我必须在以后以某种方式保存它吗?哦,对!!!是的,抱歉,您必须覆盖该文件;p我已更新了我的答案:)
{
  ...
  "scripts": {
    "ng": "ng",
    "start": "ng serve",
    "build": "ng build",
    "my-build": "ng build --prod && node prepend_slashes.js"
  }
}