在ReactJS中正确使用多个布局的方法是什么?

huangapple go评论88阅读模式
英文:

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 &#39;react&#39;;
import { Route } from &#39;react-router&#39;;
import { Layout } from &#39;./components/Layout&#39;;
import { Home } from &#39;./components/Home&#39;;
import { FetchData } from &#39;./components/FetchData&#39;;
import { Counter } from &#39;./components/Counter&#39;;
import { Urun } from &#39;./components/Urun&#39;;
import { Sepet } from &#39;./components/Sepet&#39;;
import { Urunler } from &#39;./components/Urunler&#39;;

export default class App extends Component {
    displayName = App.name

    render() {
        return (
            &lt;Layout&gt;
                &lt;Route exact path=&#39;/&#39; component={Home} /&gt;
                &lt;Route path=&#39;/counter&#39; component={Counter} /&gt;
                &lt;Route path=&#39;/fetchdata&#39; component={FetchData} /&gt;
                &lt;Route path=&#39;/Urun&#39; component={Urun} /&gt;
                &lt;Route path=&#39;/Sepet&#39; component={Sepet} /&gt;
                &lt;Route path=&#39;/Urunler&#39; component={Urunler} /&gt;      
            &lt;/Layout&gt;
        );
    }
}

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 &#39;react&#39;;
import { Route } from &#39;react-router&#39;;
import { Home } from &#39;./components/Home&#39;;
import { FetchData } from &#39;./components/FetchData&#39;;
import { Counter } from &#39;./components/Counter&#39;;
import { Urun } from &#39;./components/Urun&#39;;
import { Sepet } from &#39;./components/Sepet&#39;;
import { Urunler } from &#39;./components/Urunler&#39;;
import { Giris } from &#39;./components/Giris&#39;;
import { Kayit } from &#39;./components/Kayit&#39;;

export default class App extends Component {
    displayName = App.name

    render() {
        return (
            &lt;div&gt;
                &lt;Route exact path=&#39;/&#39; component={Home} /&gt;
                &lt;Route path=&#39;/counter&#39; component={Counter} /&gt;
                &lt;Route path=&#39;/fetchdata&#39; component={FetchData} /&gt;
                &lt;Route path=&#39;/Urun&#39; component={Urun} /&gt;
                &lt;Route path=&#39;/Sepet&#39; component={Sepet} /&gt;
                &lt;Route path=&#39;/Urunler&#39; component={Urunler} /&gt;
                &lt;Route path=&#39;/Giris&#39; component={Giris} /&gt;
                &lt;Route path=&#39;/Kayit&#39; component={Kayit} /&gt;
            &lt;/div&gt;
        );
    }
}

This is the Home.js

import React, { Component } from &#39;react&#39;;
import { Layout } from &#39;./Layout&#39;;

export class Home extends Component {
    displayName = Home.name

    render() {
        return (
            &lt;Layout&gt;
                &lt;div&gt;
                    ......
                &lt;/div&gt;
            &lt;/Layout&gt;
        );
    }
}

答案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 &#39;react&#39;;

export default class App extends Component {
    render() {
        return (
            &lt;p&gt;Login&lt;/p&gt;
        )
    }
}

Then use the following to import:

import React, { Component } from &#39;react&#39;;
import { Route } from &#39;react-router&#39;;
import { Layout } from &#39;./components/Layout&#39;;
import { Home } from &#39;./components/Home&#39;;
import { FetchData } from &#39;./components/FetchData&#39;;
import { Counter } from &#39;./components/Counter&#39;;
import { Urun } from &#39;./components/Urun&#39;;
import { Sepet } from &#39;./components/Sepet&#39;;
import { Urunler } from &#39;./components/Urunler&#39;;
// HERE import your component
import { Login } from &#39;./components/Login&#39;;

export default class App extends Component {
    displayName = App.name

    render() {
        return (
            &lt;Layout&gt;
                &lt;Route exact path=&#39;/&#39; component={Home} /&gt;
                &lt;Route path=&#39;/counter&#39; component={Counter} /&gt;
                &lt;Route path=&#39;/fetchdata&#39; component={FetchData} /&gt;
                &lt;Route path=&#39;/Urun&#39; component={Urun} /&gt;
                &lt;Route path=&#39;/Sepet&#39; component={Sepet} /&gt;
                &lt;Route path=&#39;/Urunler&#39; component={Urunler} /&gt; 
                // HERE make it into a route path
                &lt;Route path=&#39;/login&#39; component={Login} /&gt;      
            &lt;/Layout&gt;
        );
    }
}

huangapple
  • 本文由 发表于 2020年1月3日 16:22:26
  • 转载请务必保留本文链接:https://go.coder-hub.com/59575247.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定