简体   繁体   English

如何配置我的webpack.config.js文件以正确导航到其他html页面?

[英]How would I configure my webpack.config.js file to properly navigate to other html pages?

I'm kind of stuck on how to configure webpack.config.js file in order for it to navigate to other html pages. 我有点想如何配置webpack.config.js文件,以便使其导航到其他html页面。 In other words, a multi-page react.js website. 换句话说,是一个多页面的react.js网站。

I want to navigate to About.html upon clicking on About in Main.jsx file. Main.jsx文件中单击About ,我想导航到About.html I'm trying to use Routing but I know I'm way off. 我正在尝试使用“路由”,但我知道我已经走了。

I know that the meat of this is in webpack.config.js so I made an attempt below to see if it works by trying to mimic the HtmlWebPackPlugin({}) of index.html , but it doesn't. 我知道它的webpack.config.js是在webpack.config.js因此我在下面尝试通过尝试模仿index.htmlHtmlWebPackPlugin({})来查看它是否起作用,但是没有。

Note: I'm not using create-react-app for my project. 注意:我没有为我的项目使用create-react-app I'm just trying things out outside of create-react-app environment and instead, onto other environments. 我只是尝试在create-react-app环境之外,而是在其他环境中尝试其他事情。 I installed Babel and all the other stuff manually. 我手动安装了Babel和所有其他东西。

What am I doing wrong and how can I fix it? 我在做什么错,我该如何解决?

Here's Main.jsx : 这是Main.jsx

import React, {Component} from "react";
import ReactDOM from "react-dom";
import {Router, Route} from 'react-router';

class Main extends Component {
    constructor(props) {
        super(props);
        this.goToAbout = this.goToAbout.bind(this);
    }

    goToAbout() {
        console.log("clicked");

        return(
            <Router>
                <Route path={"/About"} component={Main}/>
            </Router>
        )
    }

    render() {
        return (
            <div>
                <button className="button is-primary" onClick={this.goToAbout}>About</button>
            </div>
        );
    }
}

export default Main;

const wrapper = document.getElementById("create-article-form");
wrapper ? ReactDOM.render(<Main/>, wrapper) : false;

// about page (my attempt) 
const aboutWrapper = document.getElementById("about-page"); 
aboutWrapper ? ReactDOM.render(<Main/>, wrapper) : false;

Here's About.html : 这是About.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
    <h1>About Section</h1>
    <div id="about-page"></div>
</body>
</html>

Here's webpack.config.js : 这是webpack.config.js

const HtmlWebPackPlugin = require("html-webpack-plugin");
module.exports = {
    module: {
        rules: [
            {
                test: /\.(js|jsx)$/,
                exclude: /node_modules/,
                use: {
                    loader: "babel-loader"
                }
            },
            {
                test: /\.html$/,
                use: [
                    {
                        loader: "html-loader"
                    }
                ]
            }
        ]
    },
    plugins: [
        new HtmlWebPackPlugin({
            template: "./src/index.html",
            filename: "./index.html"
        }),

        // my attempt
        new HtmlWebPackPlugin({ 
            template: "./src/About.html",
            filename: "./About.html"
        })
    ]
};

I don't believe this has to do with webpack. 我不认为这与webpack有关。

Instead of 代替

<button className="button is-primary" onClick={this.goToAbout}>About</button>

use: 采用:

<Link to="/About/">About</Link>

If you are trying to link to a page that is not inside of react, you could use something like: 如果您尝试链接到不在react内的页面,则可以使用以下方法:

<a href={`${window.location.origin}/about`} />`

You'll want to use a standard HTML anchor tag with the href set to the location where you want navigate to. 您将要使用标准HTML锚标记,并将href设置为要导航到的位置。

instead of <Route path={"/About"} component={Main}/> use 
<BrowserRouter>
<div>
<Switch>
<Link  path="/about" component="{Main}">
</Switch>
</div>
</BrowserRouter>or

<Link to="/about"></Link> and 

and import {BrowserRouter,Route,Switch,Link} from 'react-router-dom';

A bunch of issues here - this should get you pointed in the right direction: 这里有很多问题-这应该让您指出正确的方向:

Main.jsx Main.jsx

import React from "react";
import {Link, Router, Route, Switch} from 'react-router';  // <- this should be 'react-router-dom' -- upgrade?

const Main = () => (
  <Router>
    <Switch>
      <Route path="/About.html" />
    <Switch>
    <Link to="/About.html">About</Link>
  </Router>
);

ReactDOM.render(<Main/>, document.getElementById("app"));  // <- your React app should be mounted to a single element  

webpack.config.js webpack.config.js

const HtmlWebPackPlugin = require("html-webpack-plugin");
module.exports = {
    entry: "./src/js/Main",  // <- replace with actual path to Main.jsx
    module: {
        rules: [
            {
                test: /\.(js|jsx)$/,
                exclude: /node_modules/,
                use: {
                    loader: "babel-loader"
                }
            },
            {
                test: /\.html$/,
                use: [
                    {
                        loader: "html-loader"
                    }
                ]
            }
        ]
    },
    plugins: [
        new HtmlWebPackPlugin({
            template: "./src/index.html",
            filename: "./index.html"
        })
    ]
};

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何配置 webpack.config.js 将我的 HTML 文件转换/转换为 reactjs 中的 JS? - How can I configure webpack.config.js to convert/transform my HTML file into JS in reactjs? 如何将适用于 webpack.config.js 的指令/代码翻译成 vue.config.js 文件? - How can I translate the instructions/code that would work on the webpack.config.js to the vue.config.js file? 我的 webpack.config.js 文件中可能缺少什么以使“npm start”不起作用? - What could I be missing in my webpack.config.js file to have "npm start" not work? 如果没有 webpack.config.js 文件,如何正确安装 Vuetify? - How to install Vuetify correctly if there is no webpack.config.js file? 如何在 webpack.config.js 文件中包含节点模块 - How to include node module in webpack.config.js file 如何从 webpack.config.js 文件访问环境变量? - How to access env vars from webpack.config.js file? 无法在我的 react js 文件中导入 css 文件,我已经安装了 css 加载器,但不知道如何将它包含在我的 webpack.config.js 中 - unable to import css file in my react js file, i have installed the css loader but dont know how to include it in my webpack.config.js 部署应用程序后如何在 Webpack.config.js 中配置代理 url? - How to configure the proxy url inside Webpack.config.js after deploying the app? 如何在 VueCLI 3 中全局获取`webpack.config.js` 或 `vue.config.js` 文件数据 - How to get `webpack.config.js` or `vue.config.js` file data globally in VueCLI 3 Webpack.config.js配置 - Webpack.config.js configuration
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM