Javascript 为什么window.history.pushState更新我的内容而不是URL
我希望能够在我的客户端应用程序上切换语言,而无需重新加载页面。我能够做到这一点,但我的url并没有像以前那样改变 我有两种语言-法语-Javascript 为什么window.history.pushState更新我的内容而不是URL,javascript,reactjs,function,history,Javascript,Reactjs,Function,History,我希望能够在我的客户端应用程序上切换语言,而无需重新加载页面。我能够做到这一点,但我的url并没有像以前那样改变 我有两种语言-法语-/fr,英语-/en 这是我用window.history.pushState function generateUrlSwitch(lang) { const location = window.location.pathname; if (location.length > 3) { const actualPath =
/fr
,英语-/en
这是我用window.history.pushState
function generateUrlSwitch(lang) {
const location = window.location.pathname;
if (location.length > 3) {
const actualPath = location.substr(4, location.length);
const url = `/${lang}/${actualPath}`;
return window.history.pushState(null, null, url);
} else {
return '/' + lang;
}
}
当前行为:
在不重新加载的情况下将内容更新到所选语言-url保持不变,但我可以看到参数在en/fr和I console.log之间以毫秒为单位切换,这两个选项都在console中
预期行为:
基于所选语言更新内容而不重新加载页面+基于所选语言更新url
语言切换器正在调用函数:
href={generateUrlSwitch('en')}
href={generateUrlSwitch('fr')}
我做错了什么?不返回任何内容。也没有。我试图返回那里
lang
,但是输出是一样的,没有任何变化。