we use Angular 19. is it possible to use a component from a module in another component from the same module? if yes, how can i use it without the import in the first component?
partone.module.ts
import { NgModule } from '@angular/core';
import { CommonModule } from '@angular/common';
import { componentone } from './components/one/oneponent';
import { componenttwo } from './components/two/twoponent';
@NgModule({
declarations: [
],
imports: [
CommonModule, componentone, componenttwo
],
exports: [
componentone, componenttwo
]
})
export class partoneModule { }
oneponent
import { Component } from '@angular/core';
@Component({
selector: 'comp-one',
imports: [],
standalone: true,
templateUrl: './oneponent.html',
styleUrl: './oneponent.scss'
})
export class componentone {
}
twoponent
import { Component } from '@angular/core';
@Component({
selector: 'comp-two',
imports: [],
standalone: true,
templateUrl: './twoponent.html',
styleUrl: './twoponent.scss'
})
export class componenttwo {
}
oneponent.html
<comp-two></comp-two>
we use Angular 19. is it possible to use a component from a module in another component from the same module? if yes, how can i use it without the import in the first component?
partone.module.ts
import { NgModule } from '@angular/core';
import { CommonModule } from '@angular/common';
import { componentone } from './components/one/oneponent';
import { componenttwo } from './components/two/twoponent';
@NgModule({
declarations: [
],
imports: [
CommonModule, componentone, componenttwo
],
exports: [
componentone, componenttwo
]
})
export class partoneModule { }
oneponent
import { Component } from '@angular/core';
@Component({
selector: 'comp-one',
imports: [],
standalone: true,
templateUrl: './oneponent.html',
styleUrl: './oneponent.scss'
})
export class componentone {
}
twoponent
import { Component } from '@angular/core';
@Component({
selector: 'comp-two',
imports: [],
standalone: true,
templateUrl: './twoponent.html',
styleUrl: './twoponent.scss'
})
export class componenttwo {
}
oneponent.html
<comp-two></comp-two>
Share
Improve this question
asked Mar 10 at 10:46
sunicssunics
514 bronze badges
3
|
1 Answer
Reset to default 1In your question you're explicitly mentioning that your team is using Angular 19. In your example I can see that the components are defined as standalone. This means there is no need for a module in the first place. And the good news is, you definitely can use comp-one
within comp-two
without any issues.
Since Angular 19 standardised the standalone components, it is no longer needed to define a component as standalone: true
I would suggest the following refactor for your example:
import { Component } from '@angular/core';
@Component({
selector: 'comp-one',
imports: [CommonModule, ComponentTwo],
templateUrl: './oneponent.html',
styleUrl: './oneponent.scss'
})
export class ComponentOne {}
import { Component } from '@angular/core';
@Component({
selector: 'comp-two',
imports: [CommonModule],
templateUrl: './twoponent.html',
styleUrl: './twoponent.scss'
})
export class ComponentTwo {}
<comp-two></comp-two>
And there you have it! You're using component two, within component one.
Now, if you do need to have a module and use component two within component one, the following refactor is needed:
import { NgModule } from '@angular/core';
import { CommonModule } from '@angular/common';
import { componentone } from './components/one/oneponent';
import { componenttwo } from './components/two/twoponent';
@NgModule({
declarations: [
ComponentOne, ComponentTwo
],
imports: [
CommonModule,
],
exports: [
ComponentOne, ComponentTwo
]
})
export class PartOneModule { }
import { Component } from '@angular/core';
@Component({
selector: 'comp-one',
standalone: false,
templateUrl: './oneponent.html',
styleUrl: './oneponent.scss'
})
export class ComponentOne {}
import { Component } from '@angular/core';
@Component({
selector: 'comp-two',
standalone: false,
templateUrl: './twoponent.html',
styleUrl: './twoponent.scss'
})
export class ComponentTwo {}
<comp-two></comp-two>
When you're using a module, it isn't necessary to import the component within the component. That is because the components are both part of the same module.
The key difference here is that the components are now part of the declarations
array instead of the imports
array. That is because in Angular modules, components are declared within a module. Modules are imported within a module.
With that said, one could say that a standalone component is a combination of a module and a component and, therefore, standalone.
Hope this helps!
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744851294a4597142.html
standalone: false
and add the component to the declarations array and directly use the module, like how we did for modular angular components – Naren Murali Commented Mar 10 at 11:01