Skip to content

Commit

Permalink
feat: 초기 진입 경로가 해시 형태일 경우 해시라우터 쓰도록 변경
Browse files Browse the repository at this point in the history
  • Loading branch information
junman95 committed Dec 19, 2024
1 parent 009c47d commit 2a649f5
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 1 deletion.
1 change: 1 addition & 0 deletions src/main.hash.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import "./main.js";
import router from "./network/router/Router.js";

// 해당 파일을 통해 진입하는 경우 무조건 해시 라우터를 사용하도록 설정
router.toggleHash(true);
12 changes: 11 additions & 1 deletion src/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
});
4 changes: 4 additions & 0 deletions src/network/router/Router.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ class Router {
this.routes[path] = handler;
};

// 해시 라우팅 on off 함수
toggleHash = (fixed) => {
this.isHash = fixed;
if (this.isHash) {
Expand All @@ -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}`);
Expand Down Expand Up @@ -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;
Expand Down

0 comments on commit 2a649f5

Please sign in to comment.