简体   繁体   English

Angular CLI 8 中是否有 --rebaseRootRelativeCssUrls 选项的替代方案?

[英]Is there an alternative for --rebaseRootRelativeCssUrls option in Angular CLI 8?

I had a lot of issues because my application is hosted inside a subfolder and for that reason the relative /assets/ path, which is referenced inside .scss files, doesn't work.我遇到了很多问题,因为我的应用程序托管在一个子文件夹中,因此在.scss文件中引用的相对/assets/路径不起作用。 The solution was to enable the --rebaseRootRelativeCssUrls option to get the expected behaviour.解决方案是启用--rebaseRootRelativeCssUrls选项以获得预期的行为。 With that option, the build process has adjusted my /assets/ paths by injecting the "--base-href" value and now the referenced images and fonts are loading correctly.使用该选项,构建过程通过注入“--base-href”值调整了我的/assets/路径,现在引用的图像和字体可以正确加载。

However, I see in the documentation that the option is deprecated and it will be removed in the next major release.但是,我在文档中看到该选项已被弃用,并将在下一个主要版本中删除。

https://angular.io/cli/build (search for --rebaseRootRelativeCssUrls ) https://angular.io/cli/build (搜索--rebaseRootRelativeCssUrls

So my question is, what is the alternative, is there any other way to get the same result?所以我的问题是,有什么替代方法,有没有其他方法可以获得相同的结果?

According to the release notes, the recommended method for transition is using relative URLs in your .scss files.根据发行说明,推荐的转换方法是在 .scss 文件中使用相对 URL。 This, to me, seems like a bad idea though.不过,在我看来,这似乎是个坏主意。 Here's a comment from the dev team on a bug logged about this issue.这是开发团队对记录的有关此问题的错误的评论。

https://github.com/angular/angular-cli/issues/14587#issuecomment-497368020 https://github.com/angular/angular-cli/issues/14587#issuecomment-497368020

Inside your angular.json you can set multiple assetsfolder 在您的angular.json内部,您可以设置多个资产文件夹

   "build": {
      "options": {
        "assets": [
          "src/favicon.ico",
          "src/the/other/folder/assets",
          "src/assets",
          {
            "glob": "**/*",
            "input": "src/the/other/folder/assets",
            "output": "./assets"
          },
        ],

this lets you have multiple assetsfolder. 这使您拥有多个资产文件夹。

I solved it by use path in this way:我通过这种方式使用路径解决了它:

background-image: url('../../../assets/images/icon.png');

Instead of this way:而不是这种方式:

background-image: url('/src/assets/images/img.png');

If your project is deployed in a subfolder you can pass that information to your build command如果您的项目部署在子文件夹中,您可以将该信息传递给您的build命令

I suggest you to take a look at --base-href我建议你看看--base-href

In development, you typically start the server in the folder that holds index.html.在开发中,您通常在包含 index.html 的文件夹中启动服务器。 That's the root folder and you'd add near the top of index.html because / is the root of the app.那是根文件夹,您需要在 index.html 顶部附近添加,因为 / 是应用程序的根目录。

But on the shared or production server, you might serve the app from a subfolder.但在共享或生产服务器上,您可能会从子文件夹提供应用程序。 For example, when the URL to load the app is something like mysite.com/my/app/, the subfolder is my/app/ and you should add to the server version of the index.html.例如,当加载应用程序的 URL 类似于 mysite.com/my/app/ 时,子文件夹是 my/app/,您应该添加到 index.html 的服务器版本。

You can modify the production base href with the following command:您可以使用以下命令修改生产基地 href:

ng build --prod --base-href /subfolder/

You can also set base-href to ./ which means the current directory in opposite to / which stands for root directory.您还可以将 base-href 设置为./ ,这表示当前目录与/相对,代表根目录。

Another possible solution would be to change href in your main index.html另一种可能的解决方案是更改主index.html href

<base href="./">

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

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