JS소스모듈

localstorage에 리소스 저장 및 조회

JoyYellow 2022. 11. 9. 14:54
/**
* 로컬 스토리지에서 조회합니다.
*/
loadStorage() {
	// 키(item-title)를 가지고 호출합니다.
	const data = localStorage.getItem('item-title');
	try {
		// 호출된 값은 문자열로 되어있기 때문에 JSON.parse를 해줍니다.
		const playList = JSON.parse(data);
	} catch (e) {
		// 스토리지는 모바일에서 용량 문제 등으로 에러가 발생할 수 있기 때문에
        // try - catch 처리를 잘해줍니다.
		console.error(e);
	}
}

/**
* 로컬 스토리지에 저장합니다.
*/
saveStorage() { 
	try {
		// JSON.stringify 를 통해 문자열로 저장해줍니다.
		localStorage.setItem('item-title', JSON.stringify(item));
	} catch (e) {
		// 스토리지는 모바일에서 용량 문제 등으로 에러가 발생할 수 있기 때문에
        // try - catch 처리를 잘해줍니다.
		console.error(e);
	}
}

sessionstorage와 localstorage의 차이는?

 

메모리와 localstorage의 차이는?