Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

是否可以添加个router.replace方法,改变当前页面和URL,却不新增历史记录 #70

Open
xiaodongxing opened this issue Oct 17, 2023 · 1 comment

Comments

@xiaodongxing
Copy link

No description provided.

@xiaodongxing
Copy link
Author

/**
* 重定向
*
* @param {string} url 重定向的地址
* @param {Object?} options 重定向的行为配置
* @param {boolean?} options.force 是否强制刷新
* @param {boolean?} options.replace 是否使用replace方法替换当前历史记录
*/
HashLocator.prototype.redirect = function (url, options) {
options = options || {};

    url = resolveURL(url, this.current);
    var referrer = this.current;

    var isChanged = url !== referrer;
    if (isChanged) {
        this.referrer = referrer;
        this.current = url;
        location[options.replace ? 'replace' : 'assign'](url)
    }
    else {
        referrer = this.referrer;
    }

    if ((isChanged || options.force) && !options.silent) {
        this.fire('redirect', {url: url, referrer: referrer});
    }
};

/**
* 重定向
*
* @param {string} url 重定向的地址
* @param {Object?} options 重定向的行为配置
* @param {boolean?} options.force 是否强制刷新
* @param {boolean?} options.replace 是否使用replace方法替换当前历史记录
*/
HTML5Locator.prototype.redirect = function (url, options) {
options = options || {};

    url = resolveURL(url, this.current);
    var referrer = this.current;

    var isChanged = url !== referrer;

    if (isChanged) {
        this.referrer = referrer;
        this.current = url;

        history[options.replace ? 'replaceState' : 'pushState']({}, '', url)
    }

    if ((isChanged || options.force) && !options.silent) {
        this.fire('redirect', {url: url, referrer: referrer});
    }
};

/**
* @param {Object|string} url 路由地址
* @param {Object?} options 重定向的行为配置
* @param {boolean?} options.force 是否强制刷新
*/
Router.prototype.replace = function (url, options) {
this.locator.redirect(stringifyURL(url), Object.assign({replace: true}, options));
};

BUPTlhuanyu added a commit to BUPTlhuanyu/san-router that referenced this issue Oct 19, 2023
errorrik added a commit that referenced this issue Oct 20, 2023
feat: router 新增 replace 能力 #70
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant