2016-11-17 58 views
1

我正在编写一个基于react.js的网站。我希望在我的索引页面中自动滑动背景图像,并从Ant Design导入元素{carousel},该元素具有如何使用carousel的实现。我有代码将路由到每个组件。现在,我想知道如何将{carousel}插入索引页面。我有以下代码:如何将<carousel>元素插入react.js

/* eslint no-unused-vars: "off" */ 

import React from 'react'; 
import { render } from 'react-dom'; 
import { Router, Route, browserHistory } from 'react-router' 

import Root from './Root/Root' 
import MainLayout from './MainLayout/MainLayout' 
import MyRoot from './MyRoot/MyRoot' 
import CreateNew from './CreateNew/CreateNew' 
import './index.css' 
import { Carousel } from 'antd'; 

// see https://github.com/ReactTraining/react-router 
render((

    // <Carousel autoplay> 
    // <div><image>1</image></div> 
    // <div><image>2</image></div> 
    // <div><image>3</image></div> 
    // <div><image>4</image></div> 
    // </Carousel>, mountNode; 

    <Router history={browserHistory}> 
    <Route component={MainLayout}> 
     <Route path="/" component={Root}/> 
     <Route path="/myitinerary" component={MyRoot}/> 
     <Route path="/createnew" component={CreateNew}/> 
    </Route> 
    </Router> 
), document.getElementById('root')); 

老实说,我不知道为什么我这样做,我只是猜测。或者我需要创建另一个组件来保存轮换信息?或者有没有其他简单易懂的方法来做到这一点。非常感谢!

+0

我假设你会把它放到MainLayout组件中,就像你有它的方式(当然没有注释过) –

+0

@SamiKuhmonen谢谢你的建议,但它不管用。 –

+0

@XiufenXu你看到我的回答了吗?它帮助你吗? – Sergio

回答

1

React路由器将根据路径调用不同的组件。因此,在您的示例<Route path="/myitinerary" component={MyRoot}/>中,组件MyRoot需要在某处定义。

它看起来像它从这里来的:从进口MyRoot“./MyRoot/MyRoot”

所以,这里面的文件,你可以有:

class MyRoot extends React.Component { 
    render(){ 
     return (
      <Carousel autoplay> 
      <div><h3>1</h3></div> 
      <div><h3>2</h3></div> 
      <div><h3>3</h3></div> 
      <div><h3>4</h3></div> 
      </Carousel> 
     ); 
    } 
} 
export default MyRoot; 

而这会通过内部的carousell组件。 只是caroussel的例子是:

const { Carousel } = antd; 
 

 
ReactDOM.render(
 
    <Carousel autoplay> 
 
    <div><h3>1</h3></div> 
 
    <div><h3>2</h3></div> 
 
    <div><h3>3</h3></div> 
 
    <div><h3>4</h3></div> 
 
    </Carousel> 
 
, document.getElementById('app'));
.ant-carousel .slick-slide { 
 
    text-align: center; 
 
    height: 160px; 
 
    line-height: 160px; 
 
    background: #364d79; 
 
    color: #fff; 
 
    overflow: hidden; 
 
}
<link href="https://ant.design/index-1.css" rel="stylesheet"/> 
 
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react.min.js"></script> 
 
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react-dom.min.js"></script> 
 
<script src="https://npmcdn.com/antd/dist/antd.js"></script> 
 
<div id="app"></div>

1

使用来自 'antd' 一个进口{传送带}; 顶部组件