gpt-4-0314· OpenAI
WildChat-4.8M (AI2)
使用iframe嵌套页面,利用window.addEventListener('message')获取用户信息,为了避免重复获取影响性能,在第一次获取之后,vuex存储了用户信息…
- User
使用iframe嵌套页面,利用window.addEventListener('message')获取用户信息,为了避免重复获取影响性能,在第一次获取之后,vuex存储了用户信息。问题是用户退出之后换另一个账户登录,iframe页面不会更新用户信息,有什么好的方案
- Assistant
有两种方案可以解决这个问题。
方案1:使用
localStorage代替vuex存储用户信息。- 将用户信息储存在本地存储中,而不是在
vuex中。当iframe收到来自父窗口的message事件时,将用户信息存储在localStorage中。
window.addEventListener('message', (event) => { if (event.origin === 'https://example.com') { // 确认发送事件的来源 // 将用户信息存储到本地存储中 localStorage.setItem('userInfo', JSON.stringify(event.data)); } });- 当用户退出并用另一个帐户登录时,通知iframe中的脚本清除
localStorage中的用户信息。
// 父窗口中,向iframe发送消息 document.querySelector('iframe').contentWindow.postMessage('clearUserInfo', '*'); // iframe中,监听消息并清除本地存储 window.addEventListener('message', (event) => { if (event.origin === 'https://example.com' && event.data === 'clearUserInfo') { localStorage.removeItem('userInfo'); } });方案2:使用在父窗口和iframe之间通信。
- 将用户信息储存在本地存储中,而不是在
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.