【问题标题】:Using npm on angular project but it displays nothing when adding angular component在角度项目上使用npm,但添加角度组件时不显示任何内容
【发布时间】:2019-01-30 03:47:13
【问题描述】:

我正在使用在线找到的角度登录示例,我们使用 npm start 运行。我的问题是当我生成一个组件并将其添加到 app.modules.js 文件中时,该应用程序不会显示任何内容,只是在左上角加载了一个白页...。这可能是因为我使用的是 npm 吗?当我将它发布到 stackblitz 并添加侧边栏组件时,它可以工作,但是如果我通过 ng generate component test 添加一个组件,并且一旦我将它添加到 app.modules.js 文件中,它就会显示白屏,但一旦删除它就可以工作完全没问题。下面提供了 stackbliz 和原始 github 链接

stackblitz:https://stackblitz.com/edit/angular-ydiywh?file=src%2Fapp%2Fapp.module.ts

github:http://jasonwatmore.com/post/2018/06/14/nodejs-mongodb-simple-api-for-authentication-registration-and-user-management

当我运行 ng serve --open 时出现以下错误

安装模块后出错

ENOENT: no such file or directory, stat '/Users/xxx/Library/Mobile Documents/com~apple~CloudDocs//src/tsconfig.app.json'
Error: ENOENT: no such file or directory, stat '/Users/xxx/Library/Mobile Documents/com~apple~CloudDocs/src/tsconfig.app.json'
    at Object.statSync (fs.js:815:3)
    at AngularCompilerPlugin._setupOptions (/Users/xxx/Library/Mobile Documents/com~apple~CloudDocs/node_modules/@ngtools/webpack/src/angular_compiler_plugin.js:96:16)
    at new AngularCompilerPlugin (/Users/xxx/Library/Mobile Documents/com~apple~CloudDocs/node_modules/@ngtools/webpack/src/angular_compiler_plugin.js:61:14)
    at _createAotPlugin (/Users/xxx/Library/Mobile Documents/com~apple~CloudDocs/node_modules/@angular-devkit/build-angular/src/angular-cli-files/models/webpack-configs/typescript.js:41:12)
    at Object.getNonAotConfig (/Users/xxx/Library/Mobile Documents/com~apple~CloudDocs/node_modules/@angular-devkit/build-angular/src/angular-cli-files/models/webpack-configs/typescript.js:47:19)
    at BrowserBuilder.buildWebpackConfig (/Users/xxx/Library/Mobile Documents/com~apple~CloudDocs/node_modules/@angular-devkit/build-angular/src/browser/index.js:82:37)
    at DevServerBuilder.buildWebpackConfig (/Users/xxx/Library/Mobile Documents/com~apple~CloudDocs/node_modules/@angular-devkit/build-angular/src/dev-server/index.js:111:46)
    at MergeMapSubscriber.check_port_1.checkPort.pipe.operators_1.concatMap [as project] (/Users/xxx/Library/Mobile Documents/com~apple~CloudDocs/node_modules/@angular-devkit/build-angular/src/dev-server/index.js:38:40)
    at MergeMapSubscriber._tryNext (/Users/xxx/Library/Mobile Documents/com~apple~CloudDocs/node_modules/rxjs/internal/operators/mergeMap.js:65:27)
    at MergeMapSubscriber._next (/Users/xxx/Library/Mobile Documents/com~apple~CloudDocs/node_modules/rxjs/internal/operators/mergeMap.js:55:18)
    at MergeMapSubscriber.Subscriber.next (/Users/xxx/Library/Mobile Documents/com~apple~CloudDocs/node_modules/rxjs/internal/Subscriber.js:64:18)
    at TapSubscriber._next (/Users/xxx/Library/Mobile Documents/com~apple~CloudDocs/node_modules/rxjs/internal/operators/tap.js:62:26)
    at TapSubscriber.Subscriber.next (/Users/xxx/Library/Mobile Documents/com~apple~CloudDocsnode_modules/rxjs/internal/Subscriber.js:64:18)
    at MergeMapSubscriber.notifyNext (/Users/xxx/Library/Mobile Documents/com~apple~CloudDocsnode_modules/rxjs/internal/operators/mergeMap.js:84:26)
    at InnerSubscriber._next (/Users/xxx/Library/Mobile Documents/com~apple~CloudDocs/node_modules/rxjs/internal/InnerSubscriber.js:25:21)
    at InnerSubscriber.Subscriber.next (/Users/xxx/Library/Mobile/node_modules/rxjs/internal/Subscriber.js:64:18)

.angular.cli 文件

{
  "$schema": "./node_modules/@angular/cli/lib/config/schema.json",
  "version": 1,
  "newProjectRoot": "projects",
  "projects": {
    "new-cli": {
      "root": "",
      "sourceRoot": "src",
      "projectType": "application",
      "architect": {
        "build": {
          "builder": "@angular-devkit/build-angular:browser",
          "options": {
            "outputPath": "dist",
            "index": "src/index.html",
            "main": "src/main.ts",
            "tsConfig": "src/tsconfig.app.json",
            "polyfills": "src/polyfills.ts",
            "assets": [
              "src/assets"
            ],
            "styles": [],
            "scripts": []
          },
          "configurations": {
            "production": {
              "optimization": true,
              "outputHashing": "all",
              "sourceMap": false,
              "extractCss": true,
              "namedChunks": false,
              "aot": true,
              "extractLicenses": true,
              "vendorChunk": false,
              "buildOptimizer": true
            }
          }
        },
        "serve": {
          "builder": "@angular-devkit/build-angular:dev-server",
          "options": {
            "browserTarget": "new-cli:build"
          },
          "configurations": {}
        },
        "extract-i18n": {
          "builder": "@angular-devkit/build-angular:extract-i18n",
          "options": {
            "browserTarget": "new-cli:build"
          }
        },
        "lint": {
          "builder": "@angular-devkit/build-angular:tslint",
          "options": {
            "tsConfig": [],
            "exclude": []
          }
        }
      }
    },
    "new-cli-e2e": {
      "root": "e2e",
      "sourceRoot": "e2e",
      "projectType": "application"
    }
  },
  "defaultProject": "new-cli"
}

添加组件和运行 npm start 时 chrome 检查出错

Uncaught Error: Unexpected module 'SidebarModule' declared by the 
 module 'AppModule'. Please add a @Pipe/@Directive/@Component 
 annotation. at syntaxError 
 (webpack:///./node_modules/@angular/compiler/fesm5/compiler.js?:1270) 
 at eval 
 (webpack:///./node_modules/@angular/compiler/fesm5/compiler.js?:10888) 
 at Array.forEach (<anonymous>) at 
 CompileMetadataResolver.getNgModuleMetadata 
 (webpack:///./node_modules/@angular/compiler/fesm5/compiler.js?:10870) 
 at JitCompiler._loadModules 
 (webpack:///./node_modules/@angular/compiler/fesm5/compiler.js?:24112) 
 at JitCompiler._compileModuleAndComponents 
 (webpack:///./node_modules/@angular/compiler/fesm5/compiler.js?:24093) 
 at JitCompiler.compileModuleAsync 
 (webpack:///./node_modules/@angular/compiler/fesm5/compiler.js?:24053) 
 at CompilerImpl.compileModuleAsync 
 (webpack:///./node_modules/@angular/platform-browser- 
 dynamic/fesm5/platform-browser-dynamic.js?:158) at 
 PlatformRef.bootstrapModule 
 (webpack:///./node_modules/@angular/core/fesm5/core.js?:4649) at eval 
 (webpack:///./src/main.ts?:6)

【问题讨论】:

  • 1) 您在系统中全局使用哪个 cli 版本? 2)你有没有安装角CDK?
  • @VaibhavKumarGoyal 我添加了角度 cdk,我正在使用角度 6 cli
  • 为您的错误 @z123 发布了解决方案
  • 这个错误意味着它无法找到路径在.angular.cli.json 中的tsconfig.app.json请检查提到的路径是否正确@z123
  • @VaibhavKumarGoyal 我有一个 .angular.json 文件,它是正确的。我贴出来了

标签: javascript node.js angular npm angular6


【解决方案1】:

所以我去了你引用的网站并下载了 github 项目。

我按照页面上的说明进行操作,并且能够毫无问题地运行它。

我尝试通过“ng”添加组件但无法添加。我不断收到与 angular.json 无法定位等相关的各种错误。我不确定您为将组件放入其中做了什么,但这似乎与问题有关。 cli 无法开箱即用地运行这个项目,它似乎需要使用 npm。

查看一些源代码后,似乎这是 Webpack 上的 Angular,因此 CLI 不适合您。它也不允许您创建组件。

【讨论】:

  • 那么我该如何创建组件
  • 手动。基本上复制其中一个组件,例如登录/注册,重命名它,然后将其添加到应用程序组件以及您使用它的任何位置。
  • 我找到了一个更好的方法,正如 observablerxvs 提到的,我刚刚将整个项目上传到 stackblitx 并从那里导出,创建了一个新的 angular 项目并替换了不起作用的文件。
【解决方案2】:

以下是重现“本地”stackblitz 工作的步骤:

1 - 使用ng new myApp生成您的项目
2 - 通过导出按钮下载 stackblitz 项目,然后将文件解压缩到压缩文件夹中。
3 - 在提取的文件夹中,转到src 文件夹,然后在其中单击第二个src 文件夹。
4 - 复制appmain.ts 文件并将它们粘贴到第一个src 文件夹。
5 - 打开第二个src 文件夹中的typings.d 文件并复制其中的行。并将此行粘贴到第一个 src 文件夹中的 typings.d 文件中。
6 - 删除第二个src 文件夹。
7 - 将项目根目录中的src 文件夹替换为解压缩文件夹中的文件夹。
8 - 在您的项目根目录中运行此命令:npm install --save ng-sidebar
9 - 运行npm start

您现在可以创建多少组件,这仍然可以工作。

【讨论】:

    【解决方案3】:
    npm install --save-dev @angular-devkit/build-angular
    

    这应该可以解决您的开发工具包错误,它是 Angular 6 中引入的新依赖项,必须保存为开发依赖项

    【讨论】:

    • 您遇到的第二个错误意味着它无法找到路径在 .angular.cli.json 中的 tsconfig.app.json 请检查提到的路径是否正确@z123
    • 您也可以尝试使用ng new 命令创建一个新应用程序并尝试对该应用程序执行一次 ng-serve 是否可行
    • 当我使用 ng new 创建一个新项目并点击 ng serve 我得到了这个命令只能在 CLI 项目中运行。
    【解决方案4】:

    安装 Angular CLI:

    npm install @angular/cli
    

    然后从 app 目录运行它。它应该打开浏览器到 localhost:4200

    ng serve --open
    

    【讨论】:

    • 尝试我得到以下错误,我将在原始描述中发布
    • 当它说“找不到模块”时,只需 npm install “whatever module”。在错误情况下,它将是 npm install @angular-devkit/build-angular 已编辑:您可能还想使用 -g 标志。
    • @Collin Knapp 解决了模块错误,但现在我得到一个不同的错误,我将在上面发布
    • 好的。我将提交另一个答案。
    • 当我使用 npm start 运行项目时,我将发布一个我发现的错误,该错误显示在 chrome inspect 上
    猜你喜欢
    • 2016-08-19
    • 1970-01-01
    • 2021-01-04
    • 2021-02-03
    • 2019-01-26
    • 2019-03-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多