如何在 ng 与 Angular 一起使用时将静态文件(json)包含到资产文件夹中

时间:2021-01-27 16:51:01

标签: angular

我在 config.json 中有一个 apps/my-app/src/assets/config.json 文件,我希望在我 ng serve 时可以在编译源中使用该文件。

我在 angular.json 文件中添加了一个 glob 条目:

 "architect": {
        "build": {
          "builder": "@angular-devkit/build-angular:browser",
          "options": {
            "outputPath": "dist/apps/my-app",
            "index": "apps/my-app/src/index.html",
            "main": "apps/my-app/src/main.ts",
            "polyfills": "apps/my-app/src/polyfills.ts",
            "tsConfig": "apps/my-app/tsconfig.app.json",
            "aot": true,
            "assets": [
              "apps/my-app/src/favicon.ico",
              "apps/my-app/src/assets",
              {
                "glob": "config.json",
                "input": "apps/my-app/src/assets/",
                "output": "/assets/"
              }
            ],
            "styles": [
              "apps/my-app/src/styles.scss"
            ],
            "scripts": []
          },

这似乎不起作用,因为当我在 chrome devtools 中检查源时,没有 config.json(但是,我的图像正确存在)。 我怎样才能得到我的 config.json

谢谢!

1 个答案:

答案 0 :(得分:0)

尝试将所有图像移动到 apps/my-app/src/assets/images/

将其他 Json 文件放到另一个文件夹中,例如 apps/my-app/src/assets/myJsonFolder/

我有这个结构,当我为生产构建时,我的资产文件夹完全包含我在里面的那些文件夹和文件的结构

也试试那些配置

 "architect": {
        "build": {
          "builder": "@angular-devkit/build-angular:browser",
          "options": {
            "outputPath": "dist/apps/my-app",
            "index": "apps/my-app/src/index.html",
            "main": "apps/my-app/src/main.ts",
            "polyfills": "apps/my-app/src/polyfills.ts",
            "tsConfig": "apps/my-app/tsconfig.app.json",
            "aot": true,
            "assets": [
              "apps/my-app/src/favicon.ico",
              "apps/my-app/src/assets",
              {
                "glob": "**/*",    <-------------------
                "input": "apps/my-app/src/assets/",
                "output": "./assets"   <---------------
              }
            ],
            "styles": [
              "apps/my-app/src/styles.scss"
            ],
            "scripts": []
          },