标签: angular-standalone-components

如何使用 forRoot() 将 NgModule 库导入到 Angular 独立组件中?

我目前正在开发一个使用独立组件的 Angular 应用程序(版本 16.1.3)。该应用程序还依赖于使用 NgModules 的 Angular 库。我正在尝试将此库中的组件导入到我的应用程序中,其中该库还包含一个从应用程序中检索环境数据的函数。

这是ui-lib.module.ts图书馆的

export class UiLibModule {
    public static forRoot(environment: any): ModuleWithProviders<UiLibModule> {
        return {
            ngModule: UiLibModule,
            providers: [
                {
                    provide: 'environment',
                    useValue: environment,
                },
            ],
        };
    }
}
Run Code Online (Sandbox Code Playgroud)

在我的主应用程序中,我将库导入到我的main.ts文件中,如下所示:

bootstrapApplication(AppComponent, {
  providers: [
    importProvidersFrom(
    ...
    UiLibModule.forRoot(environment),
    ...)
    ]
...});
Run Code Online (Sandbox Code Playgroud)

但是,当我尝试使用库中的组件时遇到问题:

  • 如果我在主应用程序内但在 之外使用库组件<router-outlet></router-outlet>,则一切都会按预期工作。
  • 如果我在 中路由的组件中使用我的库中的相同组件<router-outlet></router-outlet>,我就会遇到问题。组件显示正确,但离开页面时,路由器无法从页面中删除该组件。此外,控制台中不会显示任何错误。

以下是我在独立组件中导入 UiLibModule 的方法:

...
@Component({
  selector: "app-root",
  templateUrl: "./app.component.html",
  styleUrls: ["./app.component.scss"],
  imports: [
    UiLibModule
  ]
Run Code Online (Sandbox Code Playgroud)

.forRoot()我已经使用使用该函数的库和不使用该函数的库对此进行了测试。似乎未使用的库.forRoot()按预期工作,而使用的库则遇到了上述问题。

我的问题涉及我的图书馆的正确和完整包含。我不确定当前导入库的方式是否准确且全面。具体来说,我很好奇是否有一种方法可以通过独立组件直接添加环境。

此外,我不确定是否需要在独立组件中再次声明环境,因为它已经在 …

ng-modules angular angular-router angular-library angular-standalone-components

19
推荐指数
1
解决办法
3623
查看次数

NullInjectorError - 根独立组件未正确导入 HttpClientModule

我有一个问题,我想重写我的应用程序代码并将主模块更改为 main.ts 中指向的主根独立组件。

一切正常,但我对 HttpClientModule 有问题。我在 root 中提供的一项服务中使用 HttpClient。在 MainComponent 中,我将 HttpClientModules 添加到导入数组中,但随后出现错误,表示我没有向应用程序提供 HttpClientMoudle,并且此服务无法使用它。

Uncaught (in promise): NullInjectorError: R3InjectorError(Standalone[MainComponent])[MainService -> MainService -> HttpClient -> HttpClient]:
NullInjectorError: No provider for HttpClient!
Run Code Online (Sandbox Code Playgroud)

当我将此 HttpClientModule 添加到 AppRoutingModule 时,我也将其添加到 main.ts 中

bootstrapApplication(MainComponent, {
  providers: [importProvidersFrom(RoutingModule)],
});
Run Code Online (Sandbox Code Playgroud)

效果很好,服务没有任何问题。

这是包含此问题的基本示例的代码,有人可以解释为什么导入到根独立组件在这种情况下不起作用,但导入到 RoutngModule 可以解决问题吗?

https://stackblitz.com/edit/angular-fhpbvm?file=src/main.ts

angular-services angular angular-standalone-components

15
推荐指数
1
解决办法
9577
查看次数

如何在独立组件中使用 Ngxs?

是否可以在独立组件中使用 Ngxs?我尝试NgxsModule通过以下方式导入:

@Component({
  ...
  imports: [
    ...
    NgxsModule.forFeature([MyState]),
...
Run Code Online (Sandbox Code Playgroud)

@Component({
  ...
  imports: [
    ...
    NgxsModule.forRoot([MyState]),
...
Run Code Online (Sandbox Code Playgroud)

但两者都给我以下错误消息:(Type 'ModuleWithProviders<NgxsFeatureModule>' is not assignable to type 'any[] | Type<any>'NgxsRootModule在这种forRoot情况下)。还提供了更深入的错误消息:'imports' contains a ModuleWithProviders value, likely the result of a 'Module.forRoot()'-style call. These calls are not used to configure components and are not valid in standalone component imports - consider importing them in the application bootstrap instead.

这是否受支持,但我的语法错误?或者这是不支持的?如果不支持,拦截器是什么?

angular ngxs angular-standalone-components

11
推荐指数
2
解决办法
6328
查看次数

使用独立组件 Angular 15 发现意外的合成属性 @transitionMessages

我正在将角度应用程序迁移到standalone component. 删除了ngModuleapp.module.ts文件

\n

主要.ts

\n
bootstrapApplication(AppComponent,{\n  providers:[importProvidersFrom(FalconCoreModule.forRoot(environment)),\n             importProvidersFrom(RouterModule.forRoot(routes))]\n}).catch(err => console.error(err));\n
Run Code Online (Sandbox Code Playgroud)\n

应用程序组件.scss

\n
@Component({\n  selector: 'app-root',\n  templateUrl: './app.component.html',\n  styleUrls: ['./app.component.scss'],\n  standalone: true,\n  imports:[RouterModule,FalconCoreModule]\n})\nexport class AppComponent {}\n
Run Code Online (Sandbox Code Playgroud)\n

自动完成.component.ts

\n
@Component({\n  selector: 'app-auto-complete',\n  templateUrl: './auto-complete.component.html',\n  styleUrls: ['./auto-complete.component.scss'],\n  standalone: true,\n  imports:[FalconCoreModule,CodeGeneratorComponent,HighlightModule,CodeButtonComponent]\n})\nexport class AutoCompleteComponent\n  extends BaseFormComponent<string>\n  implements OnInit {}\n
Run Code Online (Sandbox Code Playgroud)\n

错误

\n
core.mjs:9229 ERROR Error: Unexpected synthetic property @transitionMessages found. Please make sure that:\n  - Either `BrowserAnimationsModule` or `NoopAnimationsModule` are imported in your application.\n  - There …
Run Code Online (Sandbox Code Playgroud)

angular angular-standalone-components angular15

11
推荐指数
2
解决办法
1万
查看次数

有人尝试将 InMemoryWebAPI 与独立组件一起使用吗?

我正在使用带有独立组件的 Angular v16。我像在基于 NgModule 的项目中一样实现了 InMemoryWebAPI,但它似乎不起作用。我不断得到404 not found

有人尝试过这个并通过独立引导成功使用 InMemoryWebAPI 吗?为了让它与独立组件一起工作,我还需要做其他什么不同的事情吗?

这是我的 app.config.ts

export const appConfig: ApplicationConfig = {
  providers: [
    importProvidersFrom(InMemoryWebApiModule.forRoot(AppData, { delay: 1000 })),
    provideHttpClient(),
    provideRouter(routes, withComponentInputBinding())
  ]
};
Run Code Online (Sandbox Code Playgroud)

这是我的应用程序数据

export class AppData implements InMemoryDbService {

  createDb(): { products: Product[] } {
    const products = ProductData.products;
    console.log(products);
    return { products };
  }

}
Run Code Online (Sandbox Code Playgroud)

这是我的服务:

  private sub = this.#http.get<Product[]>('api/products')

Run Code Online (Sandbox Code Playgroud)

感谢您的任何想法或调试技巧。

编辑:我在这里有一个 stackblitz:https ://stackblitz.com/edit/github-crud-signals-djk

该服务当前已注释掉 http 调用,因为它们生成 404 错误。相反,它对数据进行硬编码。

angular angular-in-memory-web-api angular-standalone-components

9
推荐指数
1
解决办法
603
查看次数

如何在功能模块中使用独立组件、管道或指令

我将我的 Angular 项目更新为 Angular 14。现在我想要一些standalone components,,pipes或者directives

我有一个名为ProductModule的特色模块,并且希望在此模块中使用名为uppercase的独立管道。

// structure
---Product
          ---product.component
          ---product.service
          ---product.module.ts

---StandabloneFolder
                    ---uppercase.pipe.ts
Run Code Online (Sandbox Code Playgroud)

我的大写管子

@Pipe({
    name: 'uppercase',
    standalone: true,
})
export class UppercasePipe implements PipeTransform {
    transform(value: string): string {
        return "UPPERCASE_INPUT"
    }
}
Run Code Online (Sandbox Code Playgroud)

在product.component.html中

{{'Javascript Addicted' |uppercase}}
Run Code Online (Sandbox Code Playgroud)

出现以下错误:

找不到名称为“大写”的管道product.component.ts(6, 39):组件 ProductComponent 的模板中发生错误。

笔记:

standalone: true如果我添加到Product.Component并从声明数组中删除ProductComponent,这个问题就会得到解决。

angular angular14 angular-standalone-components

7
推荐指数
1
解决办法
1万
查看次数

如何将模块导入独立指令

imports属性在装饰器上可用@Component,但在@Directive装饰器上不可用。

还有另一种方法可以将模块导入到独立指令中吗?

angular-directive angular angular-standalone-components

6
推荐指数
1
解决办法
1525
查看次数

独立组件中的模拟独立指令

我正在尝试为独立组件编写单元测试并模拟其依赖性。该独立组件具有以下内容:

import { Component, OnInit } from '@angular/core';
import { CommonModule } from '@angular/common';
import { DemoDirectiveDirective } from './demo-directive.directive';

@Component({
  selector: 'app-demo-cmp',
  standalone: true,
  imports: [CommonModule,DemoDirectiveDirective],
  templateUrl: './demo-cmp.component.html',
})
export class DemoCmpComponent implements OnInit {
  constructor() {}

  ngOnInit(): void {}
}
Run Code Online (Sandbox Code Playgroud)

并且 DemoDirectiveDirective 具有以下内容:

import { Directive, ElementRef } from '@angular/core';

@Directive({
  selector: '[appDemoDirective]',
  standalone: true,
})
export class DemoDirectiveDirective {
  constructor(private hostElement: ElementRef) {
    this.hostElement.nativeElement.innerHTML += 'Update from directive! ';
  }
}
Run Code Online (Sandbox Code Playgroud)

DemoCmpComponent我想这样测试:

import { ComponentFixture, TestBed …
Run Code Online (Sandbox Code Playgroud)

jestjs angular angular-standalone-components

5
推荐指数
2
解决办法
2358
查看次数

Angular 15 参考错误:初始化前无法访问“组件 A”

我在相互导入独立组件时遇到一个问题。

成分A。

@Component({
  standalone: true,
  selector: 'app-a',
  templateUrl: './a.component.html',
  styleUrls: ['./a.scss'],
  imports: [
    BComponent
  ]
})
export class AComponent implements OnInit {

}
Run Code Online (Sandbox Code Playgroud)

组分B

@Component({
  standalone: true,
  selector: 'app-b',
  templateUrl: './b.component.html',
  styleUrls: ['./b.scss'],
  imports: [
    AComponent
  ]
})
export class BComponent implements OnInit {

}
Run Code Online (Sandbox Code Playgroud)

早些时候,这个解决方案对我来说工作得很好,因为使用模板访问这些组件。但现在收到此错误。

参考错误:初始化之前无法访问“AComponent”。

高度赞赏上述上下文中的任何帮助。

webpack angular angular-standalone-components

5
推荐指数
1
解决办法
2671
查看次数

在 Angular 17 的独立组件中使用 @ngx-translate

我在 Angular 17 中使用独立组件。当我使用模块架构时,我没有遇到这个问题。我将其添加到导入中AppModule并且效果很好。

\n
imports: [\n TranslateModule.forRoot(loader)\n],\n
Run Code Online (Sandbox Code Playgroud)\n

但是如果我添加TranslateModule.forRoot(loader)独立组件

\n
@Component({\n selector: 'app-main-work-space',\n standalone: true,\n imports: [\n  // @ts-ignore\n  TranslateModule.forRoot(loader)\n ],\n templateUrl: './main-work-space.component.html',\n styleUrl: './main-work-space.component.scss'\n})\n
Run Code Online (Sandbox Code Playgroud)\n

结果我犯了这个错误。

\n

\xe2\x9c\x98 [ERROR] TS-992012: 'imports' contains a ModuleWithProviders value, likely the result of a 'Module.forRoot()'-style call. These calls are not used to configure components and are not valid in standalone component imports - consider importing them in the application bootstrap instead.

\n

我尝试添加@ts-ignore但没有帮助。

\n

我该如何修复它?

\n

ngx-translate angular angular-standalone-components

5
推荐指数
1
解决办法
2257
查看次数

尝试从模块加载“独立”组件时出错

我想将新的独立组件注入CrashReportsComponent到我的项目中。但是,当我尝试从模块路由连接组件时,出现错误,该组件类型与 NgModule 不同 NgModule 'CrashReportsComponent' is not a subtype of 'NgModuleType'

按照开发人员的计划,我可以连接一个独立的组件而不是模块,并且这应该可以在模块中不进行任何更改的情况下工作。

import { NgModule } from '@angular/core';
import { RouterModule, Routes } from '@angular/router';
import { CommonLayoutComponent } from './common-layout.component';
import { FilterKind } from '../core/services/filter.service';

const routes: Routes = [
    {
        path: '',
        component: CommonLayoutComponent,
        children: [
            {
                path: 'crash-reports',
                loadChildren: () =>
                    import('../pages/crash-reports/crash-reports.component').then((m) => m.CrashReportsComponent),
            },
        ],
    },
];

@NgModule({
    imports: [RouterModule.forChild(routes)],
    exports: [RouterModule],
})
export class CommonLayoutRoutingModule {}
Run Code Online (Sandbox Code Playgroud)
import { Component } from …
Run Code Online (Sandbox Code Playgroud)

angular angular14 angular-standalone-components

3
推荐指数
1
解决办法
4352
查看次数

Angular 15 独立 AppComponent 和 AngularFireAuth

我正在尝试设置一个 Angular Web 应用程序,其中 AppComponent 使用 Firebase 身份验证(和数据库)本身是独立的。因此没有NgModules。

我在main.ts中添加了

bootstrapApplication(AppComponent, {
  providers: [
    importProvidersFrom(
      provideFirebaseApp(() => initializeApp(firebaseConfig)),

    ),
    importProvidersFrom(
      provideFirestore(() => getFirestore())
    ),
  ...
  ]

Run Code Online (Sandbox Code Playgroud)

在 AppComponent 中我添加了其他导入:

@Component({
  standalone: true,
  selector: 'app-root',
  template: `<router-outlet></router-outlet>`,
  imports: [
    RouterModule,
    AngularFireAuthModule,
    AngularFirestoreModule,
    AngularFireStorageModule,
    AngularFireDatabaseModule
  ]
})
export class AppComponent {
Run Code Online (Sandbox Code Playgroud)

我没有任何特定的错误,但是一旦我尝试注入构造函数,AngularFireAuth就会收到以下错误:

在此输入图像描述

我也尝试过添加

importProvidersFrom(AngularFireAuthModule),
Run Code Online (Sandbox Code Playgroud)

main.ts它没有帮助。

有谁知道我如何解决这个问题或者知道我做错了什么?

提前致谢 :)

firebase angularfire2 angular angular-standalone-components angular15

2
推荐指数
1
解决办法
1640
查看次数

将现有的 Angular 项目迁移到独立项目

我的 Angular 项目是版本 13,我将其逐步迁移到 Angular 16。现在我想让它完全独立,然后将其迁移到版本 17。

是否有任何我可以使用的实用程序或者我必须手动执行?

typescript angular angular-migration angular-standalone-components

1
推荐指数
1
解决办法
704
查看次数