【问题标题】:Building angular library at ng serve在 ng serve 构建 Angular 库
【发布时间】:2020-05-31 04:08:23
【问题描述】:

我正在为我的 Angular 库开发而苦苦挣扎。首先,我正在构建一个 Angular 8 库(当前 cli 版本 9),并且我希望在为使用该库的演示应用程序提供服务时实时看到我的库代码更改。

当前行为:当我对库源代码进行更改时,角度演示应用程序会重新加载,但我没有看到任何更改生效。此行为主要应用于 css 更改,而不是每次都应用于 .ts 文件。

预期行为:能够看到我对库源代码所做的每一项更改。

当前的 angular.json 文件

    {
  "$schema": "./node_modules/@angular/cli/lib/config/schema.json",
  "version": 1,
  "newProjectRoot": "projects",
  "projects": {
    "bDataTableDemo": {
      "projectType": "application",
      "schematics": {
        "@schematics/angular:component": {
          "style": "scss"
        }
      },
      "root": "",
      "sourceRoot": "src",
      "prefix": "app",
      "architect": {
        "build": {
          "builder": "@angular-devkit/build-angular:browser",
          "options": {
            "outputPath": "dist/bDataTableDemo",
            "index": "src/index.html",
            "main": "src/main.ts",
            "polyfills": "src/polyfills.ts",
            "tsConfig": "tsconfig.app.json",
            "aot": false,
            "assets": [
              "src/favicon.ico",
              "src/assets"
            ],
            "styles": [
              "node_modules/bootstrap/dist/css/bootstrap.min.css",
              "src/styles.scss"
            ],
            "scripts": [
              "node_modules/jquery/dist/jquery.min.js"
            ]
          },
          "configurations": {
            "production": {
              "fileReplacements": [
                {
                  "replace": "src/environments/environment.ts",
                  "with": "src/environments/environment.prod.ts"
                }
              ],
              "optimization": true,
              "outputHashing": "all",
              "sourceMap": false,
              "extractCss": true,
              "namedChunks": false,
              "aot": true,
              "extractLicenses": true,
              "vendorChunk": false,
              "buildOptimizer": true,
              "budgets": [
                {
                  "type": "initial",
                  "maximumWarning": "2mb",
                  "maximumError": "5mb"
                },
                {
                  "type": "anyComponentStyle",
                  "maximumWarning": "6kb",
                  "maximumError": "10kb"
                }
              ]
            }
          }
        },
        "serve": {
          "builder": "@angular-devkit/build-angular:dev-server",
          "options": {
            "browserTarget": "bDataTableDemo:build"
          },
          "configurations": {
            "production": {
              "browserTarget": "bDataTableDemo:build:production"
            }
          }
        },
        "extract-i18n": {
          "builder": "@angular-devkit/build-angular:extract-i18n",
          "options": {
            "browserTarget": "bDataTableDemo:build"
          }
        },
        "test": {
          "builder": "@angular-devkit/build-angular:karma",
          "options": {
            "main": "src/test.ts",
            "polyfills": "src/polyfills.ts",
            "tsConfig": "tsconfig.spec.json",
            "karmaConfig": "karma.conf.js",
            "assets": [
              "src/favicon.ico",
              "src/assets"
            ],
            "styles": [
              "src/styles.scss"
            ],
            "scripts": []
          }
        },
        "lint": {
          "builder": "@angular-devkit/build-angular:tslint",
          "options": {
            "tsConfig": [
              "tsconfig.app.json",
              "tsconfig.spec.json",
              "e2e/tsconfig.json"
            ],
            "exclude": [
              "**/node_modules/**"
            ]
          }
        },
        "e2e": {
          "builder": "@angular-devkit/build-angular:protractor",
          "options": {
            "protractorConfig": "e2e/protractor.conf.js",
            "devServerTarget": "bDataTableDemo:serve"
          },
          "configurations": {
            "production": {
              "devServerTarget": "bDataTableDemo:serve:production"
            }
          }
        }
      }
    },
    "ng-bootstrap-table": {
      "projectType": "library",
      "root": "projects/ng-bootstrap-table",
      "sourceRoot": "projects/ng-bootstrap-table/src",
      "prefix": "lib",
      "architect": {
        "build": {
          "builder": "@angular-devkit/build-ng-packagr:build",
          "options": {
            "tsConfig": "projects/ng-bootstrap-table/tsconfig.lib.json",
            "project": "projects/ng-bootstrap-table/ng-package.json"
          }
        },
        "test": {
          "builder": "@angular-devkit/build-angular:karma",
          "options": {
            "main": "projects/ng-bootstrap-table/src/test.ts",
            "tsConfig": "projects/ng-bootstrap-table/tsconfig.spec.json",
            "karmaConfig": "projects/ng-bootstrap-table/karma.conf.js"
          }
        },
        "lint": {
          "builder": "@angular-devkit/build-angular:tslint",
          "options": {
            "tsConfig": [
              "projects/ng-bootstrap-table/tsconfig.lib.json",
              "projects/ng-bootstrap-table/tsconfig.spec.json"
            ],
            "exclude": [
              "**/node_modules/**"
            ]
          }
        }
      }
    }
  },
  "defaultProject": "bDataTableDemo"
}

还有我的 tsconfig.json 文件

{
  "compileOnSave": false,
  "compilerOptions": {
    "baseUrl": "./",
    "outDir": "./dist/out-tsc",
    "sourceMap": true,
    "declaration": false,
    "downlevelIteration": true,
    "experimentalDecorators": true,
    "module": "esnext",
    "moduleResolution": "node",
    "importHelpers": true,
    "target": "es2015",
    "typeRoots": [
      "node_modules/@types"
    ],
    "lib": [
      "es2018",
      "dom"
    ],
    "paths": {
      "ng-bootstrap-table": [
        "dist/ng-bootstrap-table"
      ],
      "ng-bootstrap-table/*": [
        "dist/ng-bootstrap-table/*"
      ]
    }
  },
  "angularCompilerOptions": {
    "fullTemplateTypeCheck": true,
    "strictInjectionParameters": true
  }
}

为了获得预期的行为,我应该改变什么?

附:我是 Angular 库的新手,所以我还找不到解决方案。

提前谢谢你!!

【问题讨论】:

    标签: angular build angular-library serve


    【解决方案1】:

    您应该编辑 tsconfig 中的路径以指向您的库的 index.ts 而不是 dist/ - 这是使用 ng build my-library 生成的构建工件:

    "my-library" : [ "libs/my-library/index.ts" ]
    

    【讨论】:

    • 感谢您的回复!我已经通过在我的 lib 的 src 文件夹中添加一个 index.ts 文件并从 pulic-api.ts 文件中导出所有内容来尝试此操作,但似乎根本不起作用。我在演示项目中导入 lib 的方式是否还有其他错误?
    • 是的!但一切都改变了:/
    猜你喜欢
    • 2018-02-07
    • 1970-01-01
    • 2020-11-13
    • 2019-06-16
    • 2017-07-07
    • 2017-02-12
    • 2019-02-04
    • 2017-05-11
    • 1970-01-01
    相关资源
    最近更新 更多