我正在使用React和Laravel创建一个应用程序。我被导航部分困住了。我正在将main.js文件(包含nivo-slider代码)加载到index.blade.php文件中。
<body class="home home-4">
<!-- React App Container -->
<div class="all">
<div id="app"></div>
</div>
<!-- App JS -->
<script src="{{URL::asset('js/app.js')}}" ></script>
<!-- Vendor JS -->
<script src="{{URL::asset('/libs/jquery/jquery.js')}}"></script>
<script src="{{URL::asset('/libs/bootstrap/js/bootstrap.js')}}"></script>
<script src="{{URL::asset('/libs/jquery.countdown/jquery.countdown.js')}}"></script>
<script src="{{URL::asset('/libs/nivo-slider/js/jquery.nivo.slider.js')}}"></script>
<script src="{{URL::asset('/libs/owl.carousel/owl.carousel.min.js')}}"></script>
<script src="{{URL::asset('/libs/slider-range/js/tmpl.js')}}"></script>
<script src="{{URL::asset('/libs/slider-range/js/jquery.dependClass-0.1.js')}}"></script>
<script src="{{URL::asset('/libs/slider-range/js/draggable-0.1.js')}}"></script>
<script src="{{URL::asset('/libs/slider-range/js/jquery.slider.js')}}"></script>
<script src="{{URL::asset('/libs/elevatezoom/jquery.elevatezoom.js')}}"></script>
<!-- Template JS -->
<script src="{{URL::asset('js/main.js')}}"></script>
</body>
以下是我的包含响应路由器的应用组件。
var React = require('react');
var ReactDOM = require('react-dom');
const Router = require('react-router-dom').BrowserRouter;
const Route = require('react-router-dom').Route;
const Switch = require('react-router-dom').Switch;
// var Nav = require('./Nav');
var Home = require('./components/Home');
var About = require('./components/About');
var Contact = require('./components/Contact');
ReactDOM.render(
<Router>
<div className="container">
<Route path="/" exact component={Home}></Route>
<Route path="/home" component={Home}></Route>
<Route path="/about" component={About}></Route>
<Route path="/contact" component={Contact}></Route>
</div>
</Router>, document.getElementById('app'));
问题出在Home组件上。首次加载该应用程序时,它将正确呈现Nivo Slider。但是,当我单击任何导航链接并导航回首页时,Nivo Slider会中断。
家庭组件:
........ // Some code here
class Home extends React.Component {
render() {
return(
<div>
<Header />
<div id="content" className="site-content">
<div className="section slideshow">
<div className="container">
<div className="tiva-slideshow-wrapper">
<div id="tiva-slideshow" className="nivoSlider">
<Link to="#">
<img className="img-responsive" src="img/slideshow/home4-slideshow-1.jpg" alt="Slideshow Image" />
</Link>
<Link to="#">
<img className="img-responsive" src="img/slideshow/home4-slideshow-2.jpg" alt="Slideshow Image" />
</Link>
<Link to="#">
<img className="img-responsive" src="img/slideshow/home4-slideshow-3.jpg" alt="Slideshow Image" />
</Link>
</div>
</div>
</div>
</div>
........... // More Code to go
根据我的理解,由于某种原因,滑块无法正常运行,因此无法加载main.js。
第一次加载时滑块部分:
<div class="section slideshow">
<div class="container">
<div class="tiva-slideshow-wrapper">
<div id="tiva-slideshow" class="nivoSlider">
<a href="/" class="nivo-imageLink" style="display: block;">
<img class="img-responsive" src="img/slideshow/home4-slideshow-1.jpg" alt="Slideshow Image" style="width: 1540px; visibility: hidden;">
</a>
<a href="/" class="nivo-imageLink" style="display: none;">
<img class="img-responsive" src="img/slideshow/home4-slideshow-2.jpg" alt="Slideshow Image" style="width: 1540px; visibility: hidden;">
</a>
<a href="/" class="nivo-imageLink" style="display: none;">
<img class="img-responsive" src="img/slideshow/home4-slideshow-3.jpg" alt="Slideshow Image" style="width: 1540px; visibility: hidden;">
</a>
<img class="nivo-main-image" src="img/slideshow/home4-slideshow-1.jpg" style="display: inline; width: 1540px; height: 686px;">
<div class="nivo-caption" style="display: none;"></div>
<div class="nivo-directionNav">
<a class="nivo-prevNav">Prev</a>
<a class="nivo-nextNav">Next</a>
</div>
<!-- Some of the nivo slider code goes here -->
<div class="nivo-controlNav">
<a class="nivo-control active" rel="0">1</a>
<a class="nivo-control" rel="1">2</a>
<a class="nivo-control" rel="2">3</a>
</div>
</div>
</div>
</div>
导航后的滑块部分:
<div class="section slideshow">
<div class="container">
<div class="tiva-slideshow-wrapper">
<div id="tiva-slideshow" class="nivoSlider">
<a href="/">
<img class="img-responsive" src="img/slideshow/home4-slideshow-1.jpg" alt="Slideshow Image">
</a>
<a href="/">
<img class="img-responsive" src="img/slideshow/home4-slideshow-2.jpg" alt="Slideshow Image">
</a>
<a href="/">
<img class="img-responsive" src="img/slideshow/home4-slideshow-3.jpg" alt="Slideshow Image">
</a>
</div>
</div>
</div>
</div>
答案 0 :(得分:1)
添加 loadjs包,并在 componentDidMount()中调用它,以在路由后加载库。请查看下面的代码片段以供参考。
componentDidMount() {
loadjs('js/main.js', function() {
console.log('Test');
});
}
答案 1 :(得分:0)
感谢this的回答,该回答确认了是 js文件重定向到较新路由时未加载的问题。
因此我添加了 loadjs 软件包,并在 componentDidMount()中对其进行了调用,以在路由后加载库,从而解决了该问题。>
Home Component中的我的componentDidMount()方法:
componentDidMount() {
loadjs('js/main.js', function() {
console.log('Everything Working Fine');
});
}