์ ์ถ ์ค์ ๋๋ค ... ( "x"๋ก ํ๋ ์ ํ)
[ ] bug report => search github for a similar issue or PR before submitting
[X] feature request
[ ] support request => Please do not submit support request here, instead see https://github.com/angular/angular/blob/master/CONTRIBUTING.md#question
@DzmitryShylovich๊ฐ ์์ฒญํ # 12275 ์ ๋ํ ํ์ ๋ฌธ์
ํ์ฌ ํ๋
์ง์ฐ ๋ก๋๋ NgModule์ entryComponents
๋ ComponentFactoryResolver
๋ฅผ ์ฌ์ฉํ์ฌ ๋ ๋๋งํ ์ ์์ต๋๋ค. ์ค๋ฅ ๋ฉ์์ง: No component factory found for {{entryComponent}}
์์๋๋ ํ๋
entryComponents
๋ ๋ชจ๋์ ๊ฐ์ ธ์จ ๊ฒ์ฒ๋ผ ์ฌ์ฉํ ์ ์์ด์ผ ํฉ๋๋ค.
์ง์นจ์ผ๋ก ๋ฌธ์ ๋ฅผ ์ต์ํ์ผ๋ก ์ฌํ
http://plnkr.co/edit/9euisYeSNbEFrfzOhhzf?p=preview
๋ด๋ถ์์ ์ฌ์ฉํ๋ ๊ฒ๊ณผ ์ ์ฌํ ๊ฐ๋จํ ์ค์ ์ ๋ง๋ค์์ต๋๋ค. Main
๊ตฌ์ฑ ์์๋ Type
๋ฅผ ๋ ๋๋งํ๋ ๋ฐฉ๋ฒ์ ์ ๊ณตํฉ๋๋ค. EntryComponent
๋ Page1Module
entryComponent
๋ก ์ ์ธ๋ฉ๋๋ค. ๊ทธ๋ฌ๋ Page1Module
๋ฅผ ๋ก๋ํ ํ ComponentFactoryResolver
EntryComponent
๋ฅผ ๋ ๋๋งํ๋ ค๊ณ ํ๋ฉด No component factory found for EntryComponent
๊ฐ ๋ฐ์ํฉ๋๋ค.
ํ๋์ ๋ฐ๊พธ๋ ๋๊ธฐ/์ฌ์ฉ ์ฌ๋ก๋ ๋ฌด์์
๋๊น?
๋ฃจํธ ์์ค์์ ์์ ๋ชจ๋์ entryComponents
๋ฅผ ๋ ๋๋งํฉ๋๋ค. ์ด ์ ๊ทผ ๋ฐฉ์์ ์ฌ์ฉ ์ฌ๋ก๋ ๋ค์๊ณผ ๊ฐ์ต๋๋ค.
๊ทํ์ ํ๊ฒฝ์ ๋ํด ์๋ ค์ฃผ์ญ์์ค.
์ฐ๋ฆฌ๋ ํ์ฌ Angular 2.1.1์ ์ฌ์ฉํ๊ณ ์์ง๋ง ์ด๊ฒ์ ์ต์ ๋ฒ์ ์ Angular(2.4.6)์๋ ์ํฅ์ ๋ฏธ์นฉ๋๋ค(plnkr ์ฐธ์กฐ).
์ฝ๊ฐ์ ์กฐ์ฌ ํ์ ๋๋ ๋น์ ์ด ์ค๊ณํ ๋๋ก ์๋ํ๋ค๋ ๊ฒ์ ์์๋์ต๋๋ค.
ํญ๋ชฉ ๊ตฌ์ฑ ์์๋ ์ง์ฐ ๋ก๋๋ ๋ชจ๋์ ๊ณต๊ธ์์ ๋์ผํ ๊ท์น์ ๋ฐ๋ฆ
๋๋ค. https://angular.io/docs/ts/latest/cookbook/ngmodule-faq.html#! #q-lazy-loaded-module-provider-visibility
์ง์ฐ ๋ก๋๋ ๋ชจ๋ ๋ด์์๋ง ๋ณผ ์ ์์ต๋๋ค.
์ด๊ฒ์ด ์๋๋ ๋์์ด๋ผ๋ฉด ์์ ์ฌ์ฉ ์ฌ๋ก์ ๊ฐ์ ํน์ ์์ ์ ์ด๋ป๊ฒ ์ํํ ์ ์์ต๋๊น?
๋๋ ๋ฉ์ธ ๋ชจ๋์ ํญํ์ํค๋ ๊ฒ์ด ์ ์ ํ ํด๊ฒฐ์ฑ ์ด๋ผ๊ณ ์๊ฐํ์ง ์๋๋ค.
MainComponent
์์ ํญ๋ชฉ ๊ตฌ์ฑ ์์๋ฅผ ์ฌ์ฉํ๋ ค๋ฉด AppModule
์์ ์ ๊ณตํด์ผ ํฉ๋๋ค.
๊ทธ๋์ ๋น์ ์ ์๋ฃจ์ ์ ์ค์ ๋ก ๋ชจ๋ ๊ฒ์ ๋ฉ์ธ ๋ชจ๋๋ก ์ฎ๊ธฐ๋ ๊ฒ์ ๋๊น?
IMO๋ ๊ธฐ๋ฅ ๋ธ๋ก์ ํ๋์ ๋ชจ๋๋ก ๊ทธ๋ฃนํํ ์ ์์ด์ผ ํ๋ Angular์ ๋ชจ๋ ๊ฐ๋ ์ ์์ ํ ๊นจ๋จ๋ฆฝ๋๋ค.
์ด๊ฒ์ ๊ธฐ๋ณธ์ ์ผ๋ก ๋ชจ๋ฌ๊ณผ ๊ฐ์ ๊ฒ์ ์ง์ฐ ๋ก๋ ์ํคํ ์ฒ์ ํจ๊ป ์ฌ์ฉํ ์ ์๋ค๋ ๊ฒ์ ์๋ฏธํฉ๋๋ค.
์ฌ๋ฃ์ https://material.angular.io/components/component/dialog ์ ์ ์ฌํ API๋ฅผ ์ฌ์ฉํด์ผ ํฉ๋๋ค.
๊ทธ๋ฌ๋ฉด ์๋ํด์ผํฉ๋๋ค.
์์ฐ, ๊ทธ๋์ Angular Material์ด ํ๋ ๊ฒ์ฒ๋ผ DOM ์ฝํ ์ธ ๋ฅผ ์ด๋ํด์ผ ํ๋ค๊ณ ์๊ฐํฉ๋๋ค. ์ฌ๊ธฐ๋ฅผ ์ฐธ์กฐํ์ญ์์ค: https://github.com/angular/material2/blob/master/src/lib/core/portal/dom-portal-host.ts#L30 -L55
์ด๊ฒ์ด Angular์์ ์ด๊ฒ์ ์ฒ๋ฆฌํ๋ ๊ฐ์ฅ ์ข์ ๋ฐฉ๋ฒ์ด๋ผ๋ฉด ๊ธฐ๋ณธ์ ์ผ๋ก Angular๋ฅผ ๋ฒ๋ฆฌ๊ณ JQuery๋ฅผ ๋ค์ ์ฌ์ฉํ ์ ์์ต๋๋ค.
์ฃ์กํ์ง๋ง ์ ๋ ์ด ์ ๊ทผ ๋ฐฉ์์ ์ง์งํ๊ฒ ๋ฐ์๋ค์ผ ์ ์์ต๋๋ค.
๋ชจ๋ฌ ๋ฐ ์ง์ฐ ๋ก๋ฉ์ ๋ํ ์ต์ ์ ์ ๊ทผ ๋ฐฉ์์ด ๋ฌด์์ธ์ง ๋ชจ๋ฅด๊ฒ ์ง๋ง ์ด์จ๋ plunkr์์๋ ์๋ํ ๋๋ก ์๋ํ๋ฉฐ ์ฌ๊ธฐ์๋ ๊ฐ๋ ๋ฒ๊ทธ๊ฐ ์์ต๋๋ค.
๋ชจ๋ฌ์ ๋ํ ๋
ผ์๋ฅผ ์ํด์๋ gitter ๋ฐ stackoverflow์ ๊ฐ์ ์ง์ ์ฑ๋์ ์ฌ์ฉํ๋ ๊ฒ์ด ์ข์ต๋๋ค.
์๊ฒ ์ต๋๋ค. ์ด ๊ฒฝ์ฐ ์ด ๋ฌธ์ ๋ฅผ ๊ธฐ๋ฅ ์์ฒญ์ผ๋ก ๋ณ๊ฒฝํ๊ฒ ์ต๋๋ค.
IMO์์๋ ์ด๋ฌํ ์ฌ์ฉ ์ฌ๋ก๋ฅผ DOM์ ๋ฐ๋ผ ์๋์ผ๋ก ์ด๋ํ๋ ๋์ Angular ์์ฒด์์ ์ฒ๋ฆฌํด์ผ ํฉ๋๋ค.
์ค๋ ๊ฐ์ ๋ฌธ์ ์ ์ง๋ฉดํ์ต๋๋ค. ์ง์ฐ ๋ก๋ ๋ชจ๋์ ์ ์๋ ๋ชจ๋ฌ ๊ตฌ์ฑ ์์๋ ์ฑ ๋ชจ๋ฌ ์๋น์ค์์ ์ฒ๋ฆฌ๋์ง ์์ต๋๋ค( entryComponents
์ฌ์ฉ์๋ ๋ถ๊ตฌํ๊ณ ์ฐพ์ ์ ์์). ๊ฒ์ผ๋ฅธ ๋ชจ๋ ๊ตฌ์กฐ์ ์ฌ์ฉ ๋ก์ง์ ๊นจ๋จ๋ฆฌ๋ ๋ฉ์ธ ๋ฒ๋ค๋ก ๊ฐ์ ์ด๋:ํผ๋:
๊ฐ์ ๋ฌธ์ ๊ฐ ์์ต๋๋ค ... ๊ฒ์ผ๋ฅธ ๋ชจ๋ ๋ ผ๋ฆฌ๋ฅผ ๊นจ์ง ์๊ณ lazyLoaded ๋ชจ๋์์ entryComponents๋ฅผ ์ฌ์ฉํ ์ ์์ต๋๋ค. ๋ด LazyLoaded ๋ชจ๋์ ๋ด AppModule์ ์ ์ธ๋ entryComponent์ ์ข ์๋ฉ๋๋ค.
์ด ๋์ผํ ๋ฌธ์ ์ ๋ถ๋ช์ณค์ง๋ง ng-bootstrap ๋ชจ๋ฌ ์์ค ์ฝ๋๋ฅผ ์ดํด๋ณธ ํ ํด๊ฒฐ ๋ฐฉ๋ฒ์ ์ฐพ์์ต๋๋ค. ๋ณธ์ง์ ์ผ๋ก ๋ฌธ์ (๋ด ์ดํด๊ฐ ์๋ชป๋ ๊ฒฝ์ฐ ์์ ํด ์ฃผ์ธ์)๋ ์ฑ์ด ๋ถํธ์คํธ๋ฉ๋ ๋ ์ด๊ธฐ ์ธ์ ํฐ์ ๊ฒ์ผ๋ฅธ ๋ชจ๋์ด ํฌํจ๋์ง ์๊ณ ์ด๊ธฐ ์ธ์ ํฐ๊ฐ ์ค์ ๋๋ฉด ์์ ํ ์ ์๋ค๋ ๊ฒ์
๋๋ค. ์ด๊ฒ์ด OP์์ ์ค๋ฅ๊ฐ ๋ฐ์ํ๋ ์ด์ ์
๋๋ค. ์ง์ฐ ๋ชจ๋ ์ค ํ๋๊ฐ ๋ก๋๋๋ฉด ์ง์ฐ ๋ชจ๋์์ ์ ์ธํ๊ฑฐ๋ ์ ๊ณตํ๋ ๋ชจ๋ ํญ๋ชฉ์ ๋ํ ์ฐธ์กฐ๊ฐ ํฌํจ๋ ์์ฒด ์ธ์ ํฐ๋ฅผ ๊ฐ์ ธ์ต๋๋ค. ์ฆ, ๋์ ๊ตฌ์ฑ ์์๋ฅผ ์์ฑํ ๋๋ง๋ค ์ฌ๋ฐ๋ฅธ injector
๋ฐ componentFactoryResolver
์ ๋ํ ์ฐธ์กฐ๊ฐ ์๋ ํ ์ค์ ๋ก ์ง์ฐ ๋ก๋๋ ๋ชจ๋ ์ธ๋ถ์์ entryComponent
๋ฅผ ์ฐธ์กฐํ ์ ์์ต๋๋ค.
๊ทธ๋์ ๋ด๊ฐ ํ ๊ฒ์ ๋ด๊ฐ ๋ง๋ค๊ณ ์ถ์ ๋์ ์ปดํฌ๋ํธ์ injector
์ componentFactoryResolver
๋ฅผ ์ ์ฅํ๋ ์ฑ๊ธํค ์๋น์ค๋ฅผ ๋ง๋๋ ๊ฒ์ด์์ต๋๋ค. ์ด ์๋น์ค๋ ๊ฒ์ผ๋ฅธ ๋ชจ๋ ์ธ๋ถ์ ์์ด์ผ ํฉ๋๋ค. ๊ทธ๋ฐ ๋ค์ ๋์ ์ผ๋ก ๊ตฌ์ฑ ์์๋ฅผ ์์ฑํ ๋๋ง๋ค ์ด ์๋น์ค๋ฅผ ํธ์ถํ์ฌ ํ์ํ ๊ฒ์ ์ป์ ์ ์์ต๋๋ค.
์๋ ์ฝ๋๋ ๊ฒ์ผ๋ฅธ ๋ชจ๋ ์ธ๋ถ์์ ๋์ ๊ตฌ์ฑ ์์๋ฅผ ์์ฑํ๋ ๋ชจ๋ ์์น์ ์์ต๋๋ค.
@ViewChild('parent', {read: ViewContainerRef}) parent: ViewContainerRef;
private componentRef: ComponentRef<any>;
...
const childComponent = this.componentFactoryResolver.resolveComponentFactory(yourComponentRef);
this.refInjector = ReflectiveInjector.resolveAndCreate([{provide: yourComponentRef, useValue: yourComponentRef}], this.injector);
this.componentRef = this.parent.createComponent(childComponent, 0, this.refInjector);
```html
Then in your parent component for your entryComponent you'll inject `injector` and `componentFactoryResolver` and set their values in the shared service:
```js
constructor(private componentFactoryResolver: ComponentFactoryResolver, private injector: Injector) {}
์ด๊ฒ์ด ์๋ฏธ๊ฐ ์๋ ๊ฒฝ์ฐ ์๋ ค์ฃผ์๋ฉด ๋ ์์ธํ ์ค๋ช ํ๊ฒ ์ต๋๋ค. ํธ์ง ๋ด๊ฐ Angular 2.4.9๋ฅผ ์ฌ์ฉํ๊ณ ์๋ค๊ณ ์ธ๊ธํ๋ ๊ฒ์ ์์์ต๋๋ค.
@jmcclanahan ๊ฐ์ฌํฉ๋๋ค. ํด๋น ์๋น์ค์ ๋ชจ์๊ณผ ์ค์ ๋ฐฉ๋ฒ์ ๋ํด ์ข ๋ ๋ง์ํด ์ฃผ์๊ฒ ์ต๋๊น? ๋น์ทํ ๋ฌธ์ ๋ก ์ด๋ ค์์ ๊ฒช๊ณ ์์ต๋๋ค. ํ๋์ ์ง์ฐ ๋ก๋๋ ๋ชจ๋์์ ๋ค๋ฅธ ์ง์ฐ ๋ก๋๋ ๋ชจ๋์ ๊ตฌ์ฑ ์์๋ก ๊ตฌ์ฑ ์์๋ฅผ ๋์ ์ผ๋ก ๋ก๋ํ๋ ค๊ณ ์๋ํ๊ณ (์ ์๊ฐ์๋) ์ฌ๋ฐ๋ฅธ ์์น์ ํญ๋ชฉ ๊ตฌ์ฑ ์์๋ฅผ ์ถ๊ฐํ์ง๋ง ํ์ง ์์๋ค. ๊ฐ์ฌ ํด์
@gcorgnet - ๋ฌผ๋ก ์ ๋๋ค. ๊ทธ๋์ ์ ์ฌ์ฉ ์ฌ๋ก๋ ์ฑ์ ๋ชจ๋ ํ์ด์ง์ ๊ณตํต ๋๊ตฌ ๋ชจ์์ด ์์ง๋ง ๋๊ตฌ ๋ชจ์ ๋ด์์ ๋ด๊ฐ ์ด๋ค ํ์ด์ง์ ์์๋์ง์ ๋ฐ๋ผ ์ถ๊ฐ ๊ธฐ๋ฅ์ ์ํ ๋ฒํผ๊ณผ ๋๊ตฌ๋ฅผ ์ถ๊ฐํ๊ณ ์ถ์์ต๋๋ค. ์ด๋ฅผ ๋ฌ์ฑํ๊ธฐ ์ํด ์ด๋ฌํ ๋ฒํผ/๋๊ตฌ ๋ฐ ํด๋น ๋ ผ๋ฆฌ๋ฅผ ๋ณด์ ํ๋ ๊ตฌ์ฑ ์์๋ฅผ ๋ง๋ค์์ผ๋ฉฐ ์ด๋ฌํ ๊ตฌ์ฑ ์์๋ฅผ ์ฐ๊ฒฐ๋ ๋ชจ๋ ๋ด๋ถ์ ์ ์งํ๊ณ ์ถ์์ต๋๋ค. ๋ฐ๋ผ์ ๋ณธ์ง์ ์ผ๋ก ๋ด ๋ชจ๋ ์ธ๋ถ์๋ ์ด๋ฌํ ํน์ ๋๊ตฌ ๋ชจ์ ๊ธฐ๋ฅ์ ๋ํด ์๋ ๊ฒ์ด ์์ง๋ง ์ฌ์ ํ ๊ณตํต ๋๊ตฌ ๋ชจ์์ ํ์ํ ์ ์์ต๋๋ค. ๋ค์์ ๋์์ด ๋ ๋งํ ์์ ์๋ฃจ์ ์ ๋๋ค.
๋ด๊ฐ ๋๊ตฌ ๋ชจ์ ์๋น์ค๋ก ํ๋ ์ผ์ ๊ด์ฐฐ ๊ฐ๋ฅ ํญ๋ชฉ์ ๋ง๋๋ ๊ฒ๋ฟ์ด๋ฏ๋ก ๊ฒ์ผ๋ฅธ ๊ตฌ์ฑ ์์๋ receiveContext
๋ฅผ ์์ ํ๋ ๋๊ตฌ ๋ชจ์ ๊ณตํต ๊ตฌ์ฑ ์์์ ๋ํ componentFactoryResolver
๋ฐ injector
์ฐธ์กฐ๋ฅผ ์ ๋ฌํ ์ ์์ต๋๋ค. receiveContext
์ด๋ฒคํธ.
ํด๋ฐ.์๋น์ค.ts
@Injectable()
export class ToolbarService {
contextReceivedSource = new Subject<any>();
contextReceived$ = this.contextReceivedSource.asObservable();
receiveContext(componentFactoryResolver: ComponentFactoryResolver, injector: Injector) {
this.contextReceivedSource.next({ resolver: componentFactoryResolver, injector: injector });
}
}
์ง์ฐ ๋ก๋๋ ๊ตฌ์ฑ ์์์์ componentFactoryResolver
๋ฐ injector
๋ฅผ ์ฝ์
ํ๊ณ ๋๊ตฌ ๋ชจ์ ์๋น์ค์์ ์ด๋ฒคํธ๋ฅผ ์์ํ๋ ค๊ณ ํ ๊ฒ์
๋๋ค.
์ง์ฐ ๋ก๋๋ ๋๊ตฌ ๋ชจ์ ๊ตฌ์ฑ ์์
...
constructor(private componentFactoryResolver: ComponentFactoryResolver, private injector: Injector) {}
...
ngOnInit() {
this.toolbarService.receiveContext(this.componentFactoryResolver, this.injector);
}
...
๋ง์ง๋ง์ผ๋ก, ๋ด ํต์ฌ ๋๊ตฌ ๋ชจ์ ๊ตฌ์ฑ ์์์์ ๋๊ตฌ ๋ชจ์ ์๋น์ค์ ๊ด์ฐฐ ๊ฐ๋ฅ ํญ๋ชฉ์ ๊ตฌ๋
ํ๊ณ ์์ผ๋ฏ๋ก ์ด๋ฒคํธ๊ฐ ๋ฐ์ํ ๋๋ง๋ค ํ์ํ ๊ฒ์ผ๋ฅธ ๊ตฌ์ฑ ์์๋ฅผ ์ฝ์
ํ๊ณ ๋ง๋ค๋ ค๊ณ ํฉ๋๋ค. ์์ฑํ componentRef
๋ฅผ ํ๊ดดํ๋ ๊ฒ๋ ์ค์ํฉ๋๋ค. ๊ทธ๋ ์ง ์์ผ๋ฉด ๋ฉ๋ชจ๋ฆฌ ๋์๊ฐ ๋ฐ์ํ๊ฒ ๋ฉ๋๋ค.
...
@ViewChild('toolbarTarget', {read: ViewContainerRef}) toolbarTarget: ViewContainerRef;
component: Type<Component>;
refInjector: ReflectiveInjector;
resolverSub: Subscription;
refInjector: ReflectiveInjector;
componentFactoryResolver: ComponentFactoryResolver;
injector: Injector;
ngOnInit() {
this.resolverSub = this.toolbarService.contextReceived$.subscribe(resolver => {
this.componentFactoryResolver = resolver.resolver;
this.injector = resolver.injector;
});
}
updateToolbar(data: any) {
this.clearComponent();
this.component = data['toolbar'];
if (this.component) {
const childComponent = this.componentFactoryResolver.resolveComponentFactory(this.component);
this.refInjector = ReflectiveInjector
.resolveAndCreate([{provide: this.component, useValue: this.component}], this.injector);
this.componentRef = this.toolbarTarget.createComponent(childComponent, 0, this.refInjector);
}
}
clearComponent() {
this.toolbarTarget.clear();
if (this.componentRef) { this.componentRef.destroy(); };
}
ngOnDestroy() {
this.resolverSub.unsubscribe();
this.clearComponent();
}
๊ทธ๋ฌ๋ฉด ๋์ ์ผ๋ก ์์ฑ๋ ๊ตฌ์ฑ ์์๊ฐ ํด๋น #toolbarTarget
๋ฅผ ๋ฐฐ์นํ๋ ๊ณณ์ ๋ฐฐ์น๋ฉ๋๋ค.
<div #toolbarTarget></div>
์ ์ณ๋๊ณ , ๊ฒฝ๋ก์์ ์ง์ฐ ๋ก๋๋ ๊ตฌ์ฑ ์์ ์ฐธ์กฐ๋ฅผ ๊ฐ์ ธ์ค๊ธฐ ์ํด ๊ณตํต ๋๊ตฌ ๋ชจ์ ๊ตฌ์ฑ ์์์์ ์ด ์ค this.component = data['toolbar'];
์ ์ฌ์ฉํ๊ณ ์์ต๋๋ค. ์ด์ ๋ํ ์ ์ฒด ์ฝ๋๋ฅผ ๋ณด๊ณ ์ถ๋ค๋ฉด ๊ฒ์ํ ์ ์์ง๋ง ์ด ํ ๋ก ์ ๋ฒ์๋ฅผ ๋ฒ์ด๋ฉ๋๋ค.
{ path: '', component: yourComponent, data: { toolbar: yourToolbarComponentToInject } }
์ถ๊ฐ ์ง๋ฌธ์ด ์์ผ๋ฉด ์๋ ค์ฃผ์ธ์!
@jmcclanahan ๊ทํ์ ์ฝ๋์ ๋ํด ํ ๊ฐ์ง ์ง๋ฌธ(์๋ง๋ ์ด๋ฆฌ์์ ์ง๋ฌธ)์ด ์์ต๋๋ค. "๊ฒ์ผ๋ฅธ ๋ก๋๋ ๋๊ตฌ ๋ชจ์ ๊ตฌ์ฑ ์์"๋ฅผ ์ธ๊ธํ ๋ ngOnInit ์น์ ์ ์๋น์ค ํธ์ถ ์ฝ๋๋ฅผ ๋ฃ์ง๋ง ์ด๋ป๊ฒ ์ด๊ธฐํ(๊ตฌ์ฑ ์์)๋ฅผ ๊ด๋ฆฌํฉ๋๊น? HTML ์ฝ๋์ ์ถ๊ฐํ๊ณ ๋์ค์ DOM์์ ์จ๊ธธ ํ์ ์์ด? ํด๋น ๊ตฌ์ฑ ์์๋ ์๋จ ํ์์ค์์๋ง ์ฌ์ฉ๋๊ธฐ ๋๋ฌธ์ ๋ค๋ฅธ ๊ณณ์์๋ ๋ก๋ํ์ง ์์ต๋๋ค.
@Dunos - ์์ ์์์ ๊ทธ ๋ถ๋ถ์ ์์ ํ ์๋ตํ๋ค๋ ๊ฒ์ ๊นจ๋ฌ์์ต๋๋ค. ์ฃ์กํฉ๋๋ค! ์ฐ๊ฒฐ๋ ์ง์ฐ ๋ก๋ ๋ชจ๋์ ๋์ ๊ตฌ์ฑ ์์๋ฅผ entryComponent
๋ก ์ถ๊ฐํ๊ณ ์ถ์ ๊ฒ์
๋๋ค.
๊ทธ๋ฐ ๋ค์ ๋๊ตฌ ๋ชจ์ ๊ตฌ์ฑ ์์ ์์ฒด์์ ๋์ ๊ตฌ์ฑ ์์๋ฅผ ํ์ํ๋ ค๋ html ํ
ํ๋ฆฟ์ ์์น๋ฅผ โโ์ฐธ์กฐํ๋ ViewContainerRef
๋ฅผ ์ ์ํฉ๋๋ค. ๊ทธ๋ฐ ๋ค์ updateToolbar() ๋ฉ์๋๊ฐ ๊ตฌ์ฑ ์์ ์์ฑ์ ์ฒ๋ฆฌํ์ฌ ์ง์ ๋ ์์น์ ๋ฐฐ์นํ๊ณ ๋๊ตฌ ๋ชจ์์ ๊ตฌ์ฑ ์์๋ฅผ ํ์ํฉ๋๋ค.
@ViewChild('toolbarTarget', {read: ViewContainerRef}) toolbarTarget: ViewContainerRef;
...
updateToolbar(data: any) {
this.clearComponent();
this.component = data['toolbar'];
if (this.component) {
const childComponent = this.componentFactoryResolver.resolveComponentFactory(this.component);
this.refInjector = ReflectiveInjector
.resolveAndCreate([{provide: this.component, useValue: this.component}], this.injector);
this.componentRef = this.toolbarTarget.createComponent(childComponent, 0, this.refInjector);
}
}
๊ทธ๋ฌ๋ฉด ๋์ ์ผ๋ก ์์ฑ๋ ๊ตฌ์ฑ ์์๊ฐ ํด๋น #toolbarTarget์ ๋ฐฐ์นํ๋ ๊ณณ์ ๋ฐฐ์น๋ฉ๋๋ค.
<div #toolbarTarget></div>
clearComponent() ๋ฉ์๋๋ ngOnDestroy()๊ฐ ์คํ๋ ๋๋ง๋ค ํธ์ถ๋์ด ํ์ด์ง๋ฅผ ๋ ๋ ๋ ๊ตฌ์ฑ ์์๋ฅผ ์จ๊ธธ ์ ์์ต๋๋ค.
clearComponent() {
this.toolbarTarget.clear();
if (this.componentRef) { this.componentRef.destroy(); };
}
ngOnDestroy() {
this.resolverSub.unsubscribe();
this.clearComponent();
}
๋ํ ์์ ์๋ ๊ฒ์๋ฌผ์ ์ ๋ฐ์ดํธํ์ต๋๋ค. ์ด์ ๋ํด ๋ง์ ์ง๋ฌธ์ ๋ฐ๊ณ ์์ผ๋ฏ๋ก ๊ฐ๋ฅํ ํ ๋นจ๋ฆฌ ์ด ๋์ ๋๊ตฌ ๋ชจ์์ ์ฌ์ฉํ์ฌ ์์ ์์ ๋ฅผ ์ถ๊ฐํ๊ณ ๋งํฌ๋ก ์ ๋ฐ์ดํธํ๊ฒ ์ต๋๋ค.
@jmcclanahan ์ ์ป์์ง๋ง (๋ด ์๊ฐ์๋) ngOnInit๊ฐ ํธ์ถ๋์ง ์๊ธฐ ๋๋ฌธ์ ๋์ ๊ตฌ์ฑ ์์( this.toolbarService.receiveContext ํธ์ถ ๋ถ๋ถ)์์ ์๋น์ค์ ๋ํ ํธ์ถ์ด ์ด๋ป๊ฒ ์ด๋ฃจ์ด์ง๋์ง ์ฌ์ ํ ์ดํดํ์ง ๋ชปํฉ๋๋ค. t ๊ทธ๋ ๊ฒ ํ๋ ๋ฐฉ๋ฒ์ ์ฐธ์กฐํ์ญ์์ค). entryComponents์ ์ถ๊ฐํ์ง๋ง onInit๊ฐ ์๋ํ๋๋ก ํ๋ ค๋ฉด ์ด๊ธฐํํด์ผ ํฉ๋๋ค.
@Dunos- ์๋ง๋ ์๊ฐ ๊ฐ์ฅ ์ข์ ๊ฒ์
๋๋ค. ๋ด ์ฑ์ ์ธ๋ฑ์ค ํ์ด์ง์์ ํ์ด์ง A๋ก ์ด๋ํ๊ณ ์๋ค๊ณ ๊ฐ์ ํด ๋ณด๊ฒ ์ต๋๋ค. ํ์ด์ง A์ ๋ชจ๋/๊ตฌ์ฑ ์์๋ฅผ ์ ์ํ๋ ํ์ด์ง A์ ๋ํ ํด๋น ๊ฒฝ๋ก๊ฐ ์์ต๋๋ค. ํ์ด์ง A์๋ ์ ์ญ ๋๊ตฌ ๋ชจ์์ ํ์ํ ๋ณ๋์ ๊ตฌ์ฑ ์์๊ฐ ์ ์๋์ด ์์ต๋๋ค. ๋ฐ๋ผ์ Page A์ ์ฃผ์ ๊ตฌ์ฑ ์์ ๋ด๋ถ์ ComponentFactoryResolver
๋ฐ Injector
๋ฅผ ์ฝ์
ํ ๋ค์ receiveContext
์ด๋ฒคํธ๋ฅผ ๋ฐ์์ํต๋๋ค. ์ด ์ฝ๋ ๋นํธ:
constructor(private componentFactoryResolver: ComponentFactoryResolver, private injector: Injector) {}
...
ngOnInit() {
this.toolbarService.receiveContext(this.componentFactoryResolver, this.injector);
}
์ฃ์กํฉ๋๋ค. ํผ๋์ค๋ฌ์ธ ์ ์๋ค๋ ๊ฒ์ ์์์ต๋๋ค. ๋๊ตฌ ๋ชจ์์ ํ์ํ๋ ค๋ ๊ตฌ์ฑ ์์์ ํด๋น ์ฝ๋๋ฅผ ๋ฃ๋ ๊ฒ์ด ์๋๋ผ ํ์ฌ ์๋ ํ์ด์ง์ ์ฐ๊ฒฐ๋ ๊ตฌ์ฑ ์์์ ํด๋น ์ฝ๋๋ฅผ ๋ฃ์ต๋๋ค.
@jmcclanahan ์ ์๋ฒฝํฉ๋๋ค, ๊ฐ์ฌํฉ๋๋ค!!! ์ด์ ์๋ํฉ๋๋ค :)
@jmcclanahan ์ด ๊ฒ์๋ฌผ์ ์ฐพ๊ธฐ ์ ์ ๋์ผํ ๋ฌธ์ ๋ฅผ ํด๊ฒฐํ์ต๋๋ค. ๋ด๊ฐ ์์์ฐจ๋ฆฐ ํ ๊ฐ์ง๋ ํจ์ ์ ์ธ์ ๋ช ๊ฐ์ ๋ฌธ์๋ฅผ ์ ์ฅํ๋ ค๋ ๊ฒฝ์ฐ ์ ๋ฌํ ์ธ์ ํฐ๋ฅผ ์ฌ์ฉํ์ฌ ComponentFactoryResolver์ ๋ํ ์ฐธ์กฐ๋ฅผ ์ป์ ์ ์๋ค๋ ๊ฒ์
๋๋ค. ๋น์ ์ ๊ทธ๋ฅ ํ ์ ์์ต๋๋ค:
injector.get(ComponentFactoryResolver);
๊ทธ๋ฆฌ๊ณ ํ์ํ ์ฐธ์กฐ๋ฅผ ๋ฐํํด์ผ ํฉ๋๋ค. ๊ทธ๋ฌ๋ ๊ฒฐ๊ตญ ํจ์์ ๋งค๊ฐ๋ณ์๋ก ComponentFactoryResolver๋ฅผ ์ ๋ฌํ๋ ๊ฒ๊ณผ ๋์ผํ ๊ฒฐ๊ณผ๋ฅผ ์ ๊ณตํฉ๋๋ค.
์ด๊ฒ์ ๋ด ๋ฌธ์ ๋ ํด๊ฒฐํฉ๋๋ค.
์์ฝํ์๋ฉด, ๋น์ ์ด ์์์ผ ํ ๊ฒ์ ๋น์ ์ด lazy ๋ชจ๋์์ ์ ์ธ๋ ์ปดํฌ๋ํธ๋ฅผ ๋์ ์ผ๋ก ์์ฑํ๊ธฐ๋ฅผ ์ํ ๋ ๋น์ ์ ์ค์ ๋ก ๊ทธ lazy ๋ชจ๋์์ ์ ์ธ๋ ์ปดํฌ๋ํธ๋ก๋ถํฐ ComponentFactoryResolver๋ฅผ ์ฌ์ฉํด์ผ ํ๋ค๋ ๊ฒ์ ๋๋ค. ๋ฃจํธ ์ธ์ ํฐ(๋ฃจํธ ComponentFactoryResolver ํฌํจ)๋ ๋ก๋๋ lazyModule์์ ์ ์ธ๋ entryComponents์ ๋ํด ์์ง ๋ชปํฉ๋๋ค.
์ข ์ด์ํด ๋ณด์ด์ง๋ง ์ด๊ฒ์ด Angular๊ฐ ์ค๊ณ๋ ๋ฐฉ์(์ดํด๊ฐ ๋จ)์ด์ง๋ง ์ค์ ๋ก imho ํ๋ ์์ํฌ๋ก ์์ ํ ๋ ์ถ๊ฐ ๋ณต์ก์ฑ์ ์์ฑํฉ๋๋ค.
Angular ํ์ด ์ด๊ฒ์ ํ์ฉํ๋ ๋ฐฉ๋ฒ: ๊ฒฝ๋ก๊ฐ ํ์ฑํ๋ ๋๋ง๋ค ๋ฃจํธ ์ธ์ ํฐ๋ ํด๋น lazyModule์ ๋์ด๋ entryComponents๋ฅผ ์๋์ผ๋ก ์ถ๊ฐํด์ผ ํ๋ฉฐ ๊ฒฝ๋ก๊ฐ ๋ณ๊ฒฝ๋ ๋๋ง๋ค ์ด์ ์ ์ถ๊ฐ๋ entryComponents๊ฐ ์๋์ผ๋ก ์๋ฉธ๋์ด์ผ ํฉ๋๋ค. ์ด๋ ๊ฒ ํ๋ฉด ๋ณต์ก์ฑ์ด ์ค์ด๋ค๊ณ ์ค๋ฒํ๋ก๊ฐ ๋ฐฉ์ง๋ฉ๋๋ค. ์ถ๊ฐ ๊ตฌ์ฑ ์์ ํฉํ ๋ฆฌ๊ฐ ์๋ ๋ฃจํธ ์ธ์ ํฐ.
์ด์ ๋ํ ๊ธฐ๋ฅ ์์ฒญ์ ์ ์ถํ๊ฒ ์ต๋๋ค.
๊ฐ์ฌํฉ๋๋ค!
๊ฐ์ฌ ํฉ๋๋ค @jmcclanahan
@jmcclanahan ์ ๋ง ๊ฐ์ฌํฉ๋๋ค!! @renatoaraujoc ์ด ๊ธฐ๋ฅ์ Angular ํ์ ์ ์ถํ์ จ์ต๋๊น?
@rraya ์ด "๋ฌธ์ "์ ๋ํ ๊ธฐ๋ฅ ์์ฒญ์ ์ ์ถํ์ต๋๋ค(์๊ฐํด ๋ณด๋ฉด ๋ณ ๋ฌธ์ ๊ฐ ์๋๋๋ค). #17168์ ์ฐธ์กฐํ์ธ์.
์ฌ๊ธฐ์ ์ถ๊ฐํ๊ธฐ๋ง ํ๋ฉด ๋ฉ๋๋ค. ๋ด ์ฌ์ฉ ์ฌ๋ก๋ ์ฑ์ ๋ค๋ฅธ ๋ชจ๋ ๋ถ๋ถ์ด ๋ชจ๋ฌ์ ์ด๊ธฐ ์ํด ํธ์ถํ๋ open ๋ฉ์๋๊ฐ ์๋ AppModule์ ModalManagerService๋ฅผ ๋ง๋ ๊ฒ์ ๋๋ค. ๊ตฌ์ฑ ์์๋ฅผ ์ ๋ฌ๋ฐ์ ๋ค์ ๋ชจ๋ฌ๋ก ์ด๋ฆฝ๋๋ค. ๋ํ ์ธ์ ์๊ฐ ์ด๊ณผ๋ก ์ธํ ๊ฒฝ๋ก ๋ณ๊ฒฝ๊ณผ ๊ฐ์ด ์ฌ์ฉ์๊ฐ ๋ชจ๋ฌ์ ๋ซ๋ ๊ฒ ์ด์ธ์ ๋ค๋ฅธ ์์ ์ผ๋ก ๋ชจ๋ฌ์ ๋ซ์์ผ ํ ๋ ์ฌ์ฉ๋๋ closeAll ๋ฉ์๋๊ฐ ์์ต๋๋ค.
@jmcclanahan ํ์ง๋ง ์ฌ์ ํ ๋น์ ์ ์๊ฒฌ์ ์ดํดํ์ง ๋ชปํฉ๋๋ค. ๋ช ๊ฐ์ง ์ ์์ด ์์ผ๋ฉด ๊ฐ์ฌํฉ๋๋ค. 'this.componentFactoryResolver.resolveComponentFactory'์์ ์ค๋ฅ๊ฐ ๋ฐ์ํ ๊ฒ ๊ฐ์ต๋๋ค.
@Dunos ๋ ์ข ๋์์ค ์ ์๋?๋๋ ๋ช ์ฃผ ๋์ ๊ฐ์ ๋ฌธ์ ๋ฅผ ๊ฒช์๊ณ ์ ๋ง ๋จธ๋ฆฌ๊ฐ ์ํ๋ค.
@์์ธ ,
๋น์ ์ด ์ดํดํด์ผ ํ ๊ฒ์ ์ง์ฐ ๋ก๋๋ ๊ฒฝ๋ก๋ก ์ด๋ํ ๋๋ง๋ค ํ์ฑํ๋ ๊ฒฝ๋ก๊ฐ ํด๋น lazyModule์ ๋ํ entryComponents๋ฅผ ํฌํจํ๋ ์์ ์ธ์ ํฐ๋ฅผ ์์ฑํ๋ค๋ ๊ฒ์ ๋๋ค. AppModule(์์ฉ ํ๋ก๊ทธ๋จ์ ๋ฃจํธ ์ธ์ ํฐ)์ ์ ์๋ ์ ์ญ modalService๊ฐ ์๋ ๊ฒฝ์ฐ componentFactoryResolver๋ ๋ชจ๋ lazyModule์ ์ entryComponents๋ฅผ ์์ง ๋ชปํฉ๋๋ค. ๋ฐ๋ผ์ ์ด ๋ฌธ์ ๋ฅผ ํด๊ฒฐํ๋ ๋ฐฉ๋ฒ ์ค ํ๋๋ lazyModule์ ๊ตฌ์ฑ ์์์์ ๋ณด์ ํฉํ ๋ฆฌ ๋ฆฌ์กธ๋ฒ์ ์ธ์คํด์ค๋ฅผ ๊ฐ์ ธ์ค๊ณ ์ด๋ป๊ฒ๋ modalService์ "์ด ์ธ์ ํฐ/์ปดํฌ๋ํธ ํฉํ ๋ฆฌ ๋ฆฌ์กธ๋ฒ๋ฅผ ์ฌ์ฉํ์ฌ ์ํ๋ entryComponent๋ฅผ ๋ ๋๋งํ์ญ์์ค"๋ผ๊ณ ๋งํ๋ ๊ฒ์ ๋๋ค.
์กฐ๊ธ ์ดํดํ๊ธฐ ์ด๋ ค์ด ๊ฒ ๊ฐ์ง๋ง ๊ทธ๋ ์ง ์์ต๋๋ค.
@renatoaraujoc ์ค๋ช ํด์ฃผ์ ์ ๊ฐ์ฌํฉ๋๋ค. ์ ์ฌ์ฉ ์ฌ๋ก์์๋ ๋งํฌ์์ ๊ตฌ์ฑ ์์๋ฅผ ๋ก๋ํ๊ณ ์ถ์ต๋๋ค. ์ฆ, ์ผ์ชฝ ์ฌ์ด๋๋ฐ์์ ๋ฉ๋ด๋ฅผ ํด๋ฆญํ๊ณ ์ง์ฐ ๋ก๋๋ ๋ชจ๋์์ ๋ชจ๋์ ๊ฒ์ํ ๋ค์ ์์ฑํ๊ฒ ์ต๋๋ค. ๋์ ๊ตฌ์ฑ ์์๋ ํญ๊ณผ ๊ฐ์ ์ฑ์ ๋ง๋ค๊ณ ์ถ๊ธฐ ๋๋ฌธ์ ์ฌ๋ฐ๋ฅธ ๊ธฐ๋ณธ ์ฝํ ์ธ ์์ญ์ ์ผ๋ถ ๊ตฌ์ฑ ์์ ์ฝํ ์ธ ๊ฐ ์์ต๋๋ค. ํญ ํธ๋ฆฌ๊ฑฐ๋ฅผ ํด๋ฆญํ๋ฉด ์ฝํ ์ธ ๊ฐ ํ์๋ฉ๋๋ค. ๋๋ ์๋ฃจ์ ๊ตฌ์ถ ํญ์ด ์์ต๋๊น? angular2/4๋ฅผ ํตํ ์ฑ๊ณผ ๊ฐ์?
์ด์จ๋ , lazyload ๊ตฌ์ฑ ์์๋ ์ด๊ธฐํ๋ ์ ์์ต๋๋ค. ์ด๋ป๊ฒ ์๋น์ค์ componentFactoryResolver๋ฅผ ์๋ฆด ์ ์์ต๋๊น?
๋ด ์ฑ์๋ AppModule์ด ์๊ณ CoreModule์ด ํฌํจ๋์ด ์์ผ๋ฏ๋ก ๋ด ContentTabModule(ํญ์ ์ ๋ก๋ํ๋ ๋ฐ ์ฌ์ฉ)์ CoreModule์ ์ํ๋ฉฐ ๋ชจ๋ ์์ฑ ๊ตฌ์ฑ ์์๋ ContentTabModule์์ ์ฒ๋ฆฌํ๋ฉฐ ๋ฉ๋ด๋ฅผ ํด๋ฆญํ ๋ ์๋น์ค, ๊ตฌ์ฑ ์์, ์ง์๋ฌธ, ํ์ดํ๊ฐ ์์ต๋๋ค. ๋งํฌ, ContentTabService๋ ๋ฉ๋ด params์์ ๋์ ๊ตฌ์ฑ ์์๋ฅผ ์์ฑํฉ๋๋ค. ๋คํธ์ํฌ ํจ๋์ ๋ก๋๋ ์ผ๋ถ ํธ๋ ํฌ(์นํฉ ์ฌ์ฉ)๋ฅผ ๋ณด์์ต๋๋ค. ๊ทธ๋ฐ ๋ค์ ์ง์ฐ ๋ก๋ ๋ชจ๋์์ ์์ฐ๋ ๊ตฌ์ฑ ์์๋ฅผ ๋ง๋ค๊ณ ์ถ์ต๋๋ค.
@renatoaraujoc lazyModule์ ๊ตฌ์ฑ ์์์์ ComplementFactoryResolver์ ์ธ์คํด์ค๋ฅผ ์ด๋ป๊ฒ ์ป์ ์ ์์ต๋๊น? lazymodule์ ๊ตฌ์ฑ ์์๊ฐ ์ด๊ธฐํ๋์ง ์๊ธฐ ๋๋ฌธ์ ํจ์ฌ ๋ ์ด๋ ต๋ค๊ณ ์๊ฐํฉ๋๋ค(๋๋ ๋ด๊ฐ ํ๋ ธ์ต๋๋ค).
@werts ์์์ @jmcclanahan ๊ณผ ๋๋ ๋ํ๋ฅผ ํ์ธํ๊ณ ๋๊ตฌ ๋ชจ์๊ณผ ๋น์ทํ ๊ฒ์ ๋ํด ์ด์ผ๊ธฐํ์ต๋๋ค.
@Dunos @jmcclanahan ์ ์์ ๋ฅผ ์ดํดํ ์ ์์ต๋๋ค. ๊ฐ์ ๋ฌธ์ ๋ฅผ ๊ฒช๊ณ ์๋ค๊ณ ์๊ฐํฉ๋๋ค. lazyload ๊ตฌ์ฑ ์์๋ ์ด๊ธฐํ๋์ง ์์ต๋๋ค.
์ง์ฐ๋ก๋ ๋ชจ๋์์ this.toolbarService.receiveContext()๊ฐ ํธ์ถ๋์์ต๋๊น?
@werts ๊ฒฝ๋ก์ ํจ๊ป ์ง์ฐ ๋ชจ๋์ ์ฃผ์ ๊ตฌ์ฑ ์์๋ฅผ ๋ก๋ํด์ผ ํ๋ฉฐ ํด๋น ๊ตฌ์ฑ ์์๋ Injector ๋ฐ ComponentFactoryResolver๋ก aux ์๋น์ค๋ฅผ ํธ์ถํ๋ ๊ตฌ์ฑ ์์์ ๋๋ค.
@Dunos ๊ฐ์ฌํฉ๋๋ค. ํธ๋ฆฌํ ๊ฒฝ์ฐ ๋์์ ์ค ์ ์์ต๋๊น? ๋ค๋ฅธ ์ฌ์ฉ ์ฌ๋ก๊ฐ ์์ต๋๋ค. ๋งํฌ ํด๋ฆญ์์ ๋ชจ๋์ ์ง์ฐ ๋ก๋ํ ๋ค์ ๋์ ๊ตฌ์ฑ ์์๋ฅผ ๋ง๋ค๊ณ ์ถ์ต๋๋ค. ๊ฐ๋ฅํฉ๋๊น?
@werts ... ๋ค, ๊ฐ๋ฅํฉ๋๋ค. ์์์ ์ https://github.com/Toxicable/module-loading ์ ์ฐธ์กฐํ์ธ์.
์ข์, ์ง๊ธ ์๋ํ์ญ์์ค. @mlc-mlapis
@werts et al: ์ด ์ง์ ์ค๋ ๋๋ฅผ ๋ค๋ฅธ ์ฑ๋๋ก ๊ฐ์ ธ๊ฐ ๊ฒ์ ๊ถํ ์ ์์ต๋๊น? StackOverflow ๋๋ Gitter๊ฐ ๋ ์ ํฉํ ์ ์์ต๋๋ค. ์๋ ๊ทธ๋๋ก, ๋๋ ์ด ๋ฌธ์ ๋ฅผ ๊ตฌ๋ ํ๊ณ ์์ผ๋ฉฐ(๋ค๋ฅธ ๋ง์ ์ฌ๋๋ค๋ ๋ง์ฐฌ๊ฐ์ง๋ผ๊ณ ํ์ ํฉ๋๋ค) ํ์ฌ ๋ง์ ์์์ ๋ฐ์์ํค๊ณ ์์ด ์ฃผ์ ์์ ์กฐ๊ธ ๋ฒ์ด๋๊ณ ์์ต๋๋ค.
ํ๋ฌ์ค ์ธก๋ฉด: ๋์์ด ๋๋ ์ปค๋ฎค๋ํฐ๋ฅผ ๋ณด๋ ๋ฐ๊ฐ์ต๋๋ค!
์ด ๋ฌธ์ ๊ฐ ์ง์ฐ ๋ก๋๋ ๋ชจ๋๊ณผ ํจ๊ป ์ง๋ฉดํด์ผ ํ๋ ๋ ๋ค๋ฅธ ์ฅ์ ๋ฌผ์ด๋ผ๋ ๊ฒ์ด ๋ฏฟ๊ธฐ์ง ์์ต๋๋ค. ํ์ํ ๊ฒฝ์ฐ ๊ฒ์ผ๋ฅธ ๋ชจ๋์ ์ฝ๊ฒ ์ ๊ฑฐ/๋ด๋ณด๋ผ ์ ์๋๋ก ๊ฒ์ผ๋ฅธ ๋ชจ๋์ ์์์ ๋ถ๋ชจ(app.module)๋ก๋ถํฐ ์ต๋ช
์ผ๋ก ์ ์งํ๋ ์๋ฃจ์
์ ํ์ฌ์ ์ผ๋ก ์ฐพ๊ณ ์์์ต๋๋ค. app.component.html์ <router-outlet name="app-popup">
๊ฐ์ ๊ฐ๋จํ ์ฑ์ ๊ฒฝ์ฐ(ComponentFactory๊ฐ ์์
์ ๋์ฒดํ ์ ์๊ธฐ๋ฅผ ๋ฐ๋ผ๋ฉฐ ํฌ๊ธฐํ์ต๋๋ค) ๊ฒ์ผ๋ฅธ ์์ ๊ตฌ์ฑ ์์๋ฅผ ๋ก๋ํ๋ ๊ฒ์ ๋งค์ฐ ๋ฒ
์ฐผ์ต๋๋ค. ์ด์ ์ผ lazy-modules entry-children๊ณผ ๊ด๋ จ๋ entry-components ๋ฌธ์ ๊ฐ ๋ฐ์ํฉ๋๋ค. ๊ฒ์ผ๋ฅธ ์์ ๊ตฌ์ฑ ์์๋ฅผ ๋ฑ๋กํ๊ธฐ ์ํด ๋ฃจํธ ๋ชจ๋์ด ํ์ํ ๊ฒฝ์ฐ ๋ด ๋ชจ๋์ ์ ์ ํ๊ฒ ๊ฒฉ๋ฆฌ๋ ์ํ๋ก ์ ์งํ๋ ๊ฒ์ ๋ถ๊ฐ๋ฅํฉ๋๋ค. Angular ํ์ด ์ด๊ฒ์ ๊ณง ๋ฌธ์ ๋ก ์ธ์ํ๊ณ ํด๊ฒฐํ๊ธฐ๋ฅผ ๋ฐ๋๋๋ค. ๋๋ ์ฌ์ ํ ๊ทธ๋ค์ด ๊ฒ์ผ๋ฅธ ๋ชจ๋์ ๋ํ ์ฑ ์ ์ฒด ๋ผ์ฐํฐ ์ฝ์ผํธ์ ๋ํ ๋ด ๋ฌธ์ ์ ์๋ตํ๊ธฐ๋ฅผ ๊ธฐ๋ค๋ฆฌ๊ณ ์์ต๋๋ค. https://github.com/angular/angular/issues/19520
์ฒ์์๋ ์๋ฌด๊ฒ๋ ์์๊ณ ,
'๋ทฐ๋ฅผ ๋ง๋ค์ด๋ณด์! '๋ผ๊ณ jQuery ํ์ ๋งํฉ๋๋ค.
'๋ชฉ๋ก์ ๋ง๋ค์! '๋ผ๊ณ Angular ํ์ ๋งํฉ๋๋ค.
@jmcclanahan ์ด์ ngComponentOutlet์ด ์๊ฒผ์ต๋๋ค. ํจ์ฌ ๊ฐ๋จํด์ก์ต๋๋ค.
<ng-container *ngComponentOutlet="yourComponentClass;injector: yourInjector;"></ng-container>
@crysislinux ngComponentOutlet์ ํ๋ฅญํ์ง๋ง ์์ง ๊ตฌ์ฑ ์์์ ๋ฐ์ดํฐ๋ฅผ ์ ๋ฌํ ์ ์์ผ๋ฏ๋ก ์๋ฃจ์ ์ด ์๋๋๋ค.
์ด ๊ธฐ๋ฅ์ ๋ํ ์์์ด ์์ต๋๊น? =/
๋ฐฉ๊ธ์ด ๋ฌธ์ ๊ฐ ๋ฐ์ํ์ต๋๋ค. ๋ช ๊ฐ์ง ์ ์์ ์๋ํ์ง๋ง ์ฑ๊ณตํ์ง ๋ชปํ์ต๋๋ค. ๊ทธ๋๋ ์๋ํ์ต๋๋ค. ์ค์ ๋ก ๋๋ฆฌ๊ฒ ๋ก๋๋ ๋ชจ๋์ ๋ค๋ฅธ ์ข ์ ๋ชจ๋(์ด ๊ฒฝ์ฐ NGX ๋ชจ๋ฌ ๋ชจ๋)์ ๊ฐ์ ธ์ค๊ธฐ๊ฐ ๋๋ฝ๋์์ง๋ง ์ค๋ฅ๋ก ์ธํด ๋ด ๊ตฌ์ฑ ์์๋ฅผ ํด๊ฒฐํ ์ ์๋ค๊ณ ๋น๋ํ์ต๋๋ค.
๊ถ๊ธํฉ๋๋ค. ui-router๋ฅผ ์ฌ์ฉํ๋ฉด ์ด ๋ฌธ์ ๊ฐ ํด๊ฒฐ๋๋์? ์๋๋ฉด Angular์ ์์ฒด ๋ผ์ฐํฐ ๋ค์ค ์ฝ์ผํธ ๊ธฐ๋ฅ๋ ์์ต๋๊น? ๊ทธ๋ค์ ๋ฐฐํ์์ ์ด๋ฌํ ๋ ผ๋ฆฌ๋ฅผ ์ํํฉ๋๊น? ์ด๊ฒ์ Angular 1.x์์ ๋งค์ฐ ์ผ๋ฐ์ ์ธ ๊ฒฝ์ฐ์ด๋ฉฐ ์ด๋ฅผ Angular 2+๋ก ์ ํํ๋ ค๊ณ ํฉ๋๋ค.
๋๊ตฌ ๋ชจ์ ์์ ์์ ui-view(์๋ง๋ ๋ช ๋ช ๋ ๋ณด๊ธฐ ๋๋ ์ต๊ทค๋ฌ ๋ผ์ฐํฐ์ ๊ฒฝ์ฐ ๋ผ์ฐํฐ ์ฝ์ผํธ)๋ ๋ ๋์ ์์ค์ ๋ชจ๋์์ ์ค์ ๋ฉ๋๋ค. ์ง์ฐ ๋ก๋๋ ๋ชจ๋ ์ํ๊ฐ ๋๊ตฌ ๋ชจ์ ui-view๋ฅผ ์์ฒด ์ฝํ ์ธ ๋ก ๋์ฒดํ๋ ๊ฒฝ์ฐ "์๋"ํด์ผ ํ๊ฑฐ๋ ํ์ฌ ๋ชจ๋ ์ธ๋ถ์ ์์ ์์ค ๋ณด๊ธฐ๋ฅผ ๊ต์ฒดํด์ผ ํ๋ ์ง์ฐ ๋ก๋๋ ํ๊ฒฝ์์ ui-router๊ฐ ์๋ํ์ง ์์ต๋๋ค. . ์ด๋ Angular 1.x์ ๋นํด ๋งค์ฐ ์ ํ์ ์ ๋๋ค.
์์ ์๋ ui-router/angular ๋ผ์ฐํฐ๊ฐ ํด๊ฒฐํ๋๋ก ์ค๊ณ๋ ๊ฒ๊ณผ ๋์ผํ ๋ฌธ์ ๋ฅผ ํด๊ฒฐํ๋ ๊ฒ์ฒ๋ผ ๋ณด์ ๋๋ค. ๋ด๊ฐ ๋ญ๊ฐ๋ฅผ ๋์น๊ณ ์์ต๋๊น?
... ์ฐ๋ฆฌ๋ ์๋ง๋ ๋ฌธ์ ๊ฐ ๋ฌด์์ธ์ง ์ ํ ์์ด ๋ฒ๋ ธ์ ๊ฒ์ ๋๋ค. ์ฐ๋ฆฌ๊ฐ ์๊ณ ๋งค์ผ ์ฌ์ฉํ๋ ๊ฒ์ฒ๋ผ ์ง์ฐ ๋ก๋๋ ๋ชจ๋์ ๊ตฌ์ฑ ์์๋ฅผ ์ฌ์ฉํ๋ ๋ฐ ๋ฌธ์ ๊ฐ ์์ต๋๋ค. ๋ผ์ฐํฐ๋ฅผ ํตํ๊ฑฐ๋ ํ๋ก๊ทธ๋๋ฐ ๋ฐฉ์์ผ๋ก ์์ฒด ์ฝ๋๋ฅผ ์ฌ์ฉํ๋ ๊ฒฝ์ฐ์๋ ๋ง์ฐฌ๊ฐ์ง์ ๋๋ค.
์์ @jmcclanahan ์์ ๋ฅผ ์ฌ์ฉํ ๋ ๋ด ์ฌ์ฉ ์ฌ๋ก์ ์ ์๋ํ๋ ๊ฒ์ผ๋ก ๋ณด์ ๋๋ค.
this.componentRef = this.toolbarTarget.createComponent(childComponent, 0, this.refInjector);.
$ ๋ค์์ $ this.componentRef.changeDetectorRef.markForCheck();
๋ฅผ ์ถ๊ฐํด์ผ ํ์ต๋๋ค.
์ด๋ ๊ฒ ํ๋ฉด HostBinding ์์ฑ์ด ํธ๋ฆฌ๊ฑฐ๋์ง ์๋ ๋ฌธ์ ๊ฐ ์์ ๋์์ต๋๋ค.
๋๋ ๋์๊ฒ ํจ๊ณผ๊ฐ ์์๋ ์ ์ฌ์ ์ธ ์๋ฃจ์
์ผ๋ก ์ค์์ ํ ๊ฒ์ด๋ผ๊ณ ์๊ฐํ์ต๋๋ค. ์ด๊ฒ์ ์๋ plunkr์ ํฌํฌ์
๋๋ค.
http://plnkr.co/edit/x3fdwqrFDQr2og0p6gwr?p=preview
์ด ๋ฌธ์ ๋ฅผ ํด๊ฒฐํ๋ ค๋ฉด entryComponent(Page1Module)๊ฐ ์๋ ๋ชจ๋์ ์ ๊ณต๋ ์๋น์ค(ResolverService)์์ ๊ตฌ์ฑ ์์ ํฉํ ๋ฆฌ๋ฅผ ํด๊ฒฐํด์ผ ํฉ๋๋ค. ๊ทธ๋ฐ ๋ค์ ํด๊ฒฐ๋ ํฉํ ๋ฆฌ๋ฅผ ์ฌ์ฉํ๊ณ MainComponent์ ๋ณด๊ธฐ์ ๊ตฌ์ฑ ์์๋ฅผ ์ถ๊ฐํ๋ ๋ ๋ฒ์งธ ์๋น์ค(ChangerService)๋ก ์ ๋ฌ๋ฉ๋๋ค.
๋ด๊ฐ ์ฌ๊ฐํ๊ฒ ์ดํดํ์ง ๋ชปํ๋ ๊ฒ์ ์ด ๋น์ด๋จน์ ๋ผ์ฐํฐ์
๋๋ค. ์ด plunker๋ฅผ ํ์ธํ์ญ์์ค( @patkoeller ์ ์์์ ๋ถ๊ธฐ๋จ)
http://plnkr.co/edit/keMuCdU9BwBDNcaMUAEU?p=๋ฏธ๋ฆฌ๋ณด๊ธฐ @ src/page1.ts
๋ด๊ฐ ๊ธฐ๋ณธ์ ์ผ๋ก ํ๊ณ ์ถ์ ๊ฒ์ ์ผ๋ถ ๊ตฌ์ฑ ์์๋ฅผ ์ต์์ ์ฑ์ ์ฝ์
ํ๊ณ /page-1/sub/component
[routerLink]="['./', 'sub', 'component']
์ ๊ฐ์ ์ง์ฐ ๋ก๋ ๋ชจ๋์ ์ ์ํ๋ ๊ฒ๊ณผ ๊ด๋ จ๋ ๋ช
๋ น๊ณผ ํจ๊ป [routerLink]
์ง์๋ฌธ์ ์ฌ์ฉํ๋ ๊ฒ์
๋๋ค. ActivatedRoute
$๋ฅผ ์ฌ์ฉํ์ฌ ์ธ์ ํฐ๋ฅผ createComponent
์ ์ ๋ฌํ๊ณ ์ ๊ฒฝ์ฐ์ ์๋ํ๋ฉด ๋ค๋ฅธ ๋ชจ๋ routerLink๋ฅผ ๋ง์น ์ ์์ง๋ง router.routerState.snapshot
๋ฅผ state
์ ๋ค์ ํ ๋นํ๋ฉด
๋ฒ๊ทธ๋ฅผ ์ ๊ณ ํด์ผ ํ๋์?
๋ด ๋ํ ์ฑ๊ธํค ์๋น์ค์ ํญ๋ชฉ ๊ตฌ์ฑ ์์์ ๋์ผํ ๋ฌธ์ ์ ์ง๋ฉดํ๊ณ ์์ต๋๋ค. ๊ธฐ๋ฅ ๊ด๋ จ ๋ชจ๋ฌ ๊ตฌ์ฑ ์์๋ @jmcclanahan ์ ํด๊ฒฐ ๋ฐฉ๋ฒ ์์ด๋ ํด๋น ๋ชจ๋์ ๋จ์ ์์ด์ผ ํฉ๋๋ค.
์ด์ ๋ํ ์๋ก์ด ETA๊ฐ ์์ต๋๊น? ์๋๋ฉด ์ ์ด๋ Angular ํ์ ์๋ต์ ๋๊น? ๋ชจ๋ ์บก์ํ๊ฐ ์ฐฝ ๋ฐ์ผ๋ก ๋์ ธ์ก๊ธฐ ๋๋ฌธ์ ์ด ๊ธฐ๋ฅ์ด ์๋ ๊ฒ์ ์ ๋ง ๋์ ์ผ์ ๋๋ค. @angular/material์ ๋ํ ๋ชจ๋ ๋ํ ์์๋ ๋ฃจํธ ๋ชจ๋์์ ๊ฐ์ ธ์์ผ ํฉ๋๋ค.
@xban1x ๋๋ ์ฐ๋ฆฌ๊ฐ ๊ทธ๊ฒ์ ๋ํด ์ง์ ์ด ์๋ค๊ณ ์๊ฐํฉ๋๋ค. ์ฐ๋ฆฌ๋ ๊ทธ๊ฒ์ ๋๋ฌ๋ณด๊ธฐ๋ฅผ ์ํํด์ผํฉ๋๋ค :(
@jmcclanahan ๊ทํ์ ์ ๊ทผ ๋ฐฉ์์ ์ฌ์ฉํ์ฌ ์ฃผ์ ๋ ๊ตฌ์ฑ ์์์ ์ฐธ์กฐ๋ฅผ ์ป์ ์ ์์ต๋๊น? ๊ตฌ์ฑ ์์๋ฅผ ์ฑ๊ณต์ ์ผ๋ก ์ฃผ์ ํ์ต๋๋ค. ๊ทธ๊ฒ์ ํธ๋ฆฌ btw์ด๋ฏ๋ก ์ด์ ํธ๋ฆฌ๋ฅผ ์ฃผ์ ํ ๊ตฌ์ฑ ์์์ ํธ๋ฆฌ ํด๋ฆญ ์ด๋ฒคํธ๋ฅผ ๊ตฌ๋ ํด์ผ ํฉ๋๋ค. ์ด ์์ ์ ์ํํ ์ ์๋ ๋ฐฉ๋ฒ์ด ์์ต๋๊น?
๋ฌธ์ #23819์์ ํญ๋ชฉ ๊ตฌ์ฑ ์์๊ฐ providesIn: 'root'
์๋น์ค์ ์ ์ฌํ ๋ฐฉ์์ผ๋ก ๋์ํ๋๋ก ํ๋ ๋ฐฉ๋ฒ์ ๋ํด ์ง๋ฌธํ์ต๋๋ค. ๋ ๋ฌธ์ ๊ฐ ๊ฐ์ ๋ฌธ์ ๋ฅผ ํด๊ฒฐํ๊ธฐ ์ํ ๊ฒ ๊ฐ๊ธฐ ๋๋ฌธ์ ํด๋น ๋ฌธ์ ๋ฅผ ๋ซ์์ต๋๋ค. ์ค์ํ ๊ธฐ๋ฅ์ด๊ธฐ ๋๋ฌธ์ ์ด ๋ฌธ์ ๊ฐ ๋นจ๋ฆฌ ํด๊ฒฐ๋์์ผ๋ฉด ํฉ๋๋ค. :)
์ ๋ฐ์ดํธ
์ถ์๋์ง ์์ ์ฝ๋์์ ๋ณผ ์ ์๋ฏ์ด ์๋น์ค์ providesIn
๊ฐ ์ ์๋์ด ์์ ๋ ์ด๋ฏธ ์ฌ์ฉํ ์ ์๋๋ก ํฉํ ๋ฆฌ๋ฅผ ๋ฑ๋กํฉ๋๋ค(๋ชจ๋๋ก ๊ฐ์ ธ์ฌ ํ์ ์์ด), ์ดํดํ๋ฉด ๋ฐ๋ฅด๊ฒ:
var Injectable = makeDecorator('Injectable', undefined, undefined, undefined, function (injectableType, options) {
if (options && options.providedIn !== undefined &&
injectableType.ngInjectableDef === undefined) {
injectableType.ngInjectableDef = defineInjectable({
providedIn: options.providedIn,
factory: convertInjectableProviderToFactory(injectableType, options)
});
}
});
@Component({ ... entryComponent: true })
์ ๊ฐ์ ์ต์
์ ์ฌ์ฉํ์ฌ ๊ตฌ์ฑ ์์์ ์ ์ฌํ ๋
ผ๋ฆฌ๋ฅผ ์ ์ฉํ๋ ๋ฐฉ๋ฒ์ด ์๋ค๊ณ ๊ฐ์ ํฉ๋๋ค. ๊ทธ๋ฌ๋ฉด ๊ตฌ์ฑ ์์๋ฅผ ํฌํจํ๋ ์ฒญํฌ๊ฐ ๋ก๋๋ ๋ ํฉํ ๋ฆฌ๊ฐ ๋ฆฌ์กธ๋ฒ ์ ์ ์๋ฉ๋๋ค(๋ค์์ ํฌํจํ๋ ๋ชจ๋์ด ์์ด์ผ ํฉ๋๋ค). ๊ตฌ์ฑ ์์ ์ข
์์ฑ์ด ์๋ entryComponents
์ ๊ตฌ์ฑ ์์, ๊ทธ๋ ์ง ์์ผ๋ฉด ์ปดํ์ผ ์๊ฐ ์ค๋ฅ๊ฐ ๋ฐ์ํจ).
@lucasbasquerotto ... ๋ค์ ๋ ผ๋ฆฌ๋ฅผ ์ฌ์ฉํ์ฌ ๋ชจ๋์ ๋ก๋ํ๊ณ ํด๋น ๊ตฌ์ฑ ์์ ์ ํ์ ์ฌ์ฉํฉ๋๋ค(ํด๋น ๋ชจ๋์ ์ง์ ์ ์ฅ๋ ๋ฌธ์์ด ํค <-> ๊ตฌ์ฑ ์์ ์ ํ ๊ฐ์ ์์ฒด ๋งคํ ์ฌ์ฉ).
this._loader.load(url)
.then((_ngModuleFactory: NgModuleFactory<any>) => {
const _ngModuleRef: NgModuleRef<any> = _ngModuleFactory.create(this._injector);
const _cmpType: Type<any> = _ngModuleRef.instance.cmps.get('dynamic');
const _cmpFactory: ComponentFactory<any> = _ngModuleRef
.componentFactoryResolver
.resolveComponentFactory(_cmpType);
this._cmpRef = this.vc.createComponent(_cmpFactory, 0, this._injector, []);
});
์๋
ํ์ธ์ ์ฌ๋ฌ๋ถ, ์ง๊ธ์ ์ฌ๋ฌ ์ง์ฐ ๋ก๋ ๋ชจ๋ ํด์๊ธฐ์ ๋ํ ์ฐธ์กฐ๋ฅผ ์ ์ง ๊ด๋ฆฌํ๋ ์ฑ๊ธํค ํด๋์ค๋ฅผ ๊ฐ๋ ํด๊ฒฐ ๋ฐฉ๋ฒ์ ์ฌ์ฉํ๊ณ ์์ต๋๋ค.
Angular์ ์ ๋ฒ์ ์ผ๋ก ์ด๊ฒ์ ์ฒ๋ฆฌํ๋ ๋ ์ข์ ๋ฐฉ๋ฒ์ด ์์ต๋๊น?
๊ฐ์ฌ ํด์
์๋ ๋ชจ๋๋ค,
๋์ผํ ๋ฌธ์ ์ ์ง๋ฉดํ๊ณ ํด๊ฒฐ ๋ฐฉ๋ฒ์ ์๋ฃจ์ ์ ๋ถํธ์คํธ๋ฉ์ผ๋ก ์ฌ์ฉํฉ๋๋ค.
์ด์ ๋ํด ๋ ๋์๊ฐ ๊ธ๋ก๋ฒ ์๋น์ค์ ํ์์ฑ์ ์ค์ด๋ ๋ฐฉ๋ฒ์ RouteConfigLoadEnd ์ด๋ฒคํธ์ loadedConfig ๊ฐ์ธ ๋ณ์๋ฅผ ๋ ธ์ถํ๋ ๊ฒ์ ๋๋ค. LoadedRouterConfig ์ ํ์ ์ด ๊ฐ์ธ ๋ณ์์๋ ๋ก๋๋ ๋ชจ๋, ํด๋น ์ธ์ ํฐ ๋ฐ componentFactoryResolver๊ฐ ํฌํจ๋ฉ๋๋ค.
์ด๊ฒ์ ๊ธฐ๋ฅ ์์ฒญ์ผ๋ก ์ ์ํ๋ ค๋ฉด ์ ๋ฌธ์ ๋ฅผ ์ด์ด์ผ ํฉ๋๊น?
---------- ์์ ํ
๋ด ์ง๋ฌธ์ ์ ๊ฒฝ์ฐ์ง ๋ง์ธ์. ์ด ๊ธฐ๋ฅ ์์ฒญ์ ์ฐพ์์ต๋๋ค. https://github.com/angular/angular/issues/24069
๋๋ ๊ฐ์ ๋ฌธ์ ๊ฐ ์๋ค๊ณ ์๊ฐํ์ง๋ง ๋ด ์ถ๊ฐ ๋ชจ๋์ lazy loaded
๊ฐ ์๋๋๋ค. entryModules
๋ฅผ SharedFeatureModule
(ํ์ํ ๊ฒฝ์ฐ์๋ง ๋ก๋๋จ)์์ CoreModule
๋ก ์ด๋ํด์ผ ํฉ๋๋ค.
์ด์ ์๋ํฉ๋๋ค. ๊ทธ๋ฌ๋ ๋ด ์๊ฒฌ์ผ๋ก๋ ์ข์ง ์์ ๊ธฐ๋ฅ ๊ด๋ จ ๊ตฌ์ฑ ์์๋ฅผ ์ฝ์ด๋ก ์ฎ๊ฒผ์ต๋๋ค.
์ด ๋ฌธ์ ๊ฐ ์์ง ํด๊ฒฐ๋์ง ์์์ต๋๊น?
@jmcclanahan ์ ์๋ฃจ์
{ path: '', component: yourComponent, data: { toolbar: yourToolbarComponentToInject } }
๋์ ๋น ๊ฒฝ๋ก๋ฅผ ์ถ๊ฐํฉ๋๋ค. ๋์ ์ด๋ ๊ฒฝ๋ก๋ฅผ ๋ง๋ค๋ ค๊ณ ํ๋ฉด ์ด๊ฒ์ด ๋ฌธ์ ๊ฐ ๋ฉ๋๋ค.
์ด ๋น ๊ฒฝ๋ก๋ฅผ ๊ตฌ์ฑ ์์์ ๊ธฐ๋ณธ์ ์ผ๋ก ์ถ๊ฐํ์ง ์๊ณ ์ง์ฐ ์์ ๋ชจ๋์ ๋ถํธ์คํธ๋ฉ๊ณผ ๋น์ทํ ๊ฒ์ ์ ํธํฉ๋๋ค.
์ด์ ๋ํ ์ ๋ฐ์ดํธ๊ฐ ์์ต๋๊น?
์ฐ๋ฆฌ์ ์ฌ์ฉ์ ์ ์ ์๋ฃจ์
์๋ ๊ธ๋ก๋ฒ ์ฌ์ด๋๋ฐ๋ฅผ ์ด ์ ์๋ ๊ฐ ๊ตฌ์ฑ ์์์ ComponentFactoryResolver
๋ฐ Injector
๋ฅผ ์์ฑ์ ์ฃผ์
ํ๋ ์์
์ด ํฌํจ๋ฉ๋๋ค. ๊ทธ๋ฐ ๋ค์ ํธ์ถ ๊ตฌ์ฑ ์์๋ ํด๋น ์ฐธ์กฐ(์์ฒด)๋ฅผ ์ํ๋ Component
์ ํจ๊ป ์๋น์ค์ ์ ๋ฌํฉ๋๋ค. ์๋น์ค๋ ์ด๋ฌํ ์ธ์คํด์ค๋ฅผ ์ฌ์ฉํ์ฌ ๊ตฌ์ฑ ์์๋ฅผ ๋ง๋ญ๋๋ค. ์ง๋์น๊ฒ ๋ณต์กํ๊ณ ์ฅํฉํฉ๋๋ค.
ํญ๋ชฉ ๊ตฌ์ฑ ์์๋ฅผ ์์ฒด ํ์ ๋ชจ๋์ ๋ฃ๊ณ ์ฑ์ผ๋ก ๊ฐ์ ธ์ค๊ธฐ๋ง ํ๋ฉด ๋ฉ๋๋ค.
@broweratcognitecdotcom ์ด ๋ฌธ์ ์ ์์ ์ ์ง์ฐ ๋ก๋๋ ๋ชจ๋ ์ entryComponents
์ ์๋ค๊ณ ์๊ฐํฉ๋๋ค. AppModule
ํญ๋ชฉ ๊ตฌ์ฑ ์์๋ก ์ฌ์ฉํ๋ ค๋ ๋ชจ๋ ๊ตฌ์ฑ ์์์ ๋ชจ๋ ํ์ ๋ชจ๋์ ํฌํจํ๋ฉด ์ด๋ฌํ ๋ชจ๋ ๊ตฌ์ฑ ์์๊ฐ ๊ฐ์ ๋ก ๋ก๋ ๋์ด ์์ ์ ์ฑ๋ฅ ๋ฌธ์ ๊ฐ ๋ฐ์ํ ์ ์์ต๋๋ค.
์ด๋ฌํ ๊ตฌ์ฑ ์์๋ ํด๋น ๊ตฌ์ฑ ์์๋ฅผ ์ฌ์ฉํ๋ ๊ตฌ์ฑ ์์(์๋น์)์ ๋ํด ๋ถ๊ฐ์ง๋ก ์ ์ด์ด์ผ ํ๋ค๋ ๊ฒ์ ๋งํ ๊ฒ๋ ์๊ณ , ํด๋น ๋ชจ๋์ด ์ง์ ๊ตฌ์ฑ ์์ ๋๋ ์ผ๋ฐ ๊ตฌ์ฑ ์์(html์ ์ ์ธ๋จ)๋ก ์ฌ์ฉ๋๊ณ ์๋์ง ์ ์ ์๋๋ก ํด์ผ ํฉ๋๋ค. ์๋น์๋ ์์์ผ ํฉ๋๋ค(๋ฐ๋ผ์ AppModule์ ๋ฃ์ผ๋ฉด ์ ๊ฑฐ๊ธฐ์ ์๋์ง ์๊ธฐ๊ฐ ์ด๋ ค์ ๋ค๋ฅธ ๊ณณ์์ ๊ฒ์ํด์ผ ์ ์ ์์ต๋๋ค).
@lucasbasquerotto ... ๊ทธ๋ฐ ๊ฒ์ผ๋ฅธ ๋ชจ๋์ ํ๋ก๊ทธ๋๋ฐ ๋ฐฉ์์ผ๋ก ๋ก๋ํฉ๋๋ค... ๊ทธ๋ฌ๋ฉด ๋ชจ๋ ๊ถํ์ด ์๊ณ ์ ๋๋ก ์๋ํฉ๋๋ค.
๋ฌธ์์ด ํค ๋ฐ ํด๋น ๊ตฌ์ฑ ์์์ ๋งต์ ์ฌ์ฉํ์ฌ ๋ฃจํธ ํ์ธ์์์ ๊ตฌ์ฑ ์์๋ฅผ ํด๊ฒฐํ ์ ์๋ ์ด์ํ ๋ฌธ์ ๊ฐ ๋ฐ์ํ ํ ์ด ์ค๋ ๋๋ฅผ ์ฐ์ฐํ ๋ฐ๊ฒฌํ์ต๋๋ค. ๋ง ๊ทธ๋๋ก _factories ๋งต์์ ๊ตฌ์ฑ ์์๋ฅผ ๋ณผ ์ ์์ง๋ง ์ด๋ป๊ฒ ๋ ์ฌ๋ฐ๋ฅด๊ฒ ๋น๊ต๋์ง ์๊ณ ์ ์๋์ง ์์ ๊ฐ์ ๋ฐํํฉ๋๋ค.
@jmcclanahan @Dunos ๊ธฐ๋ฅ ๋ชจ๋์ด ์ธ์ ํฐ ์ฐธ์กฐ๋ฅผ ๋๊ฒจ์ฃผ๋ ๋์ (๋ฉ์ธ ์ฑ ๋ชจ๋์ ์ปจํ ์คํธ ๋ฑ๋ก) ์ค์ ๋ก ์ฌ๋ฐ๋ฅด๊ฒ ํด๊ฒฐ๋ ํฉํ ๋ฆฌ๋ฅผ ๋ฐํํ๋ FactoryResolver๋ผ๋ ์๋น์ค๋ฅผ ๋ ธ์ถํ๋ ๊ฒ์ด ๋ ๋์ ๊ฒ์ด๋ผ๊ณ ์๊ฐํ์ง ์์ต๋๊น? ๊ตฌ์ฑ ์์๋ ์์ฒด ๋ชจ๋์ ์ธ์ ํฐ์ ๋ํ ์ฌ๋ฐ๋ฅธ ์ฐธ์กฐ๋ฅผ ๊ฐ๊ธฐ ๋๋ฌธ์ ๋๋ค. ์ด๋ ๊ฒํ๋ฉด ๊ตฌ์ฑ ์์์ ๋ํ ๊ณต๊ฐ API๋ฅผ ๋ ธ์ถํ๋ ๊ฒ๊ณผ ๊ฐ์ต๋๋ค.
๋น์ทํ ์์ ์ ์ํํ๋ ค๊ณ ํ์ง๋ง ๋ด ๋ชจ๋์ด ์ง์ฐ ๋ก๋๋์ง ์๊ณ ์์์ ์ค๋ช ํ ๋ฌธ์ ์ ๋ถ๋ช์ณค์ต๋๋ค. ์ด๋ก ์ ์ผ๋ก ์ฌ๊ธฐ์์ ๋ถ๋ช ํ ๊ฒ์ ๋์น๊ณ ์์ง ์๋ ํ ์๋ํด์ผ ํฉ๋๊น?
@h-arora ๋๋ comp๋ฅผ๋ณด๊ณ ๋์ผํ ๋ฌธ์ ๋ฅผ ๋ฐ๊ฒฌํ์ต๋๋ค. _factories์ ์์ง๋ง ๋ฌธ์ ๋ ๋ชจ๋์ ์ค์ ๋ก ํ์๋์ง ์๋ ๋น๋ ์ค๋ฅ๊ฐ ์๋ค๋ ๊ฒ์ ๋๋ค.
#17168์์ ๊ทน๋ณต(์ด๊ฒ์ ๋ณต์ ๋ณธ์ผ๋ก ํ์๋จ)
ํ์ฌ ํ๋
์ฑ์ RootInjector์์ ์ ๊ณตํ๋ ComponentFactoryResolver๋ฅผ ์ฌ์ฉํ์ฌ ๊ตฌ์ฑ ์์๋ฅผ ๋์ ์ผ๋ก ์์ฑํ๋ ์ฑ๊ธํค ์๋น์ค๊ฐ ์๊ณ ์ง์ฐ ๋ชจ๋์ ์ ์ธ๋ entryComponent๋ฅผ ์์ฑํ๋ ค๋ ๊ฒฝ์ฐ ๋ฃจํธ ์ธ์ ํฐ๊ฐ ์ด์ ๋ํด ์์ง ๋ชปํ๊ธฐ ๋๋ฌธ์ ์คํจํฉ๋๋ค.
์์๋๋ ํ๋
์ด๊ฒ์ ๋ฃจํธ ์ธ์ ํฐ๊ฐ ์์ ์ธ์ ํฐ์ ์ํด ์๋ ค์ง entryComponents์ ๋ํด ์์ง ๋ชปํ๋ค๋ ์ ์ ๊ณ ๋ คํ๋ฉด ์ค์ ๋ก ์์๋๋ ๋์์
๋๋ค.
ํ๋์ ๋ฐ๊พธ๋ ๋๊ธฐ/์ฌ์ฉ ์ฌ๋ก๋ ๋ฌด์์
๋๊น?
๋๊ธฐ๋ Angular์ ๋ชจ๋์ ์ค๊ณ๊ฐ ์ ํํ์ง๋ง ๋ณต์ก์ฑ์ ํ์ฉํ์ฌ ๋์ ๊ตฌ์ฑ ์์๋ฅผ ๋ง๋๋ ๊ฒ์
๋๋ค.
๊ฐ๋ ๋ฒ์ : 2.4.9
์ ์๋ ์๋ฃจ์ : ์ด๊ฒ์ Angular ์์ฒด์์ ์ฑํํ ๋ชจ๋์ ์ค๊ณ์ ์ด๊ธ๋์ง๋ง, LazyModule์ entryComponents๋ฅผ RootInjector์ entryComponents์ ์ถ๊ฐํ๊ณ ํ์ํ ๋๋ง๋ค RootInjector ๋ฒ๋์ ํผํ๊ธฐ ์ํด ๋์ ๊ตฌ์ฑ ์์๋ฅผ ์์ฑํ๊ธฐ ์ํด ์ ๊ณต๋๋ ๋ณต์ก์ฑ์ ์ํํ ์ ์๋ค๊ณ ์๊ฐํฉ๋๋ค. (LazyModule์ ํ๊ดด), ์ด์ ์ ์ฃผ์ ๋ entryComponents๋ ๋ฐฐ์ด์์ ์ง์์ง๋๋ค.
๊ฐ๋จํ ๋งํ๋ฉด:
ํ์ด์ง๊ฐ ๋ก๋๋จ -> RootInjector๊ฐ ์์ฑ๋ฉ๋๋ค.
LazyModule -> ChildInjector๊ฐ ์์ฑ๋๋๋ก ํ์ฑํํ๋ ํ์์ ์คํํฉ๋๋ค.
RootInjector.entryComponents.append(childInjector.entryComponents)
๋ค๋ฅธ ๊ณณ์ผ๋ก์ ํ์ ๋ฌธ์ -> LazyModule์ด ๋ ์ด์ ํ์ํ์ง ์์ผ๋ฏ๋ก ํ๊ดด๋ฉ๋๋ค.
RootInjector.entryComponents.destroyLastAppendedEntryComponents()
์ข์ ๊ฒ์ ์ ์ํ ์ ์๊ธฐ๋ฅผ ๋ฐ๋๋๋ค.
๋๋ ํ์ฌ ํ๋ก์ ํธ๋ฅผ ๋ถํดํ๊ณ ์ง์ฐ ๋ก๋ฉ์ ๊ตฌํํ๋ ๋ฐ ๋ฉฐ์น ์ ๋ณด๋์ต๋๋ค. ๋๋ ์ฐ๋ฆฌ์ ๋ชจ๋ฌ์ด ๊ฐ๊ฐ์ ๋ชจ๋์ ์๋ ํญ๋ชฉ์ผ๋ก ์ธํด ์๋ํ์ง ์๋๋ค๋ ๊ฒ์ ๊นจ๋ฌ์์ต๋๋ค. ์ฐ๋ฆฌ๋ ๋๋์์๊ณ ์์ ์ ๊ธฐ๋ค๋ฆฌ๊ณ ์์ต๋๋ค...
์ด๊ฒ์ ์์ ๋ ๊ฒ์ ๋๊น ์๋๋ฉด ํด๊ฒฐ ๋ฐฉ๋ฒ์ ๋ชจ์ํด์ผ ํฉ๋๋ค. ์ฌ๋๋ค์ด ์์์ ํ๋ ๊ฒ์ฒ๋ผ.
@ravtakhar ์๋ง๋ ์ด๊ฒ์ด ๋ฐฑ๋ก๊ทธ์ ์๊ณ ํ์ฌ ํ ๋น๋ ์ฐ์ ์์๊ฐ ์๊ธฐ ๋๋ฌธ์ ํด๊ฒฐ ๋ฐฉ๋ฒ์ ์ดํด๋ณด๊ณ ์ถ์ ๊ฒ์ ๋๋ค.
๋๋ ํ์ฌ ํ๋ก์ ํธ๋ฅผ ๋ถํดํ๊ณ ์ง์ฐ ๋ก๋ฉ์ ๊ตฌํํ๋ ๋ฐ ๋ฉฐ์น ์ ๋ณด๋์ต๋๋ค. ๋๋ ์ฐ๋ฆฌ์ ๋ชจ๋ฌ์ด ๊ฐ๊ฐ์ ๋ชจ๋์ ์๋ ํญ๋ชฉ์ผ๋ก ์ธํด ์๋ํ์ง ์๋๋ค๋ ๊ฒ์ ๊นจ๋ฌ์์ต๋๋ค. ์ฐ๋ฆฌ๋ ๋๋์์๊ณ ์์ ์ ๊ธฐ๋ค๋ฆฌ๊ณ ์์ต๋๋ค...
์ด๊ฒ์ ์์ ๋ ๊ฒ์ ๋๊น ์๋๋ฉด ํด๊ฒฐ ๋ฐฉ๋ฒ์ ๋ชจ์ํด์ผ ํฉ๋๋ค. ์ฌ๋๋ค์ด ์์์ ํ๋ ๊ฒ์ฒ๋ผ.
์ฌ๊ธฐ์ ๊ฐ์ ๋ฌธ์
@tmirun ... ํ๋ก๊ทธ๋๋ฐ ๋ฐฉ์์ผ๋ก ์ด๋ฌํ ๊ฒ์ผ๋ฅธ ๋ชจ๋์ ๋ก๋ํ๋ฉด ... ๋ชจ๋ ๊ถํ์ด ์์ต๋๋ค. ๊ทธ๋ฌ๋ฉด ... ๋ง์ง๋ง์ผ๋ก ๋ชจ๋ ์ฐธ์กฐ๋ฅผ ์ป๊ณ moduleRef.componentFactoryResolver.resolveComponentFactory(entryComponentType)
๋ฅผ ํธ์ถํ ์ ์์์ ์๋ฏธํฉ๋๋ค.
์์ ๋ง์ ๋ฌธ์ ์ ๋์ผํ ๋ฌธ์ ๊ฐ ์์๊ณ ๋ชจ๋ ๋จ์ผ lazyLoaded ๋ชจ๋์ด ์ด๋ฌํ ๊ตฌ์ฑ ์์๋ฅผ ๋ก๋ํ๋๋ก appModule์ ์๋ ํญ๋ชฉ์ ํฌํจํ์ฌ ๋ชจ๋ entryComponents๋ฅผ ์ ์ธํ๊ณ ๋ด๋ณด๋ด๊ณ ๋ด๋ณด๋ด๋ ๋ ๋ฆฝ ์คํํ ๋ชจ๋์ ๋ง๋ค์ด ํด๊ฒฐํ์ต๋๋ค.
๋ด๊ฐ ์ฐพ์ ์ ์ผํ ๋ค๋ฅธ ์คํ ๊ฐ๋ฅํ ์๋ฃจ์
์ entryComponent
๋ฅผ ํธ์คํ
ํ๋ ๋ชจ๋์ ๋ฐ๋ผ ์ฌ์ฉ์ ์ ์ ComponentFactoryResolver
๋ฅผ ์ ๊ณตํ์ฌ ๋ํ ์์๋ฅผ ์์ฑํ๋ ๊ฒ์
๋๋ค.
์ฃผ์ : ๋ฃจํธ์ ์ ๊ณต๋ ์ฑ๊ธํค ๋ชจ๋ ์ ๊ธฐ๋ณธ ๋ชจ๋ ํญ๋ชฉ ๊ตฌ์ฑ ์์ ๋ง ๋ณผ ์ ์๋ค๋ ๊ฒ์ ๊ธฐ์ตํ์ญ์์ค. ๊ทธ๋์ ์ฒซ ๋ฒ์งธ ์๋ฃจ์
์ ์๊ฐํด ๋ด๊ธฐ๋ก ๊ฒฐ์ ํ์ต๋๋ค. ๊ทธ๋ ์ง ์์ผ๋ฉด ์์ฉ ํ๋ก๊ทธ๋จ์ ๊ฐ ํ์ ๋ชจ๋์ ๋ํด ๋ํ ์์ ์์ฑ๊ธฐ๋ฅผ ๋ง๋๋ ๊ฒ์ ๋ํด ์๊ฐํ ์ ์์ต๋๋ค. ํด๋น ํ์ ๋ชจ๋์ ๊ฒฐ๊ตญ ํด๋น ๋ชจ๋์ ComponentFactoryResolver
๋ชจ๋ฌ์ ์์ฑํ๋ ์๋น์ค๋ฅผ ์ ๊ณตํ์ฌ ๋ค๋ฅธ entryComponents
๊ฐ ์๋ ๊ณต์ ๋ชจ๋์ ํฌํจํฉ๋๋ค.
... ์ผ๋ฐ์ ์ผ๋ก ์ง์๋๋ ์๋ฃจ์ ์ ์ฌ์ฉํ ์ ์์ด์ผ ํฉ๋๋ค(์ผ๋ถ ์๋ก์ด API + Ivy ์ง์) ... Jason์ ํ๋ ์ ํ ์ด์ ์ผ๋ก ์ธํด http://www.youtube.com/watch?v=2wMQTxtpvoY&t=131m24s ... Angular 7.2 . .. ํ์ง๋ง ๋ฒ์ 7.2๊ฐ ์ฌ๋ฐ๋ฅด๊ฒ ์ธ๊ธ๋์๋์ง ํ์คํ์ง ์์ต๋๋ค.
CDK ์ค๋ฒ๋ ์ด ์ ๋์ผํ ๋ฌธ์ ๊ฐ ์์ต๋๋ค. CDK ์ค๋ฒ๋ ์ด ์์ ๋น๋๋ ๊ฐค๋ฌ๋ฆฌ ํ๋ฌ๊ทธ์ธ ์ด ์์ง๋ง ์ด ๋ฌธ์ ๋๋ฌธ์ ๋ฃจํธ ๋ชจ๋์์ ๊ฐ์ ธ์์ผ ํฉ๋๋ค.
BTW, ๋ด ๊ฒฝ์ฐ์ ๋ํ ํด๊ฒฐ ๋ฐฉ๋ฒ์ด ์์ต๋๊น?
@MurhafSousli ... lol, ๋๋ ๊ทธ๊ฒ์ด ์๋์ ์ธ ๊ฒ์ด๋ผ๊ณ ๋ค์ ๋งํด์ผํฉ๋๋ค. ์ด๊ฒ์ 100๋ฒ ์ค๋ช ํ์ต๋๋ค... ๋ฃจํธ ์ธ์ ํฐ๊ฐ ํ๋ ์๊ณ ์ง์ฐ ๋ก๋๋ ๋ชจ๋์ ํ ์์ค ๊น์ด์ด๋ฏ๋ก ๋ฃจํธ ์์ค์์ ๊ตฌ์ฑ ์์๋ฅผ ๋ง์ง ์ ์๋ค๊ณ ๊ฐ์ ํ ์ ์์ต๋๋ค. ๊ทธ๋ฌ๋ ์ฌ๋ฌ ๋ฒ ๋งํ๋ฏ์ด ํ๋ก๊ทธ๋๋ฐ ๋ฐฉ์์ผ๋ก ํด๋น ๋ชจ๋์ ๋ก๋ํ ์ ์์ต๋๋ค. ๋ฐ๋ผ์ ํด๋น ๋ชจ๋ ์ฐธ์กฐ๋ฅผ ์ป์ ์ ์์ต๋๋ค. ๊ทธ๋ฌ๋ฉด ํด๋น ๊ตฌ์ฑ ์์์ ๋๋ฌํ ์ ์์ต๋๋ค.
๋๋ ๋ชฉ๋ก์ ์ถ๊ฐ ๐. ๋ด ๊ตฌ์ฑ ์์๋ฅผ ๋์ ์ผ๋ก ๋ก๋ํ๋ ๋ชจ๋ฌ ๊ตฌ์ฑ ์์๊ฐ ์๊ณ ์ฑ ๊ตฌ์ฑ ์์์ ์ผ๋ถ๊ฐ ์๋๋ฏ๋ก ์๋ชป๋ ๋์์ธ์ด ๋ ๋ชจ๋ ๊ตฌ์ฑ ์์๋ฅผ ์ฑ์ ๋ก๋ํ๊ณ ์ถ์ง ์์ต๋๋ค. ํ์ง๋ง ์ด๊ฒ์ด ๋ด๊ฐ ์ด ๋ชจ๋ ๊ฒ์ ์ฝ์ ํ์ ์ถ์ธกํด์ผ ํ๋ ๊ฒ์ ๋๋ค.
@msarica ๋ ์ค๋ ๋์๊ฒ ๋๋ฆฌ๊ฒ ๋ก๋๋๋ ์๋น์ค๋ฅผ ๋ง๋ค๊ณ ํ์ ์ ์ฌ๋ ๊ธ๋ก๋ฒ ์๋น์ค๋ฅผ ํ์ฅํ ๋ค์, ๋๋ฆฌ๊ฒ ๋ก๋๋ ์๋น์ค๋ฅผ ์ฌ์ฉํ์ฌ ํ์ ์ ์ด๋ฉด ์์๋๋ก ์๋ํ ๊ฒ์ด๋ผ๊ณ ์ง์ ํ์ต๋๋ค.
๊ธ๋ก๋ฒ PopupService
@Injectable({ providedIn: 'root' })
export class PopupService {
constructor(
private injector: Injector,
private overlay: Overlay,
) {}
open<T = any>(component: ComponentType<T>) { ... }
}
ํน์ ์๋น์ค๋ MyFeatureModule
์์๋ง ์ง์ฐ ๋ก๋๋จ
@Injectable()
export class MyFeaturePopupService extends PopupService {}
๋๋ฆฌ๊ฒ ๋ก๋๋ MyFeatureModule
์ ์ผ๋ถ์ธ ๊ตฌ์ฑ ์์์์ ์ฌ์ฉ
@Component({ ... })
export class MyFeatureComponent {
constructor(
private myFeaturePopupService: MyFeaturePopupService,
) {}
openPopup() {
this.myFeaturePopupService.open(MyFeaturePopupComponent);
}
}
์ง์ฐ ๋ก๋ ๋ชจ๋์ ์ ์ ์
@NgModule({
imports: [MyFeatureRoutingModule],
declarations: [
MyFeatureComponent,
MyFeaturePopupComponent,
],
entryComponents: [MyFeaturePopupComponent],
providers: [MyFeaturePopupService],
})
export class MyFeatureModule {}
Angular 7์์ ํ ์คํธ๋์์ต๋๋ค.
์ฐธ๊ณ : ์ฐ๋ฆฌ ์ฝ๋๋ Angular CDK์ Overlay
๋ฅผ ํ์ฉํ์ง๋ง ๊ฐ๋
์ ๋์ผํฉ๋๋ค(์ด ์ค๋ ๋์ ๋ค๋ฅธ ๋ถ๋ถ์ ๋ฐ๋ฅด๋ฉด ์ฐ๋ ค๋๋ ๊ฒ์ Injector
์
๋๋ค).
... ์ผ๋ฐ์ ์ผ๋ก ์ง์๋๋ ์๋ฃจ์ ์ ์ฌ์ฉํ ์ ์์ด์ผ ํฉ๋๋ค(์ผ๋ถ ์๋ก์ด API + Ivy ์ง์) ... Angular 7.2์์
... @mlc-mlapis ์ผ๋ฐ ์ง์ ์๋ฃจ์ ์ด ๋ฌด์์ธ์ง ํ์ฅํ ์ ์์ต๋๊น?
@cedvdb ... ์ค์ ๋ก ์๋ฃจ์ ์ฒ๋ผ ๋ณด์ด๋ ํ๋ ์ ํ ์ด์ ์์ ์ค์ํ ์๊ฐ์ ์ธ๊ธํ์ต๋๋ค. ์ด๋ ํ๋ก๊ทธ๋๋ฐ ๋ฐฉ์์ผ๋ก ์ง์ฐ ๋ก๋๋ ๋ชจ๋์ ๋ก๋ํ ๋ค์ ๊ตฌ์ฑ ์์, ์ง์๋ฌธ, ...
๊ทธ ๋ฐฉ๋ฒ์ ์ค๋๋ ์๋ ์ฌ์ฉ ๊ฐ๋ฅํ์ง๋ง Angular CLI๋ฅผ ์ฌ์ฉํ๋ฉด ๋ผ์ฐํฐ๋ฅผ ํตํด ์ฌ์ฉํ์ง ์๋ ๊ฒฝ์ฐ์๋ ํญ์ ํ์ํ ๊ฒฝ๋ก๋ฅผ ์ ์ํด์ผ ํฉ๋๋ค.
์. ์ด๊ฒ์ ๋งค์ฐ ์์์น ๋ชปํ ์ผ์ด์์ต๋๋ค.
@angular/cdk ํฌํธ ๋ฐ ์ค๋ฒ๋ ์ด๋ฅผ ๊ธฐ๋ฐ์ผ๋ก ๋ด ์์ ์ ๋ํ ์์คํ ์ ๊ตฌํํ์ต๋๋ค: https://stackblitz.com/edit/cdk-dialog-example-p1. ์ด ๋ฌธ์ ๋ ๋ฐ์์ต๋๋ค.
@CWSpear ๊ฐ ์ธ๊ธํ ํด๊ฒฐ ๋ฐฉ๋ฒ์ด ์๋ํฉ๋๋ค. ๊ธฐ๋ฅ ๋ชจ๋์์ ์๋น์ค๋ฅผ ํ์ฅํด์ผ ํ๊ณ ๊ทธ๊ฒ ์ ์๊ฒ ํจ๊ณผ์ ์ด์์ต๋๋ค. ์ผ๋ง๋ ์ด์ํ๊ฐ!
์ด์ ๋ํ ๋ ์ฐ์ํ ์๋ฃจ์ ์ด ์์ต๋๊น?
์ด์ ๋ํ ๋ ์ฐ์ํ ์๋ฃจ์ ์ด ์์ต๋๊น?
๋ด๊ฐ ์คํํด ์จ ๊ฒ์ ๊ฒ์ผ๋ฅธ ๋ชจ๋ ์์ค์์ ์คํ๊ธฐ ๋ํผ๋ฅผ ๋ค์ ์ ๊ณตํ๋ ๊ฒ์ ๋๋ค. ์ด ๋ํผ๊ฐ ์์ฑ์์์ ์ธ์ ํฐ๋ฅผ ์ฌ์ฉํ๋๋ก ํ๊ณ ์ด๋ฅผ ์ค์ ๋ฐ์ฒ(๋ฃจํธ์์ ์ฃผ์ ๋จ)์ ์ ๋ฌํฉ๋๋ค. ํ ์ค ์์ ์ ๋๋ค. ์ผ๋ง๋ ์์ ์ ์ธ์ง๋ ๋ชจ๋ฅด๊ฒ ์ง๋ง,
@NgModule({
declarations: [LazyRootComponent, LazyEntryComponent],
entryComponents:[LazyEntryComponent],
providers:[LauncherWrapper], //<---- reprovision here
imports: [
CommonModule,
LazyModRoutingModule
]
})
export class LazyModModule { }
@Injectable()
export class LauncherWrapper {
constructor(private injector: Injector, private launcher:ActualLaucherClass) {
}
๋ด ๋ฌธ์ ๋ @Injectable({providedIn: 'root'})
๋ฅผ ์ฌ์ฉํ๊ธฐ ๋๋ฌธ์ ๋ฐ์ํ์ต๋๋ค. @Injectable({providedIn: MyModule})
๋ก ๋ณ๊ฒฝํ๋ฉด ํด๊ฒฐ๋ฉ๋๋ค. ๐
์๋น์ค๊ฐ ์ง์ฐ ๋ก๋๋ ๋ชจ๋ ํญ๋ชฉ ๊ตฌ์ฑ ์์์ ์ก์ธ์คํด์ผ ํ๊ธฐ ๋๋ฌธ์ ์ด๋ ์๋ฏธ๊ฐ ์์ต๋๋ค. ๋ฃจํธ์์ ์ ๊ณต๋๋ฉด ๋ชจ๋์ด ๋ฃจํธ ์ธ์ ํฐ์์ ์ผ์์ ์ผ๋ก ์ฐ๊ฒฐ์ด ๋์ด์ง๊ธฐ ๋๋ฌธ์ ์ก์ธ์คํ ์ ์์ต๋๋ค.
@jmcclanahan ๋ฐ @dirkluijk ๋๋ถ์ ๋ช ์๊ฐ ๋์ ๋จธ๋ฆฌ๋ฅผ ๊ธ์ ์ด๋ฉฐ ๊ทํ์ ์ค๋ช ์ด ์ ๊ฒฝ์ฐ์ ๋ํ ์๋ฃจ์ ์ ์ฐพ๋ ๋ฐ ๋์์ด ๋์์ต๋๋ค.
์ฌ๋ฐ๋ฅธ ๋ฐฉ๋ฒ์ด ๋ฌด์์ธ์ง ๊ถ๊ธํฉ๋๋ค.
@angular/cdk๊ฐ ํ์ํฉ๋๊น? ์์ ?
ํน๋ณํ ๋์ฒด ๋ ์ง์คํธ๋ฆฌ์ ๋ํด ์์ง ๋ชปํ๊ณ ComponentFactoryResolver
๋ง ์ฌ์ฉํ๋ ํ์ฌ ์ฝ๋๊ฐ ๋ง๊ธฐ ๋๋ฌธ์ ํตํฉํ๋ ค๋ ์ฝ๋๊ฐ ํ์ํ ๊ฒฝ์ฐ ํด๊ฒฐ ๋ฐฉ๋ฒ์ด ์คํ ๊ฐ๋ฅํ๋ค๊ณ ์๊ฐํ์ง ์์ต๋๋ค.
์ด๋ฅผ ์ผ๋์ ๋๊ณ ๋ด "ํด๊ฒฐ์ฑ "์ด ์์ต๋๋ค. CoalescingComponentFactoryResolver . ๋ค์๊ณผ ๊ฐ์ด ์ฑ ๋ชจ๋์์ ์ ๊ณตํ๊ณ ํด๋น ์์ฑ์์์ ์ด๊ธฐํํด์ผ ํ๋ ์๋น์ค์ ๋๋ค.
@NgModule({
providers: [CoalescingComponentFactoryResolver]
})
class AppModule {
constructor(coalescingResolver: CoalescingComponentFactoryResolver) {
coalescingResolver.init();
}
}
๊ทธ๋ฐ ๋ค์ ์ง์ฐ ๋ก๋ ๋ชจ๋์ ์ด๋ฅผ ์ฃผ์
ํ๊ณ ์์ฒด ComponentFactoryResolver
์ธ์คํด์ค๋ฅผ ๋ฑ๋กํด์ผ ํฉ๋๋ค. ์ด๋ ๊ฒ:
@NgModule({})
export class LazyModule {
constructor(
coalescingResolver: CoalescingComponentFactoryResolver,
localResolver: ComponentFactoryResolver
) {
coalescingResolver.registerResolver(localResolver);
}
}
์ด ์์ ์ด ์๋ฃ๋๋ฉด ์ง์ฐ ๋ก๋ ๋ชจ๋์ ํญ๋ชฉ ๊ตฌ์ฑ ์์๋ ์ง์ฐ ๋ก๋๋์ง ์์ ์๋น์ค์์ ์ฌ์ฉํ ์ ์์ด์ผ ํฉ๋๋ค.
์๋ ์๋ฆฌ: ์ด๊ธฐํ๋๋ฉด ๋ฃจํธ ์ฑ์ ComponentFactoryResolver
๋ฅผ ์ฃผ์
ํ๊ณ ์์ญ์ด๋ ์์ฒด ๊ตฌํ์ ํธ์ถํ๊ธฐ ์ํด resolveComponentFactory
๋ฉ์๋๋ฅผ ํจ์นํฉ๋๋ค. ์ด ๊ตฌํ์ ๋จผ์ ๋ฑ๋ก๋ ๋ชจ๋ ์ง์ฐ ๋ชจ๋ ํด์๊ธฐ์์ ๊ตฌ์ฑ ์์ ํฉํ ๋ฆฌ๋ฅผ ํด์ํ ๋ค์ ๋ฃจํธ ์ฑ ํด์๊ธฐ๋ก ํด๋ฐฑํฉ๋๋ค(์ํ ํธ์ถ์ ํผํ๊ธฐ ์ํ ์ฝ๊ฐ์ ์ถ๊ฐ ๋
ผ๋ฆฌ๊ฐ ์์ง๋ง ์ด๊ฒ์ด ์์ ์
๋๋ค).
๋ค, ๊ฝค ์ฌํ ํดํน์ ๋๋ค. ํ์ง๋ง ์ง๊ธ ๋น์ฅ์ Angular 7์์ ์๋ํฉ๋๋ค. ๋๊ตฐ๊ฐ์๊ฒ ์ ์ฉํ ์๋ ์์ต๋๋ค.
@CWSpear ์ ์๋ฃจ์ ์ ๊ธฐ๋ฐ์ผ๋ก ๊ตฌ์ฑ ์์์ ์ ๊ณต์ ๋ฐฐ์ด์ ๊ธฐ์กด PopupService๋ฅผ ์ถ๊ฐํ๊ธฐ๋ง ํ๋ฉด ์ด ์์ ์ ์ํํ ์ ์์ต๋๋ค. ๋ฐ๋ผ์ ๋ณ๋์ "๊ธฐ๋ฅ๋ณ" PopupService๋ฅผ ๋ง๋ค ํ์๊ฐ ์์์ต๋๋ค.
๊ทธ๋์ ๋ค์๊ณผ ๊ฐ์ด:
@Component({
...
providers: [PopupService] //<---- generic PopupService
})
export class MedicationPortletComponent implements OnInit, OnDestroy {
...
๋๋ ์ง์ฐ ๋ก๋ ๋ชจ๋์ NgModule์ ์ ๊ณต์ ๋ฐฐ์ด์ PopupService๋ฅผ ์ถ๊ฐํ ์๋ ์์ต๋๋ค. ์:
@NgModule({
declarations: [
...
],
imports: [
...
],
providers: [PopupService] //<--- here's the PopupService!
})
export class SharedFeaturePatientModule {}
@jonrimmer ์ต๊ณ ์ ์๋ฃจ์
@jonrimmer ์ด๊ฒ์ ์ต์ํ์ ๋ฐฉํด๊ฐ ๋๋ฉฐ ์ฐ๋ฆฌ์ ์ฌ์ฉ ์ฌ๋ก์ ์๋ฒฝํ๊ฒ ์๋ํฉ๋๋ค. ์ ์ ์ ํฌ ํ์๊ฒ ํฐ ๊ฐ์ฌ๋ฅผ ๋๋ฆฝ๋๋ค :)
๊ฒ์ผ๋ฅธ ๋ผ์ฐํฐ์ ๊ฒฝ์ฐ ์ด๋ป๊ฒ ์ํํ๊ฒ ์๋ํ๋์ง ๊ถ๊ธํ์ง๋ง ๊ฒ์ผ๋ฅธ ๊ฒฝ๋ก๋ฅผ ์ ๊ฑฐํ๊ณ ngModuleFactoryLoader๋ฅผ ์ฌ์ฉํ์ฌ ์๋์ผ๋ก ๋ชจ๋์ ๋ก๋ํ๋ฉด ์ค๋ฅ๊ฐ ๋ฐ์ํฉ๋๋ค.
//Ignore the syntax
CompA {
openDialog() {//invoked on button click of this comp
matDialog.open(FilterComponent);//works fine in case of lazy route but throws error when manually loaded
}
}
ModuleA {//lazy module
imports: [MatDialog, FilterModule],
declaration: [CompA]
}
FilterModule {
declaration: [FilterComponent],
entryComponent: [FilterComponent]
}
FilterComponent { ...
}
@jmcclanahan @MartinJHItInstituttet @splincode
async showBar() {
const module = await import("../bar/bar.module");
const compiled = this._compiler.compileModuleAndAllComponentsSync(
module.BarModule
);
compiled.ngModuleFactory.create(this._injector);
let factory: any = compiled.componentFactories.find(
componentFactory => componentFactory.selector === "app-dialog"
);
this.modal.show(factory.componentType);
}
@Jimmysh JIT ๋ชจ๋์ฉ์
๋๋ค. AOT ๋ชจ๋์์๋ import("../bar/bar.module.ngfactory")
๋ฅผ ๋ก๋ํด์ผ ํฉ๋๋ค( compileModuleAndAllComponentsSync
๋ ์ฌ์ฉํ ์ ์์).
@mlc-mlapis
https://github.com/angular/angular/blob/master/aio/src/app/custom-elements/elements-loader.ts ์์ ํฌ๊ฒ ์๊ฐ์ ๋ฐ์์ต๋๋ค.
https://github.com/Jimmysh/ng-lazy-component-load
AOT JIT๋ฅผ ์ฌ์ฉํ ์ ์์ต๋๋ค. ๋๋ฅผ ์ํ ์ฑ๊ณต.
import { InjectionToken, Type } from '@angular/core';
import { LoadChildrenCallback } from '@angular/router';
export const ELEMENT_MODULE_LOAD_CALLBACKS_AS_ROUTES = [
{
path: 'aio-foo',
loadChildren: () => import('../foo/foo.module').then(mod => mod.FooModule)
}
];
export interface WithCustomElementComponent {
customElementComponent: Type<any>;
}
export const ELEMENT_MODULE_LOAD_CALLBACKS_TOKEN = new InjectionToken<Map<string, LoadChildrenCallback>>(
'aio/elements-map'
);
export const ELEMENT_MODULE_LOAD_CALLBACKS = new Map<string, LoadChildrenCallback>();
ELEMENT_MODULE_LOAD_CALLBACKS_AS_ROUTES.forEach(route => {
ELEMENT_MODULE_LOAD_CALLBACKS.set(route.path, route.loadChildren);
});
```ํ์ดํ์คํฌ๋ฆฝํธ
import { Compiler, Inject, Injectable, NgModuleFactory, NgModuleRef, Type, ComponentFactory } from '@angular/core';
'@angular/router'์์ { LoadChildrenCallback } ๊ฐ์ ธ์ค๊ธฐ;
'./lazy-component-registry'์์ { ELEMENT_MODULE_LOAD_CALLBACKS_TOKEN } ๊ฐ์ ธ์ค๊ธฐ;
@์ฃผ์ฌ๊ฐ๋ฅ({
์ ๊ณต๋จ: '๋ฃจํธ'
})
๋ด๋ณด๋ด๊ธฐ ํด๋์ค LazyComponentLoader {
private moduleToLoad: ์ง๋
private moduleLoading = ์ ์ง๋
๊ฑด์ค์(
๊ฐ์ธ moduleRef: NgModuleRef
@Inject(ELEMENT_MODULE_LOAD_CALLBACKS_TOKEN) elementModulePaths: ์ง๋
๊ฐ์ธ ์ปดํ์ผ๋ฌ: ์ปดํ์ผ๋ฌ
) {
this.modulesToLoad = ์๋ก์ด ์ง๋(elementModulePaths);
}
load(๊ฒฝ๋ก: ๋ฌธ์์ด, ์ ํ์?: ๋ฌธ์์ด): ์ฝ์
if (this.modulesLoading.has(๊ฒฝ๋ก)) {
๋ฐํ this.modulesLoading.get(๊ฒฝ๋ก);
}
if (this.modulesToLoad.has(path)) {
const modulePathLoader = this.modulesToLoad.get(path);
const loadedAndRegistered = (modulePathLoader() as Promise<NgModuleFactory<any> | Type<any>>)
.then(elementModuleOrFactory => {
if (elementModuleOrFactory instanceof NgModuleFactory) {
return elementModuleOrFactory;
} else {
return this.compiler.compileModuleAsync(elementModuleOrFactory);
}
})
.then(elementModuleFactory => {
const elementModuleRef = elementModuleFactory.create(this.moduleRef.injector);
const factories: Map<any, ComponentFactory<any>> = (elementModuleRef.componentFactoryResolver as any)
._factories;
if (selector) {
const find = Array.from(factories.keys()).find(type => {
const factory = factories.get(type);
return factory.selector === selector;
});
if (find) {
return find;
} else {
return Promise.reject(new Error(`not found selector:${selector}`));
}
}
this.modulesToLoad.delete(path);
return;
})
.catch(err => {
this.modulesLoading.delete(path);
return Promise.reject(err);
});
this.modulesLoading.set(path, loadedAndRegistered);
return loadedAndRegistered;
}
return Promise.resolve();
}
}
``` typescript
async showFoo() {
await this.lazyComponentLoader.load('aio-foo');
this.modal.show(FooDialogComponent);
}
async showFoo2() {
const aaa = await this.lazyComponentLoader.load('aio-foo', 'app-dialog');
this.modal.show(aaa);
}
@Jimmysh ... ๊ทธ๋ฌ๋ฉด ๋ง์ต๋๋ค. JIT ๋ชจ๋์์๋ง ์ปดํ์ผ๋ฌ๋ฅผ ์ฌ์ฉํ๊ณ ์์ต๋๋ค.
constructor(private compiler: Compiler) {}
...
if (elementModuleOrFactory instanceof NgModuleFactory) {
return elementModuleOrFactory;
} else {
return this.compiler.compileModuleAsync(elementModuleOrFactory);
}
@mlc-mlapis AOT๋ NgModuleFactory๊ฐ ๋ฉ๋๋ค. https://github.com/Jimmysh/ng-lazy-component-load ๋ฅผ ํ ์คํธํ ์ ์์ต๋๋ค.
์๋ ํ์ธ์ ์ฌ๋ฌ๋ถ, ์ด ์ค๋ ๋์ ์ฃผ์ ๋๋ถ์ ๋ผ์ฐํ ์ ํตํด ๋ชจ๋ ์ธ๋ถ์ ๋ก๋๋ ํญ๋ชฉ ๊ตฌ์ฑ ์์๋ฅผ ๊ฐ์ ธ์ค๋ ค๊ณ ํ๋ ํด๊ฒฐ ๋ฐฉ๋ฒ์ด ๋ ํจํค์ง๋ฅผ ๋ง๋ค์์ต๋๋ค. ๋ณด์๊ณ ๋์์ด ๋์๊ธธ ๋ฐ๋๋๋ค.
๋ ํฌ ๋งํฌ: https://github.com/Jonathan002/route-master-example
๋ฐ๋ชจ URL: https://jonathan002.github.io/route-master-example/
FWIW, @CWSpear ๋ฐ @dirkluijk ์ ์ ์ฌํ ๋งฅ๋ฝ์์ ๋ด ์๋น์ค์์ providedIn: 'root'
๋ฅผ ์ ๊ฑฐํ์ต๋๋ค(์ง์ฐ ๋ก๋ ๋ชจ๋์ ์ ์๋์ด ์๊ณ entryComponent
์ฌ๋ ์ฑ
์์ด ์์). ๋์ ์๋น์ค๋ฅผ ์ง์ ํ์ต๋๋ค. ๋ด ์ง์ฐ ๋ชจ๋ ์ ์์ providers
์์. ์ด ์๋น์ค์ ๊ฐ์ฅ ํฐ/์ ์ผํ ๋จ์ ์ ์๋น์ค๊ฐ ๋ ์ด์ ๋๋ฌด๋ฅผ ํ๋ค ์ ์๋ค๋ ๊ฒ์
๋๋ค.
providedIn: MyLazyModule
๋ฅผ ์ฌ์ฉํ๋ฉด ์ํ ์ข
์์ฑ ๊ฒฝ๊ณ ๊ฐ ๋ฐ์ํ์ต๋๋ค.
@jonrimmer ๊ฐ์ฌํ์ง ์๊ณ ๊ธ์ ๋จ๊ธธ ์๊ฐ ์์์ด์!! ๋น์ ์ ๋ด ํ๋ฃจ๋ฅผ ์ ์ฅํ์ต๋๋ค.
๋ด ์๊ฒฌ์ ์ฝ๋ ๋ชจ๋ ์ฌ๋์๊ฒ ์ด ๋ฉ์ง ๋ต๋ณ ์ ๋ณด๋ฉด ์๊ฐ์ ๋ญ๋นํ์ง ์๋ ๋ฐ ๋์์ด ๋ ์ ์์ต๋๋ค.
Angular 8.2.0์์ ์์ง ํด๊ฒฐ๋์ง ์์์์ ํ์ธํ์ต๋๋ค. OP๊ฐ ๋ง๋ค์ด์ง ์ง 2.5๋ ์ด ๋ ์ด๋๋ก Angular์ ํ๊ฒฝ์ ๋ง์ด ๋ฐ๋์๊ณ Angular Material 2๋ Angular Components๊ฐ ๋์์ต๋๋ค.
๋ค๋ฅธ ์ฃผ์๊ฐ๋ค์ด ์ง์ ํ ๋ฐ์ ๊ฐ์ด Angular Material์ DOM์ ์ฌ์ฉํ๋ฏ๋ก ์์ฉ ํ๋ก๊ทธ๋จ ๊ตฌ์ฑ ์์๊ฐ ๋๋ฆฌ๊ฒ ๋ก๋๋ ํ์ ๋ชจ๋์ entryComponents์ ์ ์ธ๋ ๊ฒฝ์ฐ์๋ MatDialog์์ ์์ฉ ํ๋ก๊ทธ๋จ ๊ตฌ์ฑ ์์๋ฅผ ๋ก๋ํ ๋ ์์ฉ ํ๋ก๊ทธ๋จ ๊ฐ๋ฐ์๊ฐ DOM์ ์ฌ์ฉํ ํ์๊ฐ ์์ต๋๋ค.
๊ทธ๋ฌ๋ ๋น์ฆ๋์ค ์ ํ๋ฆฌ์ผ์ด์ ์์ ๋์ผํ DOM ์์ ์ ์ํํ๋ ๊ฒ์ Angular์ ์ ํ๋ฆฌ์ผ์ด์ ํ๋ก๊ทธ๋๋ฐ ์ง์นจ์ ๋ฐํด ์ด์ํด ๋ณด์ ๋๋ค.
ComponentFactoryResolver
๊ฐ ์ง์ฐ ๋ก๋๋ ํ์ ๋ชจ๋์ ์ ์ธ๋ ํญ๋ชฉ ๊ตฌ์ฑ ์์๋ฅผ ์ด๋ค ์์ผ๋ก๋ "๋ณผ" ์ ์๋๋ก OP์ ๊ธฐ๋ฅ ์์ฒญ์ ๊ตฌํํ๋ฉด ์ข์ ๊ฒ์
๋๋ค. app.module์์ ์ง์ฐ ๋ชจ๋์ ํญ๋ชฉ ๊ตฌ์ฑ ์์๋ฅผ ์ ์ธํ๋ ๊ฒ์ ์๋ํ์ง๋ง ๋ณด๊ธฐ ํํฉ๋๋ค.
์์์ ์ธ๊ธํ ๋๋ฌ์ด ์์ ์๋ฃจ์ : 1. DOM, 2. app.module์์ ๊ตฌ์ฑ ์์ ์ ์ธ, ์๋์์ ๋ ๋๋ฌ์ด ๋ค๋ฅธ ์๋ฃจ์ ์ ์ ๊ณตํฉ๋๋ค.
์ธ ๋ฒ์งธ ์๋ฃจ์ , ๋ ๋๋ฝ์ต๋๋ค.
์ง์ฐ ๋ก๋๋ ์ฌ๋ฌ ๋ชจ๋์์ ์ฌ์ฉํ๋ ๊ณต์ ์๋น์ค๊ฐ ์์ต๋๋ค.
export interface DataComponent {
data: any;
}
/**
* Display an NG component in a dialog, and this dialog has not need to answer but close.
*/
@Injectable()
export class DataComponentDialogService {
modalRef: MatDialogRef<DataComponentDialog>;
constructor(private dialog: MatDialog) { }
open(title: string, externalComponentType: Type<DataComponent>, componentData: any, autofocus = true): Observable<any> {
const isSmallScreen = window.innerWidth < 640 || window.innerHeight < 640;
this.modalRef = this.dialog.open(DataComponentDialog,
{
disableClose: true,
minWidth: isSmallScreen ? '98vw' : undefined,
autoFocus: autofocus,
data: {
title: title,
externalComponentType: externalComponentType,
componentData: componentData,
isSmallScreen: isSmallScreen
}
});
return this.modalRef.afterClosed();
}
}
์ด ์๋น์ค๋ ์ง์ฐ ๋ก๋๋์ง ์์ ๋ชจ๋์ ํญ๋ชฉ ๊ตฌ์ฑ ์์๋ฅผ ๋ณผ ์ ์์ต๋๋ค. ์ง์ฐ ๋ก๋๋ ์ผ๋ถ ๋ชจ๋์ ๊ตฌํ๋ ์ผ๋ถ ํญ๋ชฉ ๊ตฌ์ฑ ์์๊ฐ ์์ต๋๋ค. ๊ทธ๋์ ๊ฐ ์ง์ฐ ๋ชจ๋์์ ์ ๊ณตํ ์ด ์๋น์ค์ ํด๋์ค๋ฅผ ํ์ํ์ต๋๋ค.
import { DataComponentDialogService } from '../_ui_services/dataComponentDialog.component';
@Injectable()
export class LazyComponentDialogService extends DataComponentDialogService {
constructor(dialog: MatDialog) {
super(dialog);
}
}
LazyComponentDialogService๋ lazy entry ์ปดํฌ๋ํธ์ ๋์ผํ lazy ๋ชจ๋์์ ์ ๊ณต๋๊ธฐ ๋๋ฌธ์ ์ปดํฌ๋ํธ๋ฅผ ๋ณผ ์ ์๋ค.
์ฑ ์ํคํ ์ฒ์ ๋ฐ๋ผ ํ์ ํด๋์ค๊ฐ ํ์ํ์ง ์์ ์ ์์ง๋ง ๊ฐ ๋ชจ๋์์ DataComponentDialogService ๋๋ ์ด์ ์ ์ฌํ ๊ฒ์ ์ ๊ณตํ๋ฉด ๋ฉ๋๋ค.
์์ด๋น ๊ตฌํ(9.0.0-next.7์์ ํ ์คํธ)์ผ๋ก ์ธํด Angular 9์๋ ๋ ์ด์ ๋ฌธ์ ๊ฐ ์กด์ฌํ์ง ์๋ ๊ฒ ๊ฐ์ต๋๋ค. ์ง์ฐ ๋ชจ๋ ์ธ๋ถ์์ ์ง์ฐ ๋ก๋๋ ๋ชจ๋์ ๊ตฌ์ฑ ์์๋ฅผ ๋์ ์ผ๋ก ๋ง๋ค ์ ์์ต๋๋ค. ์์ด๋น์์ ๋์ ์ธ ์ปดํฌ๋ํธ์ ๋ํด์๋ ์ํธ๋ฆฌ ์ปดํฌ๋ํธ ์ ์ธ์ด ๋ ์ด์ ํ์ํ์ง ์์ ๊ฒฝ์ฐ๋ก ๋ณด์ ๋๋ค. Ivy๋ ์ปดํ์ผ๋ฌ์ ์ํด ์์ฑ๋์๋ ๋ถ๋ฆฌ๋ ์ปดํฌ๋ํธ ํฉํ ๋ฆฌ๊ฐ ํ์ํ์ง ์์ต๋๋ค. ๊ตฌ์ฑ ์์ ๊ณต์ฅ ์ ๋ณด๋ Ivy(?)๋ฅผ ์ฌ์ฉํ์ฌ ๊ตฌ์ฑ ์์ ์ ์์ ํฌํจ๋ฉ๋๋ค. ๊ตฌ์ฑ ์์ ์ ํ์ด ์์ผ๋ฉด ๊ตฌ์ฑ ์์ ํฉํ ๋ฆฌ๋ฅผ ๊ฐ์ ธ์จ ๋ค์ ์ธ์คํด์ค๋ฅผ ์์ฑํ ์ ์์ด์ผ ํฉ๋๋ค. ๊ทธ๊ฒ์ ๋จ์ง ๋ด ์ธ์์ ๋๋ค. ํ ์์ ์๊ธ์ผ๋ก ๋ด ๋ง์ ๋ฐ์
๋ชจ๋ ๋์ ๊ตฌ์ฑ ์์๋ฅผ ์ ์ธํ๊ณ ์ด๋ฅผ entryComponent๋ก ํฌํจํ๋ ๋ณ๋์ ์์ ฏ ๋ชจ๋์ ์์ฑํ๊ธฐ๋ง ํ๋ฉด ๋ฉ๋๋ค. ๊ทธ๋ฐ ๋ค์ AppModule์์ ์ด ๋ชจ๋์ ๊ฐ์ ธ์ต๋๋ค. ์ด๋ ๊ฒ ํ๋ฉด ์ ๋ ฅ ๊ตฌ์ฑ ์์๊ฐ ๋ฃจํธ ์ธ์ ํฐ์ ๋ฑ๋ก๋์๋์ง ํ์ธํ ์ ์์ต๋๋ค.
์ด ์๋ฃจ์ ์ ๊นจ๋ํ๊ณ Lazy ์บก์ํ๋ฅผ ์ค๋จํ์ง ์์ต๋๋ค.
@NgModule({
declarations: [
ModalComponent,
... more dynamic components ...
],
entryComponents: [
ModalComponent,
... more dynamic components ...
]
})
export class DynamicModule {
}
์ฑ ๋ชจ๋:
@NgModule({
declarations: [
AppComponent
],
imports: [
BrowserModule,
AppRoutingModule,
BrowserAnimationsModule,
DynamicModule
],
providers: [],
bootstrap: [
AppComponent
]
})
export class AppModule { }
@pixelbits-mk ๊ทธ๋ฌ๋ฉด ๋ชจ๋ ๋์ ๊ตฌ์ฑ ์์(์: ๋ชจ๋ฌ ๋ฑ)๊ฐ ์ฆ์ ๋ก๋ ๋์ง๋ง ์ฌ๊ธฐ์ ์์ ์ ์ฑ์ ์ด๊ธฐ ๋ก๋ ์๋๋ฅผ ๋ฆ์ถ์ง ์๋๋ก (ํนํ ํ๋ก์ ํธ๊ฐ ์ฑ์ฅํจ์ ๋ฐ๋ผ) ์ง์ฐ ๋ก๋ํ ์ ์๋๋ก ํ๋ ๊ฒ์ ๋๋ค. ), ์ด๋ฅผ ํธ์ถํ๋ ๊ตฌ์ฑ ์์์ ๊ฐ์ต๋๋ค.
์ง์ฐ ๋ก๋๋ ComponentA
์ด ์ง์ฐ ๋ก๋๋ ๋์ ComponentB
์ ๋์ ์ผ๋ก ํธ์ถํ๋ค๊ณ ๊ฐ์ ํด ๋ณด๊ฒ ์ต๋๋ค. ComponentA
๊ฐ ComponentB
๋ฅผ ํธ์ถํ๋ค๋ ๊ฒ์ ์๊ณ ์์ผ๋ฉด ComponentAModule
ComponentBModule
๋ฅผ ๊ฐ์ ธ์ฌ ์ ์์ต๋๋ค. ์ด๊ฒ์ ์ด์์ ์ธ ๊ฒฝ์ฐ๋ ์๋์ง๋ง, ๋๋ ๊ทธ๊ฒ์ผ๋ก ์ด ์ ์์ต๋๋ค.
์ฃผ์ ๋ฌธ์ ๋ ComponentA
๊ฐ ComponentB
๊ฐ ๋ก๋๋ ๊ฒ์ด๋ผ๋ ๊ฒ์ ๋ชจ๋ฅผ ๋ ๋ฐ์ํฉ๋๋ค. ์๋ฅผ ๋ค์ด ComponentB
๊ฐ ๋ชจ๋ฌ์ด๊ณ ComponentA
๊ฐ ์๋น์ค์์ showModal()
๋ฉ์๋๋ฅผ ํธ์ถํ๊ณ ์๋น์ค๊ฐ ๋ฐฑ๊ทธ๋ผ์ด๋์์ ComponentB
๋ฅผ ๋์ ์ผ๋ก ๋ก๋ํ๋ ๊ฒฝ์ฐ ComponentA
๋ ๊ทธ๊ฒ์ ๋ชจ๋ฅผ ๊ฒ์
๋๋ค.
์ง๊ธ ๋ด๊ฐ ํ๋ ์ผ์ ComponentB
showModal(ComponentB)
ComponentA
๊ฐ ComponentB
๋ก๋ ๋์ง๋ง ๋ด ์๊ฒฌ์ผ๋ก๋ ์ข์ง ์์ต๋๋ค (๋ฉ์๋์ ๋ํ ๊ฐ ํธ์ถ์ ๊ตฌ์ฑ ์์ ์ฐธ์กฐ๋ฅผ ์ ๋ฌํด์ผ ํจ).
๋ชจ๋์ ๋ช
์์ ์ผ๋ก ๊ฐ์ ธ์ฌ ํ์ ์์ด ํญ๋ชฉ ๊ตฌ์ฑ ์์๋ฅผ ์ฌ์ฉํ ์ ์๋ค๋ฉด(์ด ๋ฌธ์ ๊ฐ ๋ฌด์์ธ์ง ์๊ฐํฉ๋๋ค) ๋ฌธ์ ๊ฐ ํด๊ฒฐ๋ ๊ฒ์
๋๋ค(Angular๋ ComponentA
๊ฐ ComponentB
์ ๊ฐ์ ธ์ค๋ ์๋น์ค๋ฅผ ํธ์ถํ๋ ๊ฒ์ ๊ฐ์งํ๋ ๋ฐฉ๋ฒ์ด ์์ ์ ์์ต๋๋ค. ComponentAModule
๊ฐ์ ธ์ค๊ธฐ๋ฅผ ์์์ ์ผ๋ก ComponentBModule
ํ๊ณ ์ฐธ์กฐ๋ฅผ ๊ฐ์ ธ์จ ๊ตฌ์ฑ ์์์ ๋ชจ๋์ ๊ฐ์ ธ์ค๋ ๋ฐ๋์งํ์ง ์์ ์ํฅ์ ๋ฏธ์น ์ ์์ง๋ง ๊ฐ์ ธ์ค๊ธฐ์ ์ด์ ๋ ๋์ ์ผ๋ก ์์ฑํ์ง ์๊ธฐ ์ํ ๊ฒ์
๋๋ค. , ๋๋ ์ด๊ฒ์ ๊ทน๋จ์ ์ธ ๊ฒฝ์ฐ๋ก ๋ณผ ๊ฒ์ด๊ณ ๊ด์ฐฎ์ ๊ฒ์
๋๋ค).
๋๋ (์์ง) @jonrimmer ์ ์ ์ ํ ์คํธํ์ง ์์์ง๋ง ๊ทธ๊ฒ์ ์ ๋งํด ๋ณด์ ๋๋ค.
ComponentA
๊ฐ ํ์๋๋ ๊ฒฝ์ฐ์๋ง ComponentA
๊ฐ ComponentB
๋ก ๋ชจ๋์ ์๋์ผ๋ก ์ง์ฐ ๋ก๋ํ๋ฉด ๊ต์ฅํ ๊ฒ์
๋๋ค.
ModuleA
์๋ ComponentA1
, ComponentA2
๊ตฌ์ฑ์์๊ฐ ์์ต๋๋ค.
ComponentA1
html ComponentB
์ฐธ์กฐ ( ๋ชจ๋ ModuleB
), ํ์ง๋ง ComponentA2
๋ ๊ทธ๋ ์ง ์์ต๋๋ค.
ModuleB
๋ ComponentA1
๊ฐ ํ์๋๋ ๊ฒฝ์ฐ์๋ง ์๋์ผ๋ก ์ง์ฐ ๋ก๋๋์ง๋ง ComponentA2
๊ฐ ํ์๋๋ ๊ฒฝ์ฐ์๋ ๊ทธ๋ ์ง ์์ต๋๋ค.
์ด๊ฒ์ ํ ํ์ด์ง์ ๋์๋ณด๋ ์๋๋ฆฌ์ค์ ์๋ฒฝํ๊ฒ ๋ง์ต๋๋ค. Dahsboard๋ ๋ค์ํ ๋ชจ๋์์ ์ ๊ณต๋์ง๋ง ๋์๋ณด๋๊ฐ โโํฌํจ๋์ง ์์ ๊ฒฝ์ฐ(์: ์ฌ์ฉ์ ์ค์ ์ ํตํด) ํด๋น ๋ชจ๋์ ๋ค์ด๋ก๋ํ ํ์๊ฐ ์์ต๋๋ค.
@jonrimmer ๋ฉ์ธ ComponentFactoryResolver์ resolveComponentFactory ํจ์๋ฅผ ๋ํํ๊ณ ์ด์ ์ ๋ฑ๋ก๋ ์ง์ฐ ๋ก๋ ๋ชจ๋์ ๊ฒ์ํ๋ ์ข์ ์์ด๋์ด์ ๋๋ค.
๋ฃจํธ์์ ์ ๊ณต๋๋ ์๋น์ค๋ฅผ ์ฃผ์ ํ๋ ํญ๋ชฉ ๊ตฌ์ฑ ์์์ ํจ๊ป ์๋ํฉ๋๋ค. ํ์ง๋ง ์ง์ฐ ๋ก๋ ๋ชจ๋์์๋ง ์ ๊ณต๋๋ ์๋น์ค๋ ์ฃผ์ ํ ์ ์์ต๋๋ค. ์ด ๊ฒฝ์ฐ StaticInjectorError๊ฐ ๋ฐ์ํ์ต๋๋ค. ํ์ธํด ์ฃผ์๊ฒ ์ต๋๊น?
ivy๊ฐ ์ด ๋ฌธ์ ๋ฅผ ์ฒ๋ฆฌํ๋ ๊ฒ ๊ฐ๋ค๋ ๊ฒ์ ํ์ธํ๊ณ ์ถ์์ต๋๋ค. ๋๋ ์ค๋ ์ด ๋ฌธ์ ๋ฅผ ์ฐ์ฐํ ๋ฐ๊ฒฌํ๋๋ฐ, ์ฌ๊ธฐ์๋ ํญ๋ชฉ ๊ตฌ์ฑ ์์์ ๊ณต์ ์๋น์ค๋ฅผ ํฌํจํ๋ ์ง์ฐ ๋ก๋๋ก ๋ชจ๋์ ๋ค์ ์์ฑํ์ฌ ๊ตฌ์ฑ ์์ ํฉํ ๋ฆฌ๋ฅผ ์ฐพ์ ์ ์๋ค๋ ์๋ก์ด ์ค๋ฅ๋ฅผ ์ฐพ๊ธฐ ์ํด์๋ง ์ธ์คํด์คํํ ์ ์์ต๋๋ค. ์ฌ์ค ์ฒ์์ ์ด๊ฒ ๋ฌธ์ ์๋ค๋ ๊ฑธ ์ฝ๊ณ ์ถฉ๊ฒฉ์ ๋ฐ์์ต๋๋ค.
์ด์จ๋ ๊ธฐ์กด Angular 8.2 ํ๋ก์ ํธ์์ ivy๋ฅผ ํ์ฑํํ๊ณ ๋ชจ๋ ์์๋๋ก ์๋ํ๋ ๊ฒ ๊ฐ์ต๋๋ค.
์ง์ฐ ๋ก๋ ๋ชจ๋์์ Overlay
์ฌ์ฉ์ ์ํ ๋ด ์๋ฃจ์
:
๋ค์๊ณผ ๊ฐ์ด ComponentPortal
์ ์์ฑ์์๊ฒ ํฉํ ๋ฆฌ ๋ฆฌ์กธ๋ฒ๋ฅผ ์ ๋ฌํด์ผ ํฉ๋๋ค.
@Injectable()
export class OverlayService {
constructor(
private overlay: Overlay,
private componentFactoryResolver: ComponentFactoryResolver
) {
}
open<T>(component: ComponentType<T>) {
const overlayRef = this.overlay.create();
const filePreviewPortal = new ComponentPortal(component, null, null, this.componentFactoryResolver);
overlayRef.attach(filePreviewPortal);
}
}
๊ทธ๋ฌ๋ ์ง์ฐ ๋ก๋๋ ๋ชจ๋์์๋ OverlayService
๋ฅผ ์ ๊ณตํด์ผ ํฉ๋๋ค. OverlayService๋ OverlayService๊ฐ ์ ๊ณต๋๋ ๋ชจ๋์ ComponentFactoryResolver
๋ฅผ ์ฃผ์
ํด์ผ ํฉ๋๋ค.
@NgModule({
declarations: [
SomeComponent,
],
entryComponents: [
SomeComponent, // <-------- will be opened via this.overlayService.open(SomeComponent)
],
providers: [
OverlayService, // <--------
]
})
export class LazyLoadedModule {}
@mixrich ์ด ์ ๊ทผ ๋ฐฉ์์ [๋ด ์ฑ์์ ๊ทธ๋ ๊ฒ ํ๊ณ ์์ผ๋ฉฐ ๋ก๊ทธ์์๊ณผ ๊ฐ์ ์ผ๋ถ ์ด๋ฒคํธ์์ ๋ชจ๋ ๋ชจ๋ฌ ๋ํ ์์๋ฅผ ๋ซ์์ผ ํ ๋๊น์ง ์ ์๋ํฉ๋๋ค.] ์ด ๋ชจ๋์ ๊ฐ์ ธ์ต๋๋ค. ์ฆ, ์ฑ์๋ ์์ ์๋น์ค์ ๊ฐ์ ๋จ์ผ ์ธ์คํด์ค๊ฐ ์์ต๋๋ค.
์ค๋ฒ๋ ์ด ์๋น์ค์ ์ ํธ ํค์ด ์๊ธฐ ๋๋ฌธ์ ์ผ๋ง๋ ๋ง์ ๋ชจ๋ฌ ๋ํ ์์๊ฐ ์ด๋ ค ์๋์ง ์๊ธฐ๊ฐ ์ฝ์ง ์์ต๋๋ค.
๋ฌผ๋ก ์ฑ์ ์๊ตฌ ์ฌํญ์ด์ง๋ง ์ด ์ ๊ทผ ๋ฐฉ์์ ์ฃผ์ํ ๋ฟ์ ๋๋ค.
@mixrich ์ด ์ ๊ทผ ๋ฐฉ์์ [๋ด ์ฑ์์ ๊ทธ๋ ๊ฒ ํ๊ณ ์์ผ๋ฉฐ ๋ก๊ทธ์์๊ณผ ๊ฐ์ ์ผ๋ถ ์ด๋ฒคํธ์์ ๋ชจ๋ ๋ชจ๋ฌ ๋ํ ์์๋ฅผ ๋ซ์์ผ ํ ๋๊น์ง ์ ์๋ํฉ๋๋ค.] ์ด ๋ชจ๋์ ๊ฐ์ ธ์ต๋๋ค. ์ฆ, ์ฑ์๋ ์์ ์๋น์ค์ ๊ฐ์ ๋จ์ผ ์ธ์คํด์ค๊ฐ ์์ต๋๋ค.
์ค๋ฒ๋ ์ด ์๋น์ค์ ์ ํธ ํค์ด ์๊ธฐ ๋๋ฌธ์ ์ผ๋ง๋ ๋ง์ ๋ชจ๋ฌ ๋ํ ์์๊ฐ ์ด๋ ค ์๋์ง ์๊ธฐ๊ฐ ์ฝ์ง ์์ต๋๋ค.
๋ฌผ๋ก ์ฑ์ ์๊ตฌ ์ฌํญ์ด์ง๋ง ์ด ์ ๊ทผ ๋ฐฉ์์ ์ฃผ์ํ ๋ฟ์ ๋๋ค.
์๋ฅผ ๋ค์ด, ์ ๊ฒฝ์ฐ์๋ OverlayService๊ฐ ์ ๊ณต๋ OverlayService
ModalDialogModule
์ด ์๊ณ OverlayService.open(SomeComponent)
๋ฅผ ํธ์ถํ๋ฉด ๋ชจ๋ฌ ์ฐฝ ํ
ํ๋ฆฟ๋ ์์ฑ๋๊ณ ๋ด๋ถ์ SomeComponent
๊ฐ ์ฝ์
๋ฉ๋๋ค. ์ ์ฉํ ๊ด์ฐฐ ๊ฐ๋ฅ ํญ๋ชฉ(๋ซ๊ธฐ ๋ฐ ์ฑ๊ณต ์ด๋ฒคํธ์ ๊ฒฝ์ฐ), ๊ตฌ์ฑ ์์ ์ธ์คํด์ค ๋ฐ ์๋ close
๋ฉ์๋๋ฅผ ์ฌ์ฉํ์ฌ ์ผ๋ถ ๋ฐ์ดํฐ ๊ตฌ์กฐ๋ฅผ ๋ฐํํฉ๋๋ค.
๋ฐ๋ผ์ LazyModule์์ ๋ชจ๋ฌ์ ์ฌ์ฉํด์ผ ํ ๋ OverlayService
๋ฅผ ์ฌ์ฉํ ์ ์๋๋ก ModalDialogModule
๋ฅผ ๊ฐ์ ธ์ค๊ธฐ๋ง ํ๋ฉด ๋ฉ๋๋ค. ๋ชจ๋ฌ์ ์ฌ์ฉํ๋ ค๋ฉด ModalDialogModule
๋ฅผ ๊ฐ์ ธ์์ผ ํ๋ค๋ ๊ฒ์ ํญ์ ์๊ณ ์๊ธฐ ๋๋ฌธ์ ์ด ์ ๊ทผ ๋ฐฉ์์ด ํธ๋ฆฌํ๋ค๋ ๊ฒ์ ์์์ต๋๋ค. ๋ฐ์ ์์์ ์ฌ์ฉํ๋ ค๋ฉด ReactiveFormModule
๋ฅผ ๊ฐ์ ธ์์ผ ํ๋ค๋ ๊ฒ์ ํญ์ ์๊ณ ์๋ ๊ฒ์ฒ๋ผ
Angular 8.3.6์์ ์๋ํฉ๋๋ค. ๋ผ์ด๋ธ๋ฌ๋ฆฌ ๋ชจ๋ ํญ๋ชฉ ๊ตฌ์ฑ ์์์ ์ถ๊ฐํ๋ฉด ๋ก๋๋์ง ์๋ ํญ๋ชฉ ๊ตฌ์ฑ ์์(๋งคํธ ๋ํ ์์)๊ฐ ์๋ ๋ผ์ด๋ธ๋ฌ๋ฆฌ ๋ชจ๋์ด ์์ต๋๋ค. ์ด๋ ค๊ณ ํ์ ๋ ์ฝ์ ๋ก๊ทธ์์ MyCustomDialog์ ๋ํ ๊ตฌ์ฑ ์์ ํฉํ ๋ฆฌ๋ฅผ ์ฐพ์ ์ ์๋ค๊ณ ๋์ต๋๋ค.
์๋ฃจ์ ์ ๋ผ์ด๋ธ๋ฌ๋ฆฌ ๋ชจ๋์ NgModule ํด๋์ค์์ ๋ชจ๋ ๋ชจ๋์ ํญ๋ชฉ ๊ตฌ์ฑ ์์์ ๋ฐฐ์ด์ ๋ฐํํ๋ ์ ์ ๋ฉ์๋๋ฅผ ๋ง๋๋ ๊ฒ์ ๋๋ค. ๊ทธ๋ฐ ๋ค์ ์ฑ ๋ชจ๋ NgModule์์ ์ด ๋ฉ์๋๋ฅผ ํธ์ถํฉ๋๋ค.
@NgModule({
declarations: [
MyCustomDialogComponent
],
imports: [
CommonModule
],
exports: [
MyCustomDialogComponent
]
// no need to add the dialog component to entryComponents here.
})
export class MyCustomModule {
static getEntryComponents() {
const entryComponents = [];
entryComponents.push(MyCustomDialogComponent);
return entryComponents;
}
}
import {MyCustomModule} from 'my-custom-library'; // import library from node_modules
@NgModule({
declarations: [
AppComponent
],
imports: [
BrowserModule,
AppRoutingModule,
BrowserAnimationsModule,
MyCustomModule
],
providers: [],
bootstrap: [AppComponent],
entryComponents: [].concat(MyCustomModule.getEntryComponents())
})
export class AppModule {
}
๋๋ ์ด๊ฒ์ด ์ฌ์ ํ ์๋ํ์ง ์๋ ๋ผ์ด๋ธ๋ฌ๋ฆฌ์ entryComponents๋ฅผ ์์ ํ์ง ์๋๋ค๋ ๊ฒ์ ์๊ณ ์์ง๋ง ์ ์ด๋ ์ ์ ๋ฉ์๋๋ฅผ ์ฌ์ฉํ๋ฉด ์ฑ ๋ชจ๋์ ์ฌ์ฉ์ ์ ์ ๋ผ์ด๋ธ๋ฌ๋ฆฌ์ ํญ๋ชฉ ๊ตฌ์ฑ ์์์ ๋ํด ์ ํ์๊ฐ ์์ต๋๋ค. ๊ทธ๊ฒ์ ๋จ์ง ๊ทธ ๋ฉ์๋๋ฅผ ํธ์ถํ๊ณ ๋ผ์ด๋ธ๋ฌ๋ฆฌ์ ๋์ด๋ ๊ฒ์ ๊ฐ์ ธ์ต๋๋ค. ์ฑ์ ๋ผ์ด๋ธ๋ฌ๋ฆฌ์ ์ด๋ค ๊ตฌ์ฑ ์์๊ฐ ํญ๋ชฉ ๊ตฌ์ฑ ์์์ธ์ง ์ ์ฑ ์์ด ์์ต๋๋ค.
@asmith2306 ์ ์คํ๋ ๋ ์ฐ์ฐ์๋ฅผ ์ฌ์ฉํ์ง ์์ต๋๊น? ๋ชป์๊ฒผ์ด
@asmith2306 ์ ์คํ๋ ๋ ์ฐ์ฐ์๋ฅผ ์ฌ์ฉํ์ง ์์ต๋๊น? ๋ชป์๊ฒผ์ด
์๋ํ์ง ์๊ธฐ ๋๋ฌธ์ ๋๋ค. ์ด๋๊ฐ์ ๋ฒ๊ทธ๊ฐ ์์ต๋๋ค.
--prod ๋ชจ๋์์ ์๋ํ์ง ์๋ ๊ฒ ๊ฐ์ต๋๋ค.
์๋ํ๋๋ผ๋ ์ค์ ๋ฌธ์ ๋ฅผ ํด๊ฒฐํ์ง ์์ต๋๋ค. ๋ฌธ์ ๋ NgModule์ entryComponents์ ๊ตฌ์ฑ ์์๋ฅผ ์ถ๊ฐํ๊ณ ํด๋น ๋ชจ๋์ ๋ด ์ฑ์์ ๋๋ฆฌ๊ฒ ๋ก๋๋ ๋ชจ๋๋ก ๊ฐ์ ธ์ค๋ฉด entryComponents๊ฐ ์ฑ ํญ๋ชฉ ๊ตฌ์ฑ ์์์ ์๋์ผ๋ก ๋ฑ๋ก๋์ด์ผ ํ๋ค๋ ๊ฒ์ ๋๋ค. ๋ ์ด์์ ์์ ์ด ํ์ํ์ง ์์์ผ ํฉ๋๋ค. NgModule์ entryComponents์ ์๋ ๋ชจ๋ ๊ตฌ์ฑ ์์๋ ์ฑ ๋ชจ๋์ entryComponents์ ์๋์ผ๋ก ํฌํจ๋์ด์ผ ํฉ๋๋ค. ํ์ฌ๋ ๊ทธ๋ ์ง ์์ต๋๋ค. ๊ทธ๋ ๋ค๋ฉด ์ฐ๋ฆฌ๋ ์ฌ๊ธฐ์ ์์ง ์์์ ๊ฒ์ ๋๋ค.
@broweratcognitecdotcom ๋ค์ํ ์ฃผ์ ์์ค์ ์ถฉ๋ ๋ฐ ์ฐ์ ์์๋ ์ด๋ป์ต๋๊น?
@mlc-mlapis ์ฃผ๋ฌธ์ ์ ์ฐฉ์์ผ๋ก ์ด๋ฃจ์ด์ ธ์ผ ํ๋ค๊ณ ์๊ฐํฉ๋๋ค.
@mlc-mlapis ๋ค๋ฅธ ๊ตฌ์ฑ ์์์ ์ด๋ฆ์ด ๊ฐ์ ๊ฒฝ์ฐ๋ฅผ ์๋ฏธํฉ๋๊น? ์ฒซ ๋ฒ์งธ ์ผ์น ํญ๋ชฉ ๋๋ ์ผ์น ํญ๋ชฉ์ด ์ฌ๋ฌ ๊ฐ์ธ ๊ฒฝ์ฐ ์ค๋ฅ์ ๋๋ค. ํญ๋ชฉ ๊ตฌ์ฑ ์์ ์ด๋ฆ์ด ๊ณ ์ ํด์ผ ํ๋ค๋ ์๊ตฌ ์ฌํญ์ ๋ฐ๋ผ ์ํํ ์ ์์ต๋๋ค.
@broweratcognitecdotcom ๋ค ์๋ ํ์ธ์. ์ผ๋ฐ ์์น์ ๋ํด ์ด์ผ๊ธฐํ๋ ค๋ฉด ๋ชจ๋ ๋ณํ์ ๋ค๋ฃจ์ด์ผ ํ๊ธฐ ๋๋ฌธ์ ๋๋ค.
@mlc-mlapis ๋ง์ ์ฌ๋๋ค์ด ๊ทน๋ณตํ๊ณ ์ถ์ดํ๋ ๊ฐ๋์ ํ๊ณ๋ ํญ๋ชฉ ๊ตฌ์ฑ ์์๊ฐ ์ธ์คํด์คํ๋๊ณ ์ฑ์ ์ํด์๋ง dom์ ๋ฐฐ์น๋ ์ ์๋ค๋ ๊ฒ์ ๋๋ค. ์ฑ์ ๋ด๊ฐ ์ง์ฐ ๋ก๋๋ ๋ชจ๋๋ก ๊ฐ์ ธ์ค๋ ๋ชจ๋์ด ์ฌ์ฉํ๋ ๋ํ ์์์ ๋ํด ์ ํ์๊ฐ ์์ต๋๋ค. ํญ๋ชฉ ๊ตฌ์ฑ ์์์ ํ์ฌ ๊ฐ๋ ์ ๊ฐ๋์ ๋ชจ๋์ ํจํด์ ๊นจ๋จ๋ฆฝ๋๋ค.
Ivy entryComponents๊ฐ ์๋ AFAIK๋ ๋ ์ด์ ํ์ํ์ง ์์ต๋๋ค. ๋ง์ฃ ? ๊ตฌ์ฑ ์์์๋ ์ง์ญ์ฑ์ด ์์ผ๋ฏ๋ก ๋์ ์ผ๋ก ๊ฐ์ ธ์ค๊ธฐ๋ง ํ๋ฉด ํญ์ ์๋ํฉ๋๊น?
@Airblader ๋น์ ์ด ์ณ์ต๋๋ค. ์ฐ๋ฆฌ๋ ์๊ณ ์์ต๋๋ค. ๊ทธ๊ฒ์ ์ญ์ฌ์ ๋ํ ์์ ํ์์ด์์ต๋๋ค. ๐
์ฌ์ ํ ์ด ๋ฌธ์ ๋ก ์ด๋ ค์์ ๊ฒช๊ณ ์๋ ์ฌ๋๋ค์ ์ํด ์ค์ ๋ก ์๋ํ๋ ์๋ฃจ์ ์ด ์์ต๋๋ค. https://github.com/angular/angular/issues/14324#issuecomment -481898762
์ด ๋ฌธ์ ์ ๋ํ ํจํค์ง๋ฅผ ๊ฒ์ํฉ๋๋ค. jonrimmer์ ์ผ๋ถ ์ฝ๋ ์ฌ๋ณธ.
Angular 9์๋ ๋ฌธ์ ๊ฐ ์๋ ๊ฒ ๊ฐ์ต๋๋ค. ํ์ํ ๊ฒฝ์ฐ ์ง๊ธ ์์ ํ์ธ์. @aiao/lazy-component
๋ฅผ ์ฌ์ฉํ ์ ์์ต๋๋ค.
์ฌ๊ธฐ์ ์ํ ์ฝ๋
https://github.com/aiao-io/aiao/tree/master/integration/lazy-component
๋๋ ์ด๊ฒ์ด ์์ด๋น๋ฅผ ์คํํ๋ Angular v9์์ ์์ ๋์๋ค๊ณ ์๊ฐํฉ๋๋ค. ์ฌ์ ํ ์ ์ฌํ ๋ฌธ์ ์ ์ง๋ฉดํ๋ ์ฌ๋์ด ์์ผ๋ฉด ์ต์ํ์ ์ฌํ ์๋๋ฆฌ์ค๋ก ์ ๋ฌธ์ ๋ฅผ ์ฌ์ญ์์ค. ์ฝํฌ์ค!
์ด ๋ฌธ์ ๋ ํ๋์ด ์์ด ์๋์ผ๋ก ์ ๊ฒผ์ต๋๋ค.
์ ์ฌํ๊ฑฐ๋ ๊ด๋ จ๋ ๋ฌธ์ ๊ฐ ๋ฐ์ํ๋ฉด ์ ๋ฌธ์ ๋ฅผ ์ ์ถํ์ญ์์ค.
์๋ ๋ํ ์ ๊ธ ์ ์ฑ ์ ๋ํด ์์ธํ ์์๋ณด์ธ์.
_์ด ์์ ์ ๋ด์ ์ํด ์๋์ผ๋ก ์ํ๋์์ต๋๋ค._
๊ฐ์ฅ ์ ์ฉํ ๋๊ธ
์๊ฒ ์ต๋๋ค. ์ด ๊ฒฝ์ฐ ์ด ๋ฌธ์ ๋ฅผ ๊ธฐ๋ฅ ์์ฒญ์ผ๋ก ๋ณ๊ฒฝํ๊ฒ ์ต๋๋ค.
IMO์์๋ ์ด๋ฌํ ์ฌ์ฉ ์ฌ๋ก๋ฅผ DOM์ ๋ฐ๋ผ ์๋์ผ๋ก ์ด๋ํ๋ ๋์ Angular ์์ฒด์์ ์ฒ๋ฆฌํด์ผ ํฉ๋๋ค.