componentDidMount()
在Safari中返回时,即使从未卸载过组件,React 16触发器也是如此。如何知道何时安装?
class Foo extends React.Component {
state = {
loading: false
}
componentDidMount() {
// when going back in safari
// triggers in react 16, but not in 15.3 or preact
console.log('mounted');
}
componentWillUnmount() {
// will never trigger
console.log('will unmount');
}
leave() {
this.setState({
loading: true
});
setTimeout(() => {
window.location.href = 'https://github.com/';
}, 2000);
}
render() {
return this.state.loading ? <div>loading...</div> : <button onClick={this.leave.bind(this)}>leave</button>;
}
}
Run Code Online (Sandbox Code Playgroud)
Safari使用bfcache。如果返回,它将从缓存中获取最后一页。
当使用react 15.3或preact之类的库时,离开页面不会触发componentWillUnmount
,返回页面也不会触发componentDidMount
。
此行为会导致多个问题-例如,loading …
所以问题是,我创建了一个新分支:myname/mysuperbranch
同时我有 origin/master 分支。在我对分支进行更改后,我检出到 origin/master,它仍然包含我在myname/mysuperbranch分支内所做的所有更改。
如何正确切换分支?例如:
我对 myname/myspuerbranch 做了一些更改
在本地保存此更改。
切换到 origin/master 分支,它应该是干净的,没有任何更改。
我究竟做错了什么?
我正在实现 Vue.js 动态组件,但我似乎无法弄清楚如何仅在获取数据时才显示该组件。在此之前,它应该显示旧组件。
场景是这样的。当页面加载时,它应该显示 Home 组件。然后我点击“显示帖子”。在 Posts 组件获取它的帖子之前,什么都不应该发生。然后它应该显示 Posts 组件。我不想显示任何加载。
我可以在我的 Home 组件中获取帖子,但我认为 Posts 组件应该对此负责。此外,如果我有很多组件,我不想在我的 Home 组件中获取它们的所有数据。他们应该获取自己的数据(我认为)。这是可能的吗?
主页.js
import Home from './home.js'
import Posts from './posts.js'
export default {
template: `
<div>
<a @click="showPosts">Show posts</a>
<component :is="currentComponent" />
</div>
`,
methods:
{
showPosts()
{
// Do this ONLY after the Posts component has fetched it's data... not immediately...
this.currentComponent = Posts
}
},
data:
{
currentComponent: Home
},
}
Run Code Online (Sandbox Code Playgroud)
帖子.js
export default {
template: `
<div>
<div v-for="post in posts">{{ …
Run Code Online (Sandbox Code Playgroud) 我想上传多张图片。当我选择多个图像时,将调用 photoSelected 函数。我想使用 base64 但它在控制台上显示此错误。
PhotoSelected (e){
Let files = e.target.files;
Let reader = new FileReader();
Let file;
For (let i=0; I<files.length ; i++){
file = files [i];
reader.onload = (file) => {
This.product.photo[i] = reader.result;
}
reader.readAsDataURL(file)
}
}
Run Code Online (Sandbox Code Playgroud)