英文:
What is the proper way to use multiple layouts in ReactJS
问题
这是来自@Quentin的答案。
这是 App.js 文件:
import React, { Component } from 'react';
import { Route } from 'react-router';
import { Home } from './components/Home';
import { FetchData } from './components/FetchData';
import { Counter } from './components/Counter';
import { Urun } from './components/Urun';
import { Sepet } from './components/Sepet';
import { Urunler } from './components/Urunler';
import { Giris } from './components/Giris'; // 添加导入
import { Kayit } from './components/Kayit'; // 添加导入
export default class App extends Component {
displayName = App.name
render() {
return (
<div>
<Route exact path='/' component={Home} />
<Route path='/counter' component={Counter} />
<Route path='/fetchdata' component={FetchData} />
<Route path='/Urun' component={Urun} />
<Route path='/Sepet' component={Sepet} />
<Route path='/Urunler' component={Urunler} />
<Route path='/Giris' component={Giris} /> {/* 添加路由 */}
<Route path='/Kayit' component={Kayit} /> {/* 添加路由 */}
</div>
);
}
}
这是 Home.js 文件:
import React, { Component } from 'react';
import { Layout } from './Layout';
export class Home extends Component {
displayName = Home.name
render() {
return (
<Layout>
<div>
......
</div>
</Layout>
);
}
}
希望这可以帮助你添加登录页面的第二个布局。
英文:
I am new to React. I need to use multiple layouts in my project. I searched for a tutorial but there are so many different ways and couldn't find a working example.
Here is my App.js below. How can I add the second layout for Login page?
import React, { Component } from 'react';
import { Route } from 'react-router';
import { Layout } from './components/Layout';
import { Home } from './components/Home';
import { FetchData } from './components/FetchData';
import { Counter } from './components/Counter';
import { Urun } from './components/Urun';
import { Sepet } from './components/Sepet';
import { Urunler } from './components/Urunler';
export default class App extends Component {
displayName = App.name
render() {
return (
<Layout>
<Route exact path='/' component={Home} />
<Route path='/counter' component={Counter} />
<Route path='/fetchdata' component={FetchData} />
<Route path='/Urun' component={Urun} />
<Route path='/Sepet' component={Sepet} />
<Route path='/Urunler' component={Urunler} />
</Layout>
);
}
}
THIS IS THE ANSWER FROM @Quentin
Thanks to @Quentin, he didnt give me the fish but helped me catch the fish.
This is the App.js
import React, { Component } from 'react';
import { Route } from 'react-router';
import { Home } from './components/Home';
import { FetchData } from './components/FetchData';
import { Counter } from './components/Counter';
import { Urun } from './components/Urun';
import { Sepet } from './components/Sepet';
import { Urunler } from './components/Urunler';
import { Giris } from './components/Giris';
import { Kayit } from './components/Kayit';
export default class App extends Component {
displayName = App.name
render() {
return (
<div>
<Route exact path='/' component={Home} />
<Route path='/counter' component={Counter} />
<Route path='/fetchdata' component={FetchData} />
<Route path='/Urun' component={Urun} />
<Route path='/Sepet' component={Sepet} />
<Route path='/Urunler' component={Urunler} />
<Route path='/Giris' component={Giris} />
<Route path='/Kayit' component={Kayit} />
</div>
);
}
}
This is the Home.js
import React, { Component } from 'react';
import { Layout } from './Layout';
export class Home extends Component {
displayName = Home.name
render() {
return (
<Layout>
<div>
......
</div>
</Layout>
);
}
}
答案1
得分: 3
将您的 Layout
组件放在您希望在其中使用它的路由组件内部,而不是放在所有路由周围(包括您不希望使用它的路由)。
英文:
Whatever your Layout
component is, put it inside the route components you want to use it in instead of around all the routes (including those where you don't want to use it).
答案2
得分: 2
每个路由都必须在目标布局中呈现。
例如 https://www.c-sharpcorner.com/article/multiple-layout-in-react-with-react-router-v4/
英文:
Each route has to be rendered in the target layout.
for e.g https://www.c-sharpcorner.com/article/multiple-layout-in-react-with-react-router-v4/
答案3
得分: 0
我认为你需要更清楚地区分术语“布局”和“组件”。
我假设你正在询问如何添加组件?
你首先需要创建文件./components/Login.js
import React, { Component } from 'react';
export default class Login extends Component {
render() {
return (
<p>Login</p>
)
}
}
然后使用以下方式进行导入:
import React, { Component } from 'react';
import { Route } from 'react-router';
import { Layout } from './components/Layout';
import { Home } from './components/Home';
import { FetchData } from './components/FetchData';
import { Counter } from './components/Counter';
import { Urun } from './components/Urun';
import { Sepet } from './components/Sepet';
import { Urunler } from './components/Urunler';
// 在这里导入你的组件
import { Login } from './components/Login';
export default class App extends Component {
displayName = App.name
render() {
return (
<Layout>
<Route exact path='/' component={Home} />
<Route path='/counter' component={Counter} />
<Route path='/fetchdata' component={FetchData} />
<Route path='/Urun' component={Urun} />
<Route path='/Sepet' component={Sepet} />
<Route path='/Urunler' component={Urunler} />
// 在这里将其变成路由路径
<Route path='/login' component={Login} />
</Layout>
);
}
}
英文:
I think you would need to clarify more between terms layout
and component
.
I assume you asking how to add component?
You would need first create the file ./components/Login.js
import React, { Component } from 'react';
export default class App extends Component {
render() {
return (
<p>Login</p>
)
}
}
Then use the following to import:
import React, { Component } from 'react';
import { Route } from 'react-router';
import { Layout } from './components/Layout';
import { Home } from './components/Home';
import { FetchData } from './components/FetchData';
import { Counter } from './components/Counter';
import { Urun } from './components/Urun';
import { Sepet } from './components/Sepet';
import { Urunler } from './components/Urunler';
// HERE import your component
import { Login } from './components/Login';
export default class App extends Component {
displayName = App.name
render() {
return (
<Layout>
<Route exact path='/' component={Home} />
<Route path='/counter' component={Counter} />
<Route path='/fetchdata' component={FetchData} />
<Route path='/Urun' component={Urun} />
<Route path='/Sepet' component={Sepet} />
<Route path='/Urunler' component={Urunler} />
// HERE make it into a route path
<Route path='/login' component={Login} />
</Layout>
);
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论