我有一个独立组件,我想在非独立组件中使用它,而不使该组件也独立.这可能吗?我try 过各种方法,但总是出错.

我有这个组件

import { ChangeDetectionStrategy, Component } from '@angular/core';

@Component({
  selector: 'app-three-d-viewer',
  standalone: true,
  templateUrl: './three-d-viewer.component.html',
  styleUrls: ['./three-d-viewer.component.css'],
  changeDetection: ChangeDetectionStrategy.OnPush,
})
export class ThreeDViewerComponent {

}

我想使用Is it my app.component,这是一个非独立组件.

import { Component } from '@angular/core';

@Component({
  selector: 'app-root',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.css'],
})
export class AppComponent {
}

我想在这里用它

<div class="layout">
  <app-three-d-viewer></app-three-d-viewer>
</div>

这是我的app.module

import { NgModule } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import { AppComponent } from './app.component';
import { HttpClientModule } from '@angular/common/http';

@NgModule({
  declarations: [
    AppComponent,
  ],
  imports: [
    BrowserModule,
    HttpClientModule,
  ],
  providers: [],
  bootstrap: [AppComponent]
})
export class AppModule { }

我得到这个错误


Error: src/app/app.component.html:2:3 - error NG8001: 'app-three-d-viewer' is not a known element:
1. If 'app-three-d-viewer' is an Angular component, then verify that it is part of this module.
2. If 'app-three-d-viewer' is a Web Component then add 'CUSTOM_ELEMENTS_SCHEMA' to the '@NgModule.schemas' of this component to suppress this message.

2   <app-three-d-viewer></app-three-d-viewer>
    ~~~~~~~~~~~~~~~~~~~~

  src/app/app.component.ts:6:16
    6   templateUrl: './app.component.html',
                     ~~~~~~~~~~~~~~~~~~~~~~
    Error occurs in the template of component AppComponent.

我try 的是:

  1. 导入ThreeDViewerComponent中的独立组件,非独立组件AppComponentimports: [ThreeDViewerComponent],但得到错误Error: src/app/app.component.ts:8:12 - error NG2010: 'imports' is only valid on a component that is standalone.

  2. 使用shared.module

import { NgModule } from '@angular/core';
import { CommonModule } from '@angular/common';
import { ThreeDViewerComponent } from './path/to/three-d-viewer.component';

@NgModule({
  declarations: [ThreeDViewerComponent],
  imports: [CommonModule],
  exports: [ThreeDViewerComponent]
})
export class SharedModule {}

and import it in AppModule but 我得到这个错误:

Error: src/app/app.module.ts:14:5 - error NG6002: This import contains errors, which may affect components that depend on this NgModule.

14     SharedModule
       ~~~~~~~~~~~~


Error: src/app/shared.module.ts:6:18 - error NG6008: Component ThreeDViewerComponent is standalone, and cannot be declared in an NgModule. Did you mean to import it instead?

6   declarations: [ThreeDViewerComponent],
                   ~~~~~~~~~~~~~~~~~~~~~


Error: src/app/shared.module.ts:8:13 - error NG6004: Can't be exported from this NgModule, as it must be imported first

8   exports: [ThreeDViewerComponent]
              ~~~~~~~~~~~~~~~~~~~~~




× Failed to compile.

我能做什么?

推荐答案

独立组件在独立时无法声明(Declarations Array),因此需要在模块上导入(Imports Array),无论是顶层模块,该模块都可以由声明数组上的所有组件使用!

import { NgModule } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import { AppComponent } from './app.component';
import { HttpClientModule } from '@angular/common/http';
import { ThreeDViewerComponent } from './path/to/three-d-viewer.component';

@NgModule({
  declarations: [
    AppComponent,
  ],
  imports: [
    BrowserModule,
    HttpClientModule,
    ThreeDViewerComponent, // <- changed here!
  ],
  providers: [],
  bootstrap: [AppComponent]
})
export class AppModule { }

或者您可以将其导入到共享模块,唯一的问题是,您还必须将其导入,以便它在所有导入SharedModule的组件中可见!

import { NgModule } from '@angular/core';
import { CommonModule } from '@angular/common';
import { ThreeDViewerComponent } from './path/to/three-d-viewer.component';

@NgModule({
  declarations: [],
  imports: [CommonModule, ThreeDViewerComponent], // <- changed here!
  exports: [ThreeDViewerComponent]
})
export class SharedModule {}

您收到imports数组的错误,因为在仅独立的组件上存在imports数组,在非独立组件中,组件decorator 对象中没有导入数组!

Javascript相关问答推荐

TestCafe预计文本等于以下内容之一

如何为GrapesJS模板编辑器创建自定义撤销/重复按钮?

使用JavaScript在ionic Web应用程序中更新.pane和.view的背景 colored颜色

有条件的悲剧

窗口.getComputedStyle()在MutationObserver中不起作用

react/redux中的formData在expressjs中返回未定义的req.params.id

Cookie中未保存会话数据

如何用显示网格平滑地将元素从一个地方移动到另一个地方?

如何在Connect 4游戏中 for each 玩家使用位板寻找7形状?

切换时排序对象数组,切换不起作用

我怎么才能得到Kotlin的密文?

如何在Angular17 APP中全局设置AXIOS

如何将innerHTML字符串修剪为其中的特定元素?

如何在Angular拖放组件中同步数组?

如何将zoom 变换应用到我的d3力有向图?

如何利用CSS中的隐藏元素实现平滑扩展和防止网格行间隙

Node.js错误: node 不可单击或不是元素

通过跳过某些元素的对象进行映射

使用createBrowserRoutVS BrowserRouter的Reaction路由

如何使用puppeteer操作所有选项