javascript - 如何在从单独的文件加载自定义 javascript 之前从反应中加载所有提取
问题描述
我分别使用 react 和 jquery 我希望 bundle.js 在运行 jquery 函数之前从 react 获取所有 api 数据。
我有一个反应代码从服务器/api和jquery加载所有图像以滑动这些图像并管理图像的高度以适合屏幕。我首先包含了 bundle.js,然后是我的 custom.js 文件。即使 bundle.js 先运行,但是 fetch api 在 custom.js 之后运行,因为 jquery 无法滑动这些图像。
应用程序.js
export class App extends Component {
constructor(props){
super(props)
this.state = {
loading : false,
all_data : []
}
this.fetchFromServer();
}
fetchFromServer(){
Promise.all([
fetch("http://abcdef.com/api/slider.php",{
method : "POST",
headers: new Headers({
'Accept' : 'application/json',
}),
body: JSON.stringify({
limit : "10",
})
})
]).then(([all_post_api])=>Promise.all([all_post_api.json()])).
then(([post_data]) => this.setState({
loading : false,
all_data : post_data
}))
}
render(){
const allowRender = this.state.all_data && this.state.all_data.carousel
if (allowRender) {
return (<div className="right-content">
<Main data={this.state.all_data} />
</div>)
}
return null;
}
}
主.js
export class Main extends Component {
render(){
console.log(this.props.data.carousel);
return (
<div className="flexslider home-page">
<ul className="slides">
{
this.props.data.carousel.map((name,index)=>{
return (
<li className="images-bg" key={index}>
<img src={name.url} />
<div className="circle">
<div className="home-title left">
<span>
Welcome <strong>to</strong>
</span>
<span>
<strong>foto</strong>hunter
</span>
<p>Only the best pictures of animals and travel.</p>
</div>
<a className="button-down glyph fa-angle-down" href=""></a>
</div>
</li>
)
})
}
</ul>
</div>
)
}
}
自定义.js
$(function (){
console.log("loaded content");
$('.images-bg').each(function(){
$(this).css({
'background-image': 'url(' +$('img', this).hide().attr('src') +')',
'height': $(window).height()
});
});
/*---------- BIG SLIDER ----------*/
$('.portfolio-with-details .flexslider, .service .flexslider').flexslider({slideshowSpeed: 5000});
$('.portfolio-image.flexslider').flexslider({slideshow: false});
$('.flexslider.home-page').flexslider({
slideshowSpeed: 5000,
after : function(slider){
var next = $('.flex-active-slide', slider).find('.home-title');
var className = '';
if(next.hasClass('left')){
className = 'bounceInLeft';
}else if(next.hasClass('top')){
className = 'flipInX';
}else if(next.hasClass('zoom')){
className = 'bounceIn';
}
next.addClass(className + ' animated');
next.one('webkitAnimationEnd mozAnimationEnd MSAnimationEnd oanimationend animationend', function(){
next.removeClass(className + ' animated');
});
}
});
/*---------- //BIG SLIDER ----------*/
})
下面的截图是我现在得到的。我要相反。我希望在 Main.js console.log 之后打印 custom.js console.log 消息
解决方案
在您的 Jquery 中尝试将其更改为
$( document ).ready(function (){
console.log("loaded content");
$('.images-bg').each(function(){
$(this).css({
'background-image': 'url(' +$('img', this).hide().attr('src') +')',
'height': $(window).height()
});
});
/*---------- BIG SLIDER ----------*/
$('.portfolio-with-details .flexslider, .service .flexslider').flexslider({slideshowSpeed: 5000});
$('.portfolio-image.flexslider').flexslider({slideshow: false});
$('.flexslider.home-page').flexslider({
slideshowSpeed: 5000,
after : function(slider){
var next = $('.flex-active-slide', slider).find('.home-title');
var className = '';
if(next.hasClass('left')){
className = 'bounceInLeft';
}else if(next.hasClass('top')){
className = 'flipInX';
}else if(next.hasClass('zoom')){
className = 'bounceIn';
}
next.addClass(className + ' animated');
next.one('webkitAnimationEnd mozAnimationEnd MSAnimationEnd oanimationend animationend', function(){
next.removeClass(className + ' animated');
});
}
});
/*---------- //BIG SLIDER ----------*/
})
这样,React 会在挂载之前获取,而 Jquery 会在页面完全准备好时触发!
推荐阅读
- laravel-5 - 在 laravel 的控制器中保存 blob 图像
- php - Summernote 和 img-upload.php 文件中的绝对路径
- javascript - Equivalent of python's range in Javascript
- python - 用公式的结果填充整列 - 熊猫
- javascript - JSON.parse(string) 的匹配器 - 如何在运行匹配器之前调用 lambda?
- angular - Angular - 拖放以上传附件
- javascript - 如何使用数组从对象数组中提取值
- sql - SSRS 为每条记录设置年份变量
- console - 如何通过任何控制台/命令行将 DWG 转换为 STP 或 IGES
- gradle - Gradle - 作为“jar”任务的一部分将 jar 复制到另一个文件夹