我有一个独立组件,我想在非独立组件中使用它,而不使该组件也独立.这可能吗?我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 的是:
-
导入
ThreeDViewerComponent
中的独立组件,非独立组件AppComponent
和imports: [ThreeDViewerComponent]
,但得到错误Error: src/app/app.component.ts:8:12 - error NG2010: 'imports' is only valid on a component that is standalone.
-
使用
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.
我能做什么?