【问题标题】:Angular Schematics, code to modify angular.json is not coming togetherAngular Schematics,修改 angular.json 的代码没有放在一起
【发布时间】:2019-09-02 16:48:07
【问题描述】:

我一直在使用原理图来生成标准化项目。我遇到了几个问题,都与修改 angular.json 文件有关

以下代码是我尝试过的示例,在一个可以工作的函数中执行(除了另一件事),其中的所有内容都会执行,除了这部分会引发错误:

无法读取未定义的属性“includePaths”

这对我来说没有任何意义。

您在这里看到的另一个问题是我似乎无法捕获项目的名称,因为我需要修改 angular.json 文件中“architect”中的内容,我必须跳过项目名称,我的解决方法是手动输入项目名称为

ng add myschemat --name=new-app

但是我应该能够捕获这个属性,我已经尝试过并且确实可以在控制台中看到它,但是在替换时 options.name 用于 p(来自已评论的 forEach 函数)它不起作用并显示无法获取 undefined 的架构师属性em>

import {NodePackageInstallTask} from '@angular-devkit/schematics/tasks';

[...]


    if(_host.exists('angular.json')){
      const angularStr = _host.read('angular.json') !.toString('utf-8')
      const angular = JSON.parse(angularStr)
      const projs = (Object.keys( angular['projects'] )).slice(0, 1); //also tried [0]

      console.log("App Detection & your options: \n", projs, "\n", options)
      // projs.forEach((p: string | number) => {
        angular['projects'][options.name]['architect']['build']['options']['stylePreprocessorOptions'] = 
        { 
          "includePaths": ["src/scss/"] 
        }
        angular['projects'][options.name]['architect']['build']['options']['es5BrowserSupport']= true;
        angular['projects'][options.name]['architect']['build']['options']['styles'].push(
          [
            "node_modules/devextreme/dist/css/dx.common.css",
            "node_modules/devextreme/dist/css/dx.light.css",
            "src/styles.scss"
          ]
        )
      // });

      _host.overwrite('angular.json', JSON.stringify(angular, null, '\t'))

      _context.addTask( new NodePackageInstallTask )
    }

应该是添加对象属性

"styles": [
  "node_modules/devextreme/dist/css/dx.common.css",
  "node_modules/devextreme/dist/css/dx.light.css",
  "src/styles.scss"
],
"scripts": [],
"es5BrowserSupport": true,
"stylePreprocessorOptions": {
  "includePaths": [
    "src/scss/"
  ]
}

然而,当我编译 ng add myschemat --name=new-app

安装停止并简单显示:Cannot read property 'includePaths' of undefined

这很奇怪,因为它实际上可以工作,我只是添加了几个样式和真正的属性以及对 package.json 的一些更改(即使这些发生在此 angular.json 修改之前,它们似乎也没有改变任何东西

这里有什么我肯定错过的吗?

【问题讨论】:

    标签: angular angular-schematics


    【解决方案1】:

    这不是一个解决方案,但我刚刚创建了另一个项目,复制了所有内容并开始工作。

    【讨论】:

      猜你喜欢
      • 2018-10-23
      • 2019-03-25
      • 2016-02-01
      • 2019-06-21
      • 1970-01-01
      • 1970-01-01
      • 2016-10-26
      • 2019-10-26
      • 2022-12-18
      相关资源
      最近更新 更多