์ ์ถํฉ๋๋ค...
๊ธฐ๋ฅ ์ ์์ฒญํ๊ฑฐ๋ ๋ฒ๊ทธ ๋ฅผ ๋ณด๊ณ ํ์๊ฒ ์ต๋๊น?
0์ด ์๋ ๋ค๋ฅธ ์ธ๋ฑ์ค์์ ๋๋๊ทธ ์คํฌ๋กค์ ์์ํ ์ ์์ต๋๊น?
ํ์ฌ ํ๋์ ๋ฌด์์
๋๊น?
ํ์ฌ ๋๋๊ทธ ์คํฌ๋กค์ ๊ธฐ๋ณธ์ ์ผ๋ก ์ฒซ ๋ฒ์งธ ์ด๋ฏธ์ง/๊ตฌ์ฑ ์์์์ ์์๋ฉ๋๋ค.
ํ๋์ ๋ฐ๊พธ๋ ๋๊ธฐ/์ฌ์ฉ ์ฌ๋ก๋ ๋ฌด์์
๋๊น?
๋๋๊ทธ ์คํฌ๋กค์ ์์ํ ์ธ๋ฑ์ค๋ฅผ ์ ํํ๋ ๊ฒ์ด ์ข์ต๋๋ค(๋ก๋ ์).
์ค์ ๋ก `ngAfterViewInit ๋ธ๋ก์์ moveTo
๋ฅผ ํธ์ถํ์ฌ ์ด๊ธฐ ์ธ๋ฑ์ค๋ฅผ ์ค์ ํ ์ ์์ต๋๋ค. setTimeout(() => {...}, 0)์ผ๋ก ํธ์ถ์ ๋ํํด์ผ ํฉ๋๋ค.
์๋ฅผ ๋ค์ด:
@ViewChild('nav', {read: DragScrollComponent}) ds: DragScrollComponent;
ngAfterViewInit() {
setTimeout(() => {
this.ds.moveTo(3);
}, 0);
}
์ด๋ฏธ ์๋ํ์ต๋๋ค .. ๋ถํํ๋ ๋งค๋ฒ์ด ์ค๋ฅ๊ฐ ๋ฐ์ํฉ๋๋ค.
core.js:1673 ERROR TypeError: Cannot read property '_elementRef' of undefined
at DragScrollComponent.push../node_modules/ngx-drag-scroll/lib/ngx-drag-scroll.js.DragScrollComponent.maximumIndex (ngx-drag-scroll.js:415)
at DragScrollComponent.push../node_modules/ngx-drag-scroll/lib/ngx-drag-scroll.js.DragScrollComponent.moveTo (ngx-drag-scroll.js:214)
at myComponent.component.ts:141
at ZoneDelegate.push../node_modules/zone.js/dist/zone.js.ZoneDelegate.invokeTask (zone.js:421)
at Object.onInvokeTask (core.js:3815)
at ZoneDelegate.push../node_modules/zone.js/dist/zone.js.ZoneDelegate.invokeTask (zone.js:420)
at Zone.push../node_modules/zone.js/dist/zone.js.Zone.runTask (zone.js:188)
at push../node_modules/zone.js/dist/zone.js.ZoneTask.invokeTask (zone.js:496)
at ZoneTask.invoke (zone.js:485)
at timer (zone.js:2054)
ngAfterViewInit
๋ธ๋ก ๋ด์์ $ moveTo
๋ฅผ ํธ์ถํ๊ณ ์์ต๋๊น? moveTo
๋ฉ์๋๋ ๋๋๊ทธ ์คํฌ๋กค ์บ๋ฌ์
์์๊ฐ ํ์ด์ง์ ํ์๋ ๋ ํธ์ถํ ์ ์์ต๋๋ค.
๋๋ ์ฝ๊ฐ์ ์ง์ฐ์ ์ถ๊ฐํ๊ณ ๋ค์๊ณผ ๊ฐ์ด ๋ค์ ์๋ํ ์ ์์ต๋๋ค.
setTimeout(() => {
this.ds.moveTo(3);
}, 1000);
์๋ํ๋์ง ์๋ ค์ฃผ์ธ์.
๋๋ ํ๋ค. ๊ทธ๋ฌ๋ 1์ด์ ์๊ฐ ์ด๊ณผ๋ก ์ด์ ์๋ํฉ๋๋ค! ์ฝ๊ฐ ํดํน์ด์ง๋ง ๋์์ ์ฃผ์ ์ ๊ฐ์ฌํฉ๋๋ค.
ํ , ์ฝ๋๋ฅผ ๋ณด์ง ์๊ณ ๋ ngAfterViewInit
์ด ์ ๋น์ ์๊ฒ ํจ๊ณผ๊ฐ ์๋์ง ์ ์ ์์ต๋๋ค. ์ง๊ธ์ ์ด ๊ธฐ๋ฅ์ด ์ฌ๋ผ์ง ๋๊น์ง setTimeout
๋ฅผ ์ฌ์ฉํ์ธ์. PR ํ์ํฉ๋๋ค.
์ค์ ๋ก `ngAfterViewInit ๋ธ๋ก์์
moveTo
๋ฅผ ํธ์ถํ์ฌ ์ด๊ธฐ ์ธ๋ฑ์ค๋ฅผ ์ค์ ํ ์ ์์ต๋๋ค. setTimeout(() => {...}, 0)์ผ๋ก ํธ์ถ์ ๋ํํด์ผ ํฉ๋๋ค.
์๋ฅผ ๋ค์ด:@ViewChild('nav', {read: DragScrollComponent}) ds: DragScrollComponent; ngAfterViewInit() { setTimeout(() => { this.ds.moveTo(3); }, 0); }
์๋ ํ์ธ์ ๋ฌธ์์ ์ด ๊ธฐ๋ฅ์ ์ถ๊ฐํ์ญ์์ค.
์ด ์๋ฃจ์
์ ์ฌ์ฉํ๋ฉด ์ฒซ ๋ฒ์งธ ํญ๋ชฉ์ด ํน์ ํญ๋ชฉ์ผ๋ก ์ด๋ํ๋ ์ ๋๋ฉ์ด์
์ด ์ฌ์ฉ์์๊ฒ ํ์๋ฉ๋๋ค.
์๋์ ์ ์ํฉ๋๋ค. ๊ด์ฐฎ๋ค๊ณ ์๊ฐํ์๋์?
ngAfterViewInit(){
setTimeout(()=>{
this.ds.currIndex = 3;
this.ds._elementRef.nativeElement.children[0].scrollLeft = someValue;
}, 10);
}
๊ฐ์ฅ ์ ์ฉํ ๋๊ธ
์ค์ ๋ก `ngAfterViewInit ๋ธ๋ก์์
moveTo
๋ฅผ ํธ์ถํ์ฌ ์ด๊ธฐ ์ธ๋ฑ์ค๋ฅผ ์ค์ ํ ์ ์์ต๋๋ค. setTimeout(() => {...}, 0)์ผ๋ก ํธ์ถ์ ๋ํํด์ผ ํฉ๋๋ค.์๋ฅผ ๋ค์ด: