'Info'에 해당되는 글 13건

  1. 2023.03.15 AWS EC2 - Golang 세팅
  2. 2023.03.15 AWS & VS Code 연동
  3. 2023.03.15 AWS EC2 - 인스턴스 생성 및 Putty 연결
  4. 2023.03.15 AWS RDS - MySQL 구축
  5. 2023.02.21 Svelte.js 메모_Event/Binding

AWS EC2 - Golang 세팅

Info/AWS 2023. 3. 15. 23:13 |
728x90

※ 해당 글은 작성자 참고용으로 작성된 글입니다.

 

※ 인스턴스 생성 후 가능합니다.

https://lazeturtle.tistory.com/34

 

AWS EC2 - 인스턴스 생성 및 Putty 연결

※ 해당 글은 작성자 참고용으로 작성된 글입니다. 1. 인스턴스 구축 대시보드 - '인스턴스 시작' 버튼 선택 이름 : 인스턴스 이름(프로젝트명) OS : Linux 키 페어 : IAM을 통해 미리 등록한 사용자 키

lazeturtle.tistory.com

※ VS Code를 연동 후 하는 것을 추천드립니다.

https://lazeturtle.tistory.com/36

 

AWS & VS Code 연동

※ 해당 글은 작성자 참고용으로 작성된 글 입니다. 인스턴스 생성 후 진행 가능 https://lazeturtle.tistory.com/34 AWS EC2 - 인스턴스 생성 및 Putty 연결 ※ 해당 글은 작성자 참고용으로 작성된 글입니다.

lazeturtle.tistory.com


1. EC2 내 Golang 기본 세팅

1) root 계정으로 변경

sudo -s

2) 업데이트

yum update -y

3) Golang 설치

sudo yum install -y golang

4) go 환경 변수 설정

export GOROOT=/usr/lib/golang
export GOPATH=$HOME/(작업 공간)
export PATH=$PATH:$GOROOT/bin

(환경변수 지정 전에 작업 공간에 대한 폴더를 미리 만들어 두세요)

(Linux 폴더 생성 명령어 : mkdir (폴더명))

 

5) go 버전 확인

go version

위 이미지처럼 나온다면 설치 성공

 

2. Golang 테스트

1) main.go 파일 생성

(별도 디렉토리에서 생성하는 것을 추천드려요 - 디렉토리 생성 명령어 : mkdir (디렉토리명))

(파일 및 디렉토리 생성시, root 권한으로 할 경우, 나중에 번거롭습니다.)

touch main.go

 

2) AWS 연동된 VS Code에서 main.go 열기 및 코드 작성

package main

import (
	"fmt"
	"log"
	"net/http"
)

func HomeEndpoint(w http.ResponseWriter, r *http.Request) {
	fmt.Fprintln(w, "Hello world & Hello lazeturtle :)")
}

func main() {
	http.HandleFunc("/", HomeEndpoint)
	if err := http.ListenAndServe(":3000", nil); err != nil {
		log.Fatal(err)
	}
}

 

3) main.go 파일 실행

go run main.go

 

4) 페이지 테스트

URL : (EC2 인스턴스 퍼블릭 IPv4 DNS):3000


실행이 안될 경우,

원인 1. URL에 포트 번호 미입력

URL에 퍼블릭 IPv4 DNS 만 입력 하고 뒤에 :3000 (Port)를 입력하지 않을 경우,

페이지가 보여지지 않을 수 있음

 

원인 2. 인바운드 규칙을 설정하지 않은 경우,

해당 코드는 Port(:3000) 에 대한 페이지를 보여주는데, 인바운드 규칙에 그런 부분이 없을 경우,

페이지가 보여지지 않을 수 있음

그렇기에 인바운드 규칙을 별도로 추가해야만 함!

 

728x90

'Info > AWS' 카테고리의 다른 글

AWS & VS Code 연동  (0) 2023.03.15
AWS EC2 - 인스턴스 생성 및 Putty 연결  (0) 2023.03.15
AWS RDS - MySQL 구축  (0) 2023.03.15
Posted by 게으른거북
:

AWS & VS Code 연동

Info/AWS 2023. 3. 15. 22:35 |
728x90

※ 해당 글은 작성자 참고용으로 작성된 글 입니다.

 

인스턴스 생성 후 진행 가능

https://lazeturtle.tistory.com/34

 

AWS EC2 - 인스턴스 생성 및 Putty 연결

※ 해당 글은 작성자 참고용으로 작성된 글입니다. 1. 인스턴스 구축 대시보드 - '인스턴스 시작' 버튼 선택 이름 : 인스턴스 이름(프로젝트명) OS : Linux 키 페어 : IAM을 통해 미리 등록한 사용자 키

lazeturtle.tistory.com

위 진행을 통해 다운 받은 키페어(.pem)을 C:\Users\(User이름)\.ssh에 복붙


AWS & VS Code 연동

1) VS Code 프로그램 열기

2) Ctrl + Shift + X 를 통해 'Remote - SSH' 검색

3) 'Remote - SSH' 다운로드

4) 좌측 아이콘 중 Remote Explorer 선택 -> 톱니모양 버튼(Open SSH Config File) 선택

5) 첫번째 줄(\.ssh\config) 선택

 

6) 내용 입력

Host aws
  HostName (EC2 인스턴스 퍼블릭 IPv4 주소 또는 IPv4 DNS)
  User (사용자 이름 - 초기 상태인 경우, 'ec2-user')
  IdentityFile ~/.ssh/(키페어 파일명).pem
  ForwardAgent yes

 

7) 연결(Connect in Current Window...) --> 연결 성공시, 초록색 아이콘 표시 

728x90

'Info > AWS' 카테고리의 다른 글

AWS EC2 - Golang 세팅  (0) 2023.03.15
AWS EC2 - 인스턴스 생성 및 Putty 연결  (0) 2023.03.15
AWS RDS - MySQL 구축  (0) 2023.03.15
Posted by 게으른거북
:
728x90

※ 해당 글은 작성자 참고용으로 작성된 글입니다.

 

1. 인스턴스 구축

대시보드 - '인스턴스 시작' 버튼 선택

 

이름 : 인스턴스 이름(프로젝트명)

OS : Linux

키 페어 : IAM을 통해 미리 등록한 사용자 키 페어

 

그 외에 기본 설정 그대로 진행 및 '인스턴스 시작' 버튼 선택

 

인스턴스 상태가 '대기 중'에서 '실행 중'으로 바뀔 때까지 기다림

상태가 변경된 경우, 새로 생성된 '인스턴스 ID' 선택

 

생성 완료된 인스턴스 확인

 

728x90

 

2. AWS & Putty 연동

Putty 및 Puttygen 다운 / AWS IAM pem 미리 다운로드 후 진행

Puttygen 실행 - 'Load' 버튼 선택

 

1) 파일 확장자를 All Files(*.*)로 변경

2) 미리 다운 받은 .pem(키 페어) 열기

 

정상적으로 열릴 경우, 뜨는 창

 

사용할 암호 입력 후 'Save private key' 버튼 선택

(별도의 디렉터리에 저장)

 

(인스턴스 세부 정보를 같이 띄워놓고 하시는 것을 추천드려요)

1) Host Name : 인스턴스 퍼블릭 IPv4 주소 또는 DNS

2) Port : 보안 그룹을 통해 지정한 포트 번호 입력

3) SSH 지정

 

1) 우측 메뉴 Connection > Auth > Credentials 선택

2) Private key file for authentication > Browse. 선택

3) Puttygen 저장한 .ppk 파일 등록

4) 우측 하단 'Open' 버튼 선택

 

'Accept' 버튼 선택

 

login as : 인스턴스 사용자 이름(초기 상태인 경우 'ec2-user' 입력

imported-openssh-key : Puttygen에서 입력한 비밀번호 입력

728x90

'Info > AWS' 카테고리의 다른 글

AWS EC2 - Golang 세팅  (0) 2023.03.15
AWS & VS Code 연동  (0) 2023.03.15
AWS RDS - MySQL 구축  (0) 2023.03.15
Posted by 게으른거북
:

AWS RDS - MySQL 구축

Info/AWS 2023. 3. 15. 16:13 |
728x90

※ 해당 글은 본인 참고용으로 작성한 글입니다.

 

1. 데이터베이스 생성

'데이터베이스 생성' 클릭

 

- 엔진 유형 : MySQL

- 엔진 버전 : (원하는 버전 선택)

- 템플릿 : 프리 티어

 

DB 인스턴스 식별자 : RDS Database 이름

마스터 사용자 이름 : (마스터 로그인 ID)

마스터 암호 : (마스터 비밀번호)

 

할당된 스토리지 : (저장소 용량)

스토리지 자동 조정 : (체크 해제)

 

컴퓨팅 리소스 : 연결 안함(차후에도 수정 가능)

네트워크 유형 : IPv4

퍼블릭 엑세스 : 예

VP 보안 그룹 : (사용자에 맞추어 지정)

추가 구성 - 데이터베이스 포트 : (사용자에 맞춰 지정)

 

데이터베이스 인증 옵션 : 암호 인증(차후 HeidiSQL에서 로그인을 위해)

 

최하단 추가설정 조정

초기 데이터베이스 이름 : (필요없을 경우, 미입력)

백업 : 체크 해제

유지 관리 : 체크 해제

 

모든 설정 지정 후 '데이터베이스 생성' 버튼 클릭

 

상태가 현재 '생성 중' 이며 이것이 '사용 가능'으로 변경되어야 이용이 가능함

(약 5~10분 소요)

 

 

728x90

 

2. 데이터베이스 조정

DB 식별자를 선택하여 대시보드로 이동

1) 엔드 포인트 확인(메모장 또는 별도 복붙)

2) '보안 그룹' 선택

 

우측 하단 '인바운드 규칙 편집' 버튼 선택

 

사용자 IP 등록 및 '규칙 저장' 버튼 선택

 

3. HeidiSQL 로그인

호스트명/IP : RDS 엔드 포인트

사용자 : 마스터 사용자 이름

암호 : 마스터 암호

위 내용 입력 후 '열기'

 

성공!

728x90

'Info > AWS' 카테고리의 다른 글

AWS EC2 - Golang 세팅  (0) 2023.03.15
AWS & VS Code 연동  (0) 2023.03.15
AWS EC2 - 인스턴스 생성 및 Putty 연결  (0) 2023.03.15
Posted by 게으른거북
:
728x90

1. Mouse 이벤트

1단계 - 마우스 위치 좌표 표시

<script>
	let m = { x: 0, y: 0 };

	function handleMousemove(event) {
		m.x = event.clientX;
		m.y = event.clientY;
	}
</script>

<div on:mousemove={handleMousemove}>
	The mouse position is {m.x} x {m.y}
</div>

<style>
	div { width: 100%; height: 100%; }
</style>

2단계 - Event Handlers Inline

<script>
	let m = { x: 0, y: 0 };
</script>

<div on:mousemove={e => m = { x: e.clientX, y: e.clientY }}>
	The mouse position is {m.x} x {m.y}
</div>

<style>
	div { width: 100%; height: 100%; }
</style>

 

2. Click 이벤트

(한 번이라도 클릭한 경우, 더 이상 Alert가 표시되지 않음)

<script>
	function handleClick() {
		alert('no more alerts')
	}
</script>

<button on:click|once={handleClick}>
	Click me
</button>

1. preventDefault - calls event.preventDefault() before running the handler. Useful for client-side form handling, for example. (Handler를 실행하기 전에 preventDefault()를 진행. 클라이언트 측 양식 처리에 유용)
2. stopPropagation - calls event.stopPropagation(), preventing the event reaching the next element. (event가 다음 element에 도달하는 것을 방지)
3. passive - improves scrolling performance on touch/wheel events (Svelte will add it automatically where it's safe to do so) (터치/휠 이벤트에서 스크롤 성능 향상(Svelte가 안전한 곳에 자동으로 추가))

4. nonpassive - explicitly set passive: false (명시적으로 설정된 패시브: false)
5. capture - fires the handler during the capture phase instead of the bubbling phase (MDN docs) (버블링 단계 대신 캡처 단계에서 처리기를 실행)
6. once - remove the handler after the first time it runs (핸들러를 처음 실행한 후 제거)
7. self - only trigger handler if event.target is the element itself (event.target이 요소 자체인 경우에만 트리거 핸들러)
8. trusted - only trigger handler if event.isTrusted is true. I.e. if the event is triggered by a user action. (event.isTrusted가 true인 경우에만 트리거 핸들러. 즉, 사용자 작업에 의해 이벤트가 트리거된 경우.)

ex)  on:click|once|capture={...}

 

3. Dispatcher

1단계

App.svelte

<script>
	import Inner from './Inner.svelte';

	function handleMessage(event) {
		alert(event.detail.text);
	}
</script>

<Inner on:messageTurtle={handleMessage}/>

Inner.svelte

<script>
	import { createEventDispatcher } from 'svelte';

	const dispatch = createEventDispatcher();

	function sayHello() {
		dispatch('messageTurtle', {
			text: 'Hello!'
		});
	}
</script>

<button on:click={sayHello}>
	Click to say hello
</button>

 

2단계

App.svelte

<script>
	import Outer from './Outer.svelte';

	function handleMessage(event) {
		alert(event.detail.text);
	}
</script>

<Outer on:messageTurtle={handleMessage}/>

Outer.svelte

<script>
	import Inner from './Inner.svelte';
</script>

<Inner on:messageTurtle/>

Inner.svelte

<script>
	import { createEventDispatcher } from 'svelte';

	const dispatch = createEventDispatcher();

	function sayHello() {
		dispatch('messageTurtle', {
			text: 'Hello!'
		});
	}
</script>

<button on:click={sayHello}>
	Click to say hello
</button>

 

3. Event Forwarding

App.svelte

<script>
	import CustomButton from './CustomButton.svelte';

	function handleClick() {
		alert('Button Clicked');
	}
</script>

<CustomButton on:click={handleClick}/>

<button on:click={handleClick}> Click me </button>

CustomButton.svelte

<button on:click>
	Click me
</button>

<style>
	button {
		background: #E2E8F0;
		color: #64748B;
		border: unset;
		border-radius: 6px;
		padding: .75rem 1.5rem;
		cursor: pointer;
	}
	button:hover {
		background: #CBD5E1;
		color: #475569;
	}
	button:focus {
		background: #94A3B8;
		color: #F1F5F9;
	}
</style>

 

728x90

 

4. Input Text Binding

(Input box에 입력한 값을 자동으로 갱신하여 표시)

<script>
	let name = 'world';
</script>

<input bind:value={name}>

<h1>Hello {name}!</h1>

 

5. Input Number Binding

(input 타입 중 number, range에 대해 데이터 연결)

<script>
	let a = 1;
	let b = 2;
</script>

<label>
	<input type=number bind:value={a} min=0 max=10>
	<input type=range bind:value={a} min=0 max=10>
</label>

<label>
	<input type=number bind:value={b} min=0 max=10>
	<input type=range bind:value={b} min=0 max=10>
</label>

<p>{a} + {b} = {a + b}</p>

<style>
	label { display: flex }
	input, p { margin: 6px }
</style>

 

6. Input Checkbox Binding

<script>
	let yes = false;
</script>

<label>
	<input type=checkbox bind:checked={yes}>
	Yes! Send me regular email spam
</label>

{#if yes}
	<p>Thank you. We will bombard your inbox and sell your personal details.</p>
{:else}
	<p>You must opt-in to continue. If you're not paying, you're the product.</p>
{/if}

<button disabled={!yes}>
	Subscribe
</button>

 

7. Input Grouping

<script>
	let scoops = 1;
	let flavours = ['Mint choc chip'];

	let menu = [
		'Cookies and cream',
		'Mint choc chip',
		'Raspberry ripple'
	];

	function join(flavours) {
		if (flavours.length === 1) return flavours[0];
		return `${flavours.slice(0, -1).join(', ')} and ${flavours[flavours.length - 1]}`;
	}
</script>

<h2>Size</h2>

<label>
	<input type=radio bind:group={scoops} name="scoops" value={1}>
	One scoop
</label>

<label>
	<input type=radio bind:group={scoops} name="scoops" value={2}>
	Two scoops
</label>

<label>
	<input type=radio bind:group={scoops} name="scoops" value={3}>
	Three scoops
</label>

<h2>Flavours</h2>

{#each menu as flavour}
	<label>
		<input type=checkbox bind:group={flavours} name="flavours" value={flavour}>
		{flavour}
	</label>
{/each}

{#if flavours.length === 0}
	<p>Please select at least one flavour</p>
{:else if flavours.length > scoops}
	<p>Can't order more flavours than scoops!</p>
{:else}
	<p>
		You ordered {scoops} {scoops === 1 ? 'scoop' : 'scoops'}
		of {join(flavours)}
	</p>
{/if}

 

8. Inpute Textarea Binding

<script>
	import { marked } from 'marked';
	let value = `Some words are *italic*, some are **bold**`;
</script>

{@html marked(value)}

<textarea bind:value></textarea>

<style>
	textarea { width: 100%; height: 200px; }
</style>

 

9. Input Select Binding

<script>
	let questions = [
		{ id: 1, text: `Where did you go to school?` },
		{ id: 2, text: `What is your mother's name?` },
		{ id: 3, text: `What is another personal fact that an attacker could easily find with Google?` }
	];

	let selected;

	let answer = '';

	function handleSubmit() {
		alert(`answered question ${selected.id} (${selected.text}) with "${answer}"`);
	}
</script>

<h2>Insecurity questions</h2>

<form on:submit|preventDefault={handleSubmit}>
	<select bind:value={selected} on:change="{() => answer = ''}">
		{#each questions as question}
			<option value={question}>
				{question.text}
			</option>
		{/each}
	</select>

	<input bind:value={answer}>

	<button disabled={!answer} type=submit>
		Submit
	</button>
</form>

<p>selected question {selected ? selected.id : '[waiting...]'}</p>

<style>
	input {
		display: block;
		width: 500px;
		max-width: 100%;
	}
</style>

 

10. Select Multiple

(Checkbox 방식을 Select에서도 적용)

<script>
	let scoops = 1;
	let flavours = ['Mint choc chip'];

	let menu = [
		'Cookies and cream',
		'Mint choc chip',
		'Raspberry ripple'
	];

	function join(flavours) {
		if (flavours.length === 1) return flavours[0];
		return `${flavours.slice(0, -1).join(', ')} and ${flavours[flavours.length - 1]}`;
	}
</script>

<h2>Size</h2>

<label>
	<input type=radio bind:group={scoops} value={1}>
	One scoop
</label>

<label>
	<input type=radio bind:group={scoops} value={2}>
	Two scoops
</label>

<label>
	<input type=radio bind:group={scoops} value={3}>
	Three scoops
</label>

<h2>Flavours</h2>

<select multiple bind:value={flavours}>
	{#each menu as flavour}
		<option value={flavour}>
			{flavour}
		</option>
	{/each}
</select>

{#if flavours.length === 0}
	<p>Please select at least one flavour</p>
{:else if flavours.length > scoops}
	<p>Can't order more flavours than scoops!</p>
{:else}
	<p>
		You ordered {scoops} {scoops === 1 ? 'scoop' : 'scoops'}
		of {join(flavours)}
	</p>
{/if}

 

11. Contenteditable Binding

<script>
	let html = '<p>Write some text!</p>';
</script>

<div
	contenteditable="true"
	bind:innerHTML={html}
></div>

<pre>{html}</pre>

<style>
	[contenteditable] {
		padding: 0.5em;
		border: 1px solid #eee;
		border-radius: 4px;
	}
</style>

 

12. Each Block Binding

<script>
	let todos = [
		{ done: false, text: 'finish Svelte tutorial' },
		{ done: false, text: 'build an app' },
		{ done: false, text: 'world domination' }
	];

	function add() {
		todos = todos.concat({ done: false, text: '' });
	}

	function clear() {
		todos = todos.filter(t => !t.done);
	}

	$: remaining = todos.filter(t => !t.done).length;
</script>

<h1>Todos</h1>

{#each todos as todo}
	<div class:done={todo.done}>
		<input
			type=checkbox
			bind:checked={todo.done}
		>

		<input
			placeholder="What needs to be done?"
			bind:value={todo.text}
		>
	</div>
{/each}

<p>{remaining} remaining</p>

<button on:click={add}>
	Add new
</button>

<button on:click={clear}>
	Clear completed
</button>

<style>
	.done {
		opacity: 0.4;
	}
</style>

 

13. Media Elements

(영상에 대한 데이터를 연동하고 추가로 이벤트 진행-영상 상단에 상태값 표시)

<script>
	// These values are bound to properties of the video
	let time = 0;
	let duration;
	let paused = true;

	let showControls = true;
	let showControlsTimeout;

	// Used to track time of last mouse down event
	let lastMouseDown;

	function handleMove(e) {
		// Make the controls visible, but fade out after
		// 2.5 seconds of inactivity
		clearTimeout(showControlsTimeout);
		showControlsTimeout = setTimeout(() => showControls = false, 2500);
		showControls = true;

		if (!duration) return; // video not loaded yet
		if (e.type !== 'touchmove' && !(e.buttons & 1)) return; // mouse not down

		const clientX = e.type === 'touchmove' ? e.touches[0].clientX : e.clientX;
		const { left, right } = this.getBoundingClientRect();
		time = duration * (clientX - left) / (right - left);
	}

	// we can't rely on the built-in click event, because it fires
	// after a drag — we have to listen for clicks ourselves
	function handleMousedown(e) {
		lastMouseDown = new Date();
	}

	function handleMouseup(e) {
		if (new Date() - lastMouseDown < 300) {
			if (paused) e.target.play();
			else e.target.pause();
		}
	}

	function format(seconds) {
		if (isNaN(seconds)) return '...';

		const minutes = Math.floor(seconds / 60);
		seconds = Math.floor(seconds % 60);
		if (seconds < 10) seconds = '0' + seconds;

		return `${minutes}:${seconds}`;
	}
</script>

<h1>Caminandes: Llamigos</h1>
<p>From <a href="https://studio.blender.org/films">Blender Studio</a>. CC-BY</p>

<div>
	<video
		poster="https://sveltejs.github.io/assets/caminandes-llamigos.jpg"
		src="https://sveltejs.github.io/assets/caminandes-llamigos.mp4"
		on:mousemove={handleMove}
		on:touchmove|preventDefault={handleMove}
		on:mousedown={handleMousedown}
		on:mouseup={handleMouseup}
		bind:currentTime={time}
		bind:duration
		bind:paused>
		<track kind="captions">
	</video>

	<div class="controls" style="opacity: {duration && showControls ? 1 : 0}">
		<progress value="{(time / duration) || 0}"/>

		<div class="info">
			<span class="time">{format(time)}</span>
			<span>click anywhere to {paused ? 'play' : 'pause'} / drag to seek</span>
			<span class="time">{format(duration)}</span>
		</div>
	</div>
</div>

<style>
	div {
		position: relative;
	}

	.controls {
		position: absolute;
		top: 0;
		width: 100%;
		transition: opacity 1s;
	}

	.info {
		display: flex;
		width: 100%;
		justify-content: space-between;
	}

	span {
		padding: 0.2em 0.5em;
		color: white;
		text-shadow: 0 0 8px black;
		font-size: 1.4em;
		opacity: 0.7;
	}

	.time {
		width: 3em;
	}

	.time:last-child { text-align: right }

	progress {
		display: block;
		width: 100%;
		height: 10px;
		-webkit-appearance: none;
		appearance: none;
	}

	progress::-webkit-progress-bar {
		background-color: rgba(0,0,0,0.2);
	}

	progress::-webkit-progress-value {
		background-color: rgba(255,255,255,0.6);
	}

	video {
		width: 100%;
	}
</style>

<audio>와 <video>의 데이터 바인딩
1) 읽기 전용
duration - 총 재생 시간(초 단위)
buffered - {start, end}의 객체 배열
seekable - ditto
played - ditto
seeking - boolean 값
ended - boolean 값
2) 양방향
currentTime - 현재 지점의 시간(초 단위)
playbackRate - 재생 속도(보통 - 1)
paused - this one should be self-explanatory
volume - 0과 1 사이의 값 지정
muted - true가 음소거된 boolean 값

 

14. 치수(Dimensions)

(Range를 활용해 글자 크기 조정)

<script>
	let w;
	let h;
	let size = 42;
	let text = 'edit me';
</script>

<input type=range bind:value={size}>
<input bind:value={text}>

<p>size: {w}px x {h}px</p>

<div bind:clientWidth={w} bind:clientHeight={h}>
	<span style="font-size: {size}px">{text}</span>
</div>

<style>
	input { display: block; }
	div { display: inline-block; }
	span { word-break: break-all; }
</style>

연결되는 데이터 및 기능은 읽기 전용으로만 사용됨

 

15. Component Binding

App.svelte

<script>
	import Keypad from './Keypad.svelte';

	let pin;
	$: view = pin ? pin.replace(/\d(?!$)/g, '•') : 'enter your pin';

	function handleSubmit() {
		alert(`submitted ${pin}`);
	}
</script>

<h1 style="color: {pin ? '#333' : '#ccc'}">{view}</h1>

<Keypad bind:value={pin} on:submitTurtle={handleSubmit}/>

Keypad.svelte

<script>
	import { createEventDispatcher } from 'svelte';

	export let value = '';

	const dispatch = createEventDispatcher();

	const select = num => () => value += num;
	const clear  = () => value = '';
	const submitKeypad = () => dispatch('submitTurtle');
</script>

<div class="keypad">
	<button on:click={select(1)}>1</button>
	<button on:click={select(2)}>2</button>
	<button on:click={select(3)}>3</button>
	<button on:click={select(4)}>4</button>
	<button on:click={select(5)}>5</button>
	<button on:click={select(6)}>6</button>
	<button on:click={select(7)}>7</button>
	<button on:click={select(8)}>8</button>
	<button on:click={select(9)}>9</button>

	<button disabled={!value} on:click={clear}>clear</button>
	<button on:click={select(0)}>0</button>
	<button disabled={!value} on:click={submitKeypad}>submit</button>
</div>

<style>
	.keypad {
		display: grid;
		grid-template-columns: repeat(3, 5em);
		grid-template-rows: repeat(4, 3em);
		grid-gap: 0.5em
	}

	button {
		margin: 0
	}
</style>

 

16. Binding to component instances

App.svelte

<script>
	import InputField from './InputField.svelte';

	let field;
</script>

<InputField bind:this={field}/>

<button on:click={() => field.focus()}>Focus field</button>

InputField.svelte

<script>
	let input;

	export function focus() {
		input.focus();
	}
</script>

<input bind:this={input} />

DOM 요소에 바인딩할 수 있는 것처럼 구성 요소 인스턴스 자체에도 바인딩이 가능함

예를 들어, DOM Elements를 바인딩할 때와 동일한 방법으로 <InputField> 인스턴스를 변수 명명 필드에 바인딩할 수 있음

728x90

'Info > Svelte' 카테고리의 다른 글

Svelte.js 메모_Logic(로직)  (0) 2023.02.21
Svelte.js 메모_Basic  (0) 2023.02.20
Posted by 게으른거북
: