From 2a649f58b35493bd1b1e842d823d80e69de7b03b Mon Sep 17 00:00:00 2001 From: JunmanChoi Date: Fri, 20 Dec 2024 03:24:24 +0900 Subject: [PATCH] =?UTF-8?q?feat:=20=EC=B4=88=EA=B8=B0=20=EC=A7=84=EC=9E=85?= =?UTF-8?q?=20=EA=B2=BD=EB=A1=9C=EA=B0=80=20=ED=95=B4=EC=8B=9C=20=ED=98=95?= =?UTF-8?q?=ED=83=9C=EC=9D=BC=20=EA=B2=BD=EC=9A=B0=20=ED=95=B4=EC=8B=9C?= =?UTF-8?q?=EB=9D=BC=EC=9A=B0=ED=84=B0=20=EC=93=B0=EB=8F=84=EB=A1=9D=20?= =?UTF-8?q?=EB=B3=80=EA=B2=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main.hash.js | 1 + src/main.js | 12 +++++++++++- src/network/router/Router.js | 4 ++++ 3 files changed, 16 insertions(+), 1 deletion(-) diff --git a/src/main.hash.js b/src/main.hash.js index 79524db0..1195f387 100644 --- a/src/main.hash.js +++ b/src/main.hash.js @@ -1,4 +1,5 @@ import "./main.js"; import router from "./network/router/Router.js"; +// 해당 파일을 통해 진입하는 경우 무조건 해시 라우터를 사용하도록 설정 router.toggleHash(true); diff --git a/src/main.js b/src/main.js index 9eae5bb8..3b475291 100644 --- a/src/main.js +++ b/src/main.js @@ -10,4 +10,14 @@ const init = () => { }; init(); -window.addEventListener("load", () => router.route(window.location.pathname)); +window.addEventListener("load", () => { + // 진입점이 해시 경로인지 아닌지에 따라 어떠한 경로를 라우팅할지 결정 + // 해시로 진입한 경우에는 해시라우터를 쓴다고 toggle 해 주어야 할까? + window.location.hash + ? router.route(window.location.hash.replace("#", "")) + : router.route(window.location.pathname); + + if (window.location.hash) { + router.toggleHash(true); + } +}); diff --git a/src/network/router/Router.js b/src/network/router/Router.js index 30759bfc..4d6dad6d 100644 --- a/src/network/router/Router.js +++ b/src/network/router/Router.js @@ -51,6 +51,7 @@ class Router { this.routes[path] = handler; }; + // 해시 라우팅 on off 함수 toggleHash = (fixed) => { this.isHash = fixed; if (this.isHash) { @@ -69,6 +70,7 @@ class Router { }; route = (path) => { + console.log(path, "path", this.isHash); const handler = this.routes[path]; if (handler) { log(`Routing to ${path}`); @@ -96,7 +98,9 @@ class Router { let skip = false; if (guards) { for (const guard of guards) { + console.log(guards, "guards"); const redirectPath = guard(); + console.log(redirectPath, "redirectPath"); if (redirectPath) { this.navigate(redirectPath); skip = true;