使用手动JavaScript重定向覆盖服务器文件夹重定向

时间:2018-12-23 16:36:42

标签: javascript reactjs

如果我犯了任何菜鸟错误,请忽略,这是我第一次在StackOverflow上发帖。

所以我当时在使用JavaScript重定向我的应用程序内部。我没有为此使用反应重定向库。我正在使用状态更改要显示的当前页面。

我所做的是每当我的应用加载了主要组件时,在componentWillMount函数中,我使用window.location.href来获取用户的URL,基于此,我有不同的切换方式,并相应地更改了状态。在我的本地主机上一切正常。

但是在将构建版本上传到服务器上并使用了手动重定向之后,例如site_name.com/my_redirect,它表明链接已断开。我假设它与服务器在寻找该目录有关。

componentWillMount() {
    let requestedPage = window.location.href;
    this.setRedirects(requestedPage.split('/').pop());
}

// This method updates the url
updateURL(directory = "home") {
    window.history.pushState(null, directory, '/'+directory);
}

// This method updates the state variable to set redirects
setRedirects(requestedPage) {
    switch(requestedPage.toLowerCase()) {
        case "home" : 
            break;

        case "my-story":
            this.changePage("My Story");
            break;

        case "my-works":
            this.changePage("My Works");
            break;

        case "my-skills":
            this.changePage("My Skills");
            break;

        default:
            this.updateURL();
            break;
    }
}

// This method is responsible to change the current page via state change
changePage(pageName, tag = null) {
    if(pageName === 'home') {
        this.updateURL();
        this.setState(
            {
                currentPage: "home",
                prevPage: tag
            }
        );
    }
    else if(pageName === 'My Story') {
        this.updateURL("my-story");
        this.setState(
            {
                currentPage: "about",
                prevPage: null
            }
        );
    }
    else if(pageName === 'My Works'){
        this.updateURL("my-works");
        this.setState(
            {
                currentPage: "projects",
                prevPage: null
            }
        );
    }
    else if(pageName === 'My Skills'){
        this.updateURL("my-skills");
        this.setState(
            {
                currentPage: "skills",
                prevPage: null
            }
        );
    }
}

所以也许有人可以帮助我弄清楚如何覆盖此服务器重定向并使用我的手动重定向。

0 个答案:

没有答案