Hacker News 크롤링 EP3 (페이징 구현 및 라우터)

GoGoDev·2021년 9월 7일
0

News 웹페이지

목록 보기
3/4

router 사용

function router() {
  //location.hash를 통해 지금 보고있는 화면의 위치 해시값을 받아 목록을 보여줄지 내용을 보여줄지 정한다.
  const routePath = location.hash;

  if (routePath === "") {
    newsFeed(); //routePath 값에 #만 있는 경우 빈 문자열을 출력한다.
  } else if (routePath.indexOf("#/page/") >= 0) {
    store.currentPage = Number(routePath.substr(7));
    newsFeed();
  } else {
    newsDetail();
  }
}

window.addEventListener("hashchange", router); //hash값을 받아 알맞는 라우터를 찾고 보여줄 화면을 지정한다

router();

라우터를 사용하여 이전 EP에서의 화면 전환이 이루어지지 않았던 문제점을 해결한다.

profile
🐣차근차근 무럭무럭🐣

0개의 댓글