简体   繁体   English

Angular 在多个应用程序内共享资产

[英]Angular shared assets within multiple applications

I am working on multiple small apps, which will share common and shared modules, and assets.我正在开发多个小型应用程序,它们将共享通用和共享模块以及资产。

The part about how to create project structure was answered here: https://stackoverflow.com/a/61254557/1351452关于如何创建项目结构的部分在这里得到了回答: https://stackoverflow.com/a/61254557/1351452

My project folders structure is as follow: -root --projects ---app1 ---app2 ---library我的项目文件夹结构如下:-root --projects ---app1 ---app2 ---library

  1. I want to share assets (images and fonts) between apps and library (for example I have header component with logo)我想在应用程序和库之间共享资产(图像和字体)(例如,我有带有徽标的 header 组件)
  2. I want to have one place for all the css (SCSS).我想为所有 css (SCSS) 提供一个地方。 Some compoents from shared library have also SCSS, but I think I should keep it separate (because within component, css code is added to index file tag)共享库中的一些组件也有 SCSS,但我认为我应该将其分开(因为在组件内,css 代码已添加到索引文件标签中)
  3. Where should I keep that shared-assets folder, how to configure it for build and how to access from each app (import scss and get images and fonts).我应该在哪里保存共享资产文件夹,如何配置它以进行构建以及如何从每个应用程序访问(导入 scss 并获取图像和字体)。

Put assets in root folder: root/assets/将资产放在根文件夹中: root/assets/

and change the path to assets in angular.json并更改angular.json中资产的路径

{
  ...
  "projects": {
    "app1": {
      "projectType": "application",
      ...
      "architect": {
        "build": {
          ...
          "options": {
            ...
            "assets": [
              //  change this
              // "projects/app1/src/assets",
              "assets",
            ],
          }
        }
      }
    },
    "app2": {
      "projectType": "application",
      ...
      "architect": {
        "build": {
          ...
          "options": {
            ...
            "assets": [
              //  change this
              // "projects/app2/src/assets",
              "assets",
            ],
          }
        }
      }
    }
  }
}

I tried to achieve pretty much the same thing.我试图实现几乎相同的目标。 However, I do not build my library.但是,我不建立我的图书馆。 I just link to the library.module and use the components like that.我只是链接到 library.module 并使用这样的组件。

And now, for the shared assets:现在,对于共享资产:

In angular.json , I added the library's.scss to the app's build options, so it looked like this:angular.json中,我将 library's.scss 添加到应用程序的构建选项中,所以它看起来像这样:

"styles": [
  "projects/library/src/styles.scss",
  "projects/app1/src/styles.scss"
],

I did not add the library's asset folder to the project's asset section because according to CLI "tHaT DoEsN't WoRk" ( The assets asset path must start with the project source root. )我没有将库的资产文件夹添加到项目的资产部分,因为根据 CLI “tHaT DoEsN't WoRk”( The assets asset path must start with the project source root.

Now what?.怎么办?。 I realised the font files I declared in my library's style.scss got copied: This lead me to this thought, What happens?我意识到我在库的 style.scss 中声明的字体文件被复制了:这让我想到,会发生什么? if I also reference my assets in that file?如果我还在该文件中引用我的资产? So I put this in the project/library/src/styles.scss :所以我把它放在project/library/src/styles.scss

// fixes for library assets (they're not included if not listed here)
.its-2020-and-this-still-doesnt-work-out-of-the-box {
  background-image: url('assets/my-shared-logo.svg');
}

Then I was able to reference these assets from within my library's components like this:然后我可以从我的库组件中引用这些资产,如下所示:

<!-- library assets must be loaded from root level -->
<img src="my-shared-logo.svg" alt="" />

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

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