์ฌ๋ณด์ธ์!
Ionic 2 ํ๋ก์ ํธ์์ Angular 2๋ก ์์
์ค์ด๊ณ LazyLoad๋ฅผ ๊ฐ์ ธ์ค๋ ค๊ณ ํ์ง๋ง ์ปดํ์ผ ์ค๋ฅ๊ฐ ๋ฐ์ํฉ๋๋ค.
my_file.js
import {Component, Input} from "angular2/core";
import {LazyLoadImageDirective} from "ng2-lazyload-image/index";
@Component({
selector: 'image',
template: `
<img src="images/placeholder_big" [lazyLoad]="image" offset="50">
`,
directives: [LazyLoadImageDirective]
})
class ImageComponent {
@Input('src') image;
}
export {ImageComponent}
/Users/lobetia/repo/ionic2_migration/node_modules/ng2-lazyload-image/index.ts:1
import {LazyLoadImageDirective} from './src/lazyload-image.directive';
^
ParseError: 'import' and 'export' may appear only with 'sourceType: module'
/index๋ฅผ ์ฐ์ง ์๊ณ ๋ชจ๋์ ํฌํจํ๋ ค๊ณ ํฉ๋๋ค.
import {LazyLoadImageDirective} from "ng2-lazyload-image";
๋น์ทํ ์ค๋ฅ๊ฐ ๋ฐ์ํฉ๋๋ค
/Users/lobetia/repo/ionic2_migration/node_modules/ng2-lazyload-image/src/lazyload-image.directive.ts:1
import 'rxjs/add/observable/fromEvent';
^
ParseError: 'import' and 'export' may appear only with 'sourceType: module'
์ด ๋ฌธ์ ๋ฅผ ํด๊ฒฐํ๋ ๋ฐฉ๋ฒ์ ๋ชจ๋ฅด๊ฒ ๊ณ ์ด ๋ชจ๋๊ณผ ๊ด๋ จ๋ ๋ฌธ์ ์ธ์ง ์ ์ ์์ต๋๋ค.
๋์์ฃผ์๊ฒ ์ด์ ๐ฐ ? ๊ฐ์ฌํฉ๋๋ค
์๋ ํ์ธ์,
๋น์ ์ ์ค์ ์ ๋ฌด์์
๋๊น? babel ๋๋ typescript์ ํจ๊ป Ionic์ ์ฌ์ฉํ๊ณ ์์ต๋๊น?
typescript๋ฅผ ์ฌ์ฉํ๋ ๊ฒฝ์ฐ tsconfig.json
ํ์ผ์ ์ ๊ณตํ ์ ์์ต๋๊น?
ํ์ฌ ๋
ธ๋ 5.4์์ [email protected] ๋ฅผ ์ฌ์ฉ
๋๋ typescript๋ฅผ ์ฌ์ฉํ๊ณ ๋ด tsconfig.json์
{
"compilerOptions": {
"target": "es5",
"module": "commonjs",
"emitDecoratorMetadata": true,
"experimentalDecorators": true
},
"filesGlob": [
"**/*.ts",
"!node_modules/**/*"
],
"exclude": [
"node_modules",
"typings/main",
"typings/main.d.ts"
],
"compileOnSave": false,
"atom": {
"rewriteTsconfig": false
}
}
์ด์ ๋ ๋ชจ๋ฅด๊ฒ ์ง๋ง ์ด๋ค ์ด์ ๋ก typescript๋ ์ปดํ์ผ๋ .js
ํ์ผ์ ๊ฐ์ ธ์ค๋ ๋์ ์์ค ํ์ผ์ ๋ค์ ์ปดํ์ผํ๋ ค๊ณ ํฉ๋๋ค.
๊ทธ๋ฌ๋ ๋ด ์๋ฃจ์
์ npm์์ ์์ค ํ์ผ์ ์ ์ธํ๋ ๊ฒ์ด ์์ต๋๋ค.
๋ฐ๋ผ์ ๋ค์์ ์ฌ์ฉํ์ฌ ์ ํ๋ก์ ํธ๋ฅผ ๋ง๋๋ ๊ฒฝ์ฐ:
$ ionic start MyIonic2Project tutorial --v2 --ts
๊ทธ๋ฐ ๋ค์ /app/pages/hello-ionic/hello-ionic.ts
๋ฅผ ๋ค์๊ณผ ๊ฐ์ด ๋ณ๊ฒฝํฉ๋๋ค.
import { Page } from 'ionic-angular';
import { LazyLoadImageDirective } from 'ng2-lazyload-image';
@Page({
templateUrl: 'build/pages/hello-ionic/hello-ionic.html',
directives: [ LazyLoadImageDirective ]
})
export class HelloIonicPage {
defaultImage: string;
image: string;
offset: number;
constructor() {
this.defaultImage = 'https://www.placecage.com/1000/500';
this.image = 'https://images.unsplash.com/photo-1443890923422-7819ed4101c0?fm=jpg';
this.offset = 100;
}
}
/app/pages/hello-ionic/hello-ionic.html
:
<ion-navbar *navbar>
<button menuToggle>
<ion-icon name="menu"></ion-icon>
</button>
<ion-title>Hello Ionic</ion-title>
</ion-navbar>
<ion-content padding class="getting-started" #container>
<p style="height: 1000px;">
This starter project comes with simple tabs-based layout for apps
that are going to primarily use a Tabbed UI.
</p>
<img
[src]="defaultImage"
[lazyLoad]="image"
[offset]="offset"
[scrollTarget]="container._scrollEle">
</ion-content>
์ด์ [email protected]
์ ์๋ํ๋ ๊ฒ ๊ฐ์ต๋๋ค.
๋น์ ์ด ๊ทธ๊ฒ์ ๋๋ฆด ์ ์์ต๋๊น?
๊ทธ๋ฆฌ๊ณ ๋ ๋ค๋ฅธ ๊ฒ์ ์ง์๋ฌธ์ด ๋ฌ๋ฆฌ ๋งํ์ง ์์ผ๋ฉด window
์ ๋ํ ์คํฌ๋กค ์ด๋ฒคํธ๋ฅผ ์์ ํ๋ค๋ ๊ฒ์
๋๋ค. ์คํฌ๋กค์ ์ฒ๋ฆฌํ๋ ๋ด๋ถ div์ด๊ธฐ ๋๋ฌธ์ ionic
์ฑ์ ๋ํด ์ํํด์ผ ํ๋ ์์
์
๋๋ค.
์ฆ
<ion-content #container>
<img [scrollTarget]="container._scrollEle" ...>
</ion-content>
์์ ์์ # 2๋ฅผ ์ฐธ์กฐํ์ญ์์ค.
๋งค๋ ฅ์ฒ๋ผ ์๋ํฉ๋๋ค, ๋น์ ์ ๊ต์ฅํฉ๋๋ค!
img-lazy ๊ตฌ์ฑ ์์์ ๋ชจ๋ ๊ฒ์ ํฌ์ฅํ์ผ๋ฉฐ ์ด๋ฏธ์ง์ ํ์ด๋์ธ ์ ๋๋ฉ์ด์
๊ณผ ์ผ๋ถ ์คํ๋ผ์ธ ์บ์ฑ ๊ธฐ๋ฅ์ ๋ํด ์ฝ๊ฐ์ ์์
์ ํ ๊ฒ์
๋๋ค.
๋ด ์๊ฐ์ ์ด ๋ฌธ์ ๋ ์๋ฒฝํ๊ฒ ๋ง๊ฐ๋์์ต๋๋ค ๐ ๐
๋คํ์ด๋ค!
์ด๋ฏธ์ง์ ํ์ด๋์ธ์ ์ ๋๋ฉ์ด์ ์ผ๋ก ๋ง๋๋ ์์ ์ ์กฐ๊ธ ํ ๊ฒ์ ๋๋ค.
๊ทธ๋ฅ ๋น์ ์ด ์๊ณ ์์ต๋๋ค. ng2-lazyloading
ํด๋์ค๋ฅผ ์ถ๊ฐํ๋ฉด ์ด๋ฏธ์ง๊ฐ ๋ก๋๋ ํ ์๋์ผ๋ก ์ ๊ฑฐ๋๊ณ ng2-lazyloaded
ํด๋์ค๊ฐ ์ถ๊ฐ๋ฉ๋๋ค.
์ฆ
// Before loaded
<img [lazyLoad]="image" class="ng2-lazyloading">
// After loaded
<img [lazyLoad]="image" class="ng2-lazyloaded">
https://github.com/tjoskar/ng2-lazyload-image/blob/a227020c52af0fc30b6683e05c4f4a57f67b13ce/example/image.component.ts#L14 ๋ฐ ๋ฌผ๋ก ์์ค ์ฝ๋: https://github.com /tjoskar/ng2-lazyload-image/blob/8e8f7d46ebad227c4584594b32d0ac9b1945a74e/src/lazyload-image.directive.ts#L77
๋ฌธ์ ๊ฐ ๋ฐ์ํ๋ฉด ์๋ ค์ฃผ์ญ์์ค.
์คํ๋ผ์ธ ์บ์ฑ ๊ธฐ๋ฅ์ด ์์ ์ ์์ต๋๋ค.
๋ฉ์ง ๊ฒ์ ๋ง๋ค๋ฉด ์๋ ค์ฃผ์ธ์ ;)
๊ฐ์ฅ ์ ์ฉํ ๋๊ธ
๊ทธ๋ฆฌ๊ณ ๋ ๋ค๋ฅธ ๊ฒ์ ์ง์๋ฌธ์ด ๋ฌ๋ฆฌ ๋งํ์ง ์์ผ๋ฉด
window
์ ๋ํ ์คํฌ๋กค ์ด๋ฒคํธ๋ฅผ ์์ ํ๋ค๋ ๊ฒ์ ๋๋ค. ์คํฌ๋กค์ ์ฒ๋ฆฌํ๋ ๋ด๋ถ div์ด๊ธฐ ๋๋ฌธ์ionic
์ฑ์ ๋ํด ์ํํด์ผ ํ๋ ์์ ์ ๋๋ค.์ฆ
์์ ์์ # 2๋ฅผ ์ฐธ์กฐํ์ญ์์ค.