GVKun编程网logo

React Routing with Express、Webpack 开发中间件、React 路由器 dom(react中路由有哪些常用组件)

3

此处将为大家介绍关于ReactRoutingwithExpress、Webpack开发中间件、React路由器dom的详细内容,并且为您解答有关react中路由有哪些常用组件的相关问题,此外,我们还将

此处将为大家介绍关于React Routing with Express、Webpack 开发中间件、React 路由器 dom的详细内容,并且为您解答有关react中路由有哪些常用组件的相关问题,此外,我们还将为您介绍关于./node_modules/react-router-dom/react-router-dom.js尝试导入错误:未从“ react-router”导出“ Navigate”、645 webpack常用plugins:clean-webpack-plugin,html-webpack-plugin,webpack.DefinePlugin,copy-webpack-plug、angularjs-routing – 使用angular-routing = undefined的ngTable不是一个函数、Angular路由器:命名的插座似乎不适用于相对路由,也不适用于延迟加载的模块 lazy-member-routing.module app-routing.module.ts list.component.html的有用信息。

本文目录一览:

React Routing with Express、Webpack 开发中间件、React 路由器 dom(react中路由有哪些常用组件)

React Routing with Express、Webpack 开发中间件、React 路由器 dom(react中路由有哪些常用组件)

如何解决React Routing with Express、Webpack 开发中间件、React 路由器 dom

我在使用 react-router-dom 和使用 express、webpack-dev-middleware、webpack-hot-middleware 的自定义开发服务器进行路由时遇到问题。

这是我的 webpack 配置。

const path = require("path");
const HtmlWebpackPlugin = require("html-webpack-plugin");
const webpack = require(''webpack'');

module.exports = {
    entry: [
        // ''react-hot-loader/patch'',''webpack-hot-middleware/client?reload=true'',''./index.js''
    ],output: {
        filename: ''[name].js'',path: path.join(__dirname,''./dist''),publicPath: ''/'',clean: true,chunkFilename: ''[name].chunk.js'',},mode: ''development'',devtool: ''inline-source-map'',module: {
        rules: [
            {
                test: /\\.(js|jsx)$/,exclude: /node_modules/,use: [''babel-loader'']
            },{
                test: /\\.css$/,use: [''style-loader'',''css-loader'']
            },{
                test: /\\.s[ac]ss$/,''css-loader'',''sass-loader'']
            },{
                test: /\\.(png|jpe?g|gif)$/,type: ''asset/resource''
            },]
    },plugins: [
        new webpack.HotModuleReplacementPlugin(),new HtmlWebpackPlugin({
            template: path.join(__dirname,"public","index.html"),title: ''Title''
        }),],optimization: {
        splitChunks: {
            chunks: ''all'',}

这是我的自定义 dev express server.js 文件

const express = require(''express'');
const webpack = require(''webpack'');
const webpackDevMiddleware = require(''webpack-dev-middleware'');
const webpackHotMiddleware = require(''webpack-hot-middleware'');
const path = require(''path'');
const fs = require(''fs'');
const app = express();
const config = require("./webpack.dev.config");

const compiler = webpack(config);

//Enable "webpack-dev-middleware"
app.use(webpackDevMiddleware(compiler,{
    publicPath: config.output.publicPath,stats: {
        chunks: false,hash: false,modules: false,version: false,assets: false,entrypoints: false,builtAt: false,}
}));

//Enable "webpack-hot-middleware"
app.use(webpackHotMiddleware(compiler));

app.use(express.static(''./public''));


// serve the routes
app.get(''*'',(req,res) => {
     fs.readFile(path.join(compiler.outputPath,''index.html''),(err,file) => {
        if (err) {
            res.sendStatus(404);
        } else {
            res.send(file.toString());
        }
    });
});

app.listen(8000,() => console.log(''server listening on port 8000\\n''))

当我打开应用程序并且所有路由都给出 404 not found 时。但是当我向编译器选项添加 writetodisk 选项时,一切正常。我希望路由在不写入磁盘的情况下工作。任何帮助表示赞赏。提前致谢!!

这是我的文件夹结构

解决方法

此问题的解决方案已发布在 Github 问题页面 here 上。

为了使用 webpack-middleware 从内存提供文件,而不将它们写入磁盘,同时还能够显式引用文件(例如在 res.render 中),建议的解决方案是添加webpackMiddleware 两次,在 historyApiFallback 之前和之后,如下所示:

var webpackMiddleware = require("webpack-dev-middleware");
var historyApiFallback = require("connect-history-api-fallback");
var instance = webpackMiddleware(...);
app.use(instance);
app.use(historyApiFallback());
app.use(instance);

./node_modules/react-router-dom/react-router-dom.js尝试导入错误:未从“ react-router”导出“ Navigate”

如何解决./node_modules/react-router-dom/react-router-dom.js尝试导入错误:未从“ react-router”导出“ Navigate”

./node_modules/react-router-dom/react-router-dom.js Attempted import error: ''Navigate'' is not exported from ''react-router''.

react-router-dom的版本是6.0.0-alpha.2,而react-router是5.2.0。 两者均已正确安装。我不确定如何解决此错误。有人可以给我任何可能的解决方法吗?

我的代码中甚至没有<Navigate to=?>行。

解决方法

为什么只安装两个都需要,这可以工作

  1. 执行npm删除react-router
  2. 删除node_modules
  3. 纱线安装或npm安装和
  4. 启动纱线或启动npm

645 webpack常用plugins:clean-webpack-plugin,html-webpack-plugin,webpack.DefinePlugin,copy-webpack-plug

645 webpack常用plugins:clean-webpack-plugin,html-webpack-plugin,webpack.DefinePlugin,copy-webpack-plug

-

  • 前端小菜鸟,喜欢前端,不断学习
  • 微信:jie178463596
  • 微信小群:纯粹讨论技术、面试、工作为主,划水少,拒绝广告

认识Plugin


CleanWebpackPlugin


HtmlWebpackPlugin


生成的index.html分析


自定义HTML模板


自定义模板数据填充


DefinePlugin的介绍


DefinePlugin的使用


copyWebpackPlugin


目录结构


wk.config.js

const path = require('path');
const { CleanWebpackPlugin } = require('clean-webpack-plugin');
const HtmlWebpackPlugin = require('html-webpack-plugin');
const { DefinePlugin } = require('webpack'); // DefinePlugin是webpack内置插件
const copyWebpackPlugin = require('copy-webpack-plugin');

module.exports = {
  entry: "./src/main.js",
  output: {
    filename: "js/bundle.js",
    // 必须是一个绝对路径
    path: path.resolve(__dirname, "./build"),
    // assetmodulefilename: "img/[name].[hash:6][ext]"
  },
  module: {
    rules: [
      {
        // 规则使用正则表达式
        test: /\.css$/, // 匹配资源
        use: [
          // { loader: "css-loader" },
          // 注意: 编写顺序(从下往上, 从右往做, 从后往前)
          "style-loader",
          {
            loader: "css-loader",
            options: {
              importLoaders: 1
            }
          },
          "postcss-loader"
        ],
        // loader: "css-loader"
      },
      {
        test: /\.less$/,
        use: [
          "style-loader",
          {
            loader: "css-loader",
            options: {
              importLoaders: 2
            }
          },
          "postcss-loader",
          "less-loader"
        ]
      },
      {
        test: /\.(png|jpe?g|gif|svg)$/,
        // type: "asset/resource", file-loader的效果
        // type: "asset/inline", url-loader
        type: "asset",
        generator: {
          filename: "img/[name].[hash:6][ext]"
        },
        parser: {
          dataUrlCondition: {
            maxSize: 100 * 1024
          }
        }
      },
      {
        test: /\.ttf|eot|woff2?$/i,
        type: "asset/resource",
        generator: {
          filename: "font/[name].[hash:6][ext]"
        }
      }
    ]
  },
  plugins: [
    new CleanWebpackPlugin(),
    new HtmlWebpackPlugin({
      title: "哈哈 webpack",
      template: "./public/index.html"
    }),
    new DefinePlugin({
      // 要包裹两层引号
      BASE_URL: '"./"'
    }),
    new copyWebpackPlugin({
      patterns: [
        {
          // to: xxx, // 不用写,默认会使用output.path
          from: "public",
          globOptions: {
            ignore: [
              "**/index.html",
              "**/.DS_Store",
              "**/abc.txt"
            ]
          }
        }
      ]
    })
  ]
}


publuc/index.html

<!DOCTYPE html>
<html lang="">
  <head>
    <Meta charset="utf-8">
    <Meta http-equiv="X-UA-Compatible" content="IE=edge">
    <Meta name="viewport" content="width=device-width,initial-scale=1.0">
    
    <link rel="icon" href="<%= BASE_URL %>favicon.ico">
    
    <title><%= htmlWebpackPlugin.options.title %></title>
  </head>
  <body>
    <noscript>
      <strong>We're sorry but <%= htmlWebpackPlugin.options.title %> doesn't work properly without JavaScript enabled. Please enable it to continue.</strong>
    </noscript>
    <div id="app"></div>
    <!-- built files will be auto injected -->
  </body>
</html>

build/index.html

<!DOCTYPE html>
<html lang="">
  <head>
    <Meta charset="utf-8">
    <Meta http-equiv="X-UA-Compatible" content="IE=edge">
    <Meta name="viewport" content="width=device-width,initial-scale=1.0">
    
    <link rel="icon" href="./favicon.ico">
    
    <title>杰帅的webpack</title>
  <script defer src="js/bundle.js"></script></head>
  <body>
    <noscript>
      <strong>We're sorry but 杰帅的webpack doesn't work properly without JavaScript enabled. Please enable it to continue.</strong>
    </noscript>
    <div id="app"></div>
    <!-- built files will be auto injected -->
  </body>
</html>

angularjs-routing – 使用angular-routing = undefined的ngTable不是一个函数

angularjs-routing – 使用angular-routing = undefined的ngTable不是一个函数

请帮忙.我被困在这一点超过14个小时,我找不到解释或例子.

这就是我现在拥有的:
http://plnkr.co/edit/PaYR7c0QXSKxl1jcmRBQ

如果我不使用角度路由,ngTable可以正常工作.一旦我添加路由,它就会出现此错误.
当我评论$scope.tableParams = new ngTableParams({时,错误消失了
…但是数据没有显示.

代码不起作用:

var app = angular.module('main',[
  'ngRoute','ngTable'
]).
config(['$routeProvider',function($routeProvider) {
  $routeProvider.when('/',{templateUrl: 'partial1.html',controller: 'MyCtrl1'});
  $routeProvider.otherwise({redirectTo: '/'});
}]);

app.controller('MyCtrl1',[function($scope,ngTableParams) {
  var data = [{name: "Moroni",age: 50},{name: "Tiancum",age: 43},{name: "Jacob",age: 27},{name: "Nephi",age: 29},{name: "Enos",age: 34},age: 34}];

    $scope.tableParams = new ngTableParams({
        page: 1,// show first page
        count: 10           // count per page
    },{
        total: data.length,// length of data
        getData: function($defer,params) {
            $defer.resolve(data.slice((params.page() - 1) * params.count(),params.page() * params.count()));
        }
    });   
  }]);
    </script>
  </head>

  <body ng-app="main">
    <div ng-view=""></div>
  </body>

解决方法

应更正HTML标题:(您在角度之前加载ngTable)

<script>document.write('<base href="' + document.location + '" />');</script>
<script data-require="angular.js@*" data-semver="1.2.10" src="http://code.angularjs.org/1.2.10/angular.js"></script>
<script data-require="angular-route@*" data-semver="1.2.10" src="http://code.angularjs.org/1.2.10/angular-route.js"></script>
<script data-require="ng-table@*" data-semver="0.3.0" src="http://bazalt-cms.com/assets/ng-table/0.3.0/ng-table.js"></script>

控制器声明有点偏.

app.controller('MyCtrl1',['$scope','ngTableParams',function MyCtrl1($scope,ngTableParams) {

Angular路由器:命名的插座似乎不适用于相对路由,也不适用于延迟加载的模块 lazy-member-routing.module app-routing.module.ts list.component.html

Angular路由器:命名的插座似乎不适用于相对路由,也不适用于延迟加载的模块 lazy-member-routing.module app-routing.module.ts list.component.html

如何解决Angular路由器:命名的插座似乎不适用于相对路由,也不适用于延迟加载的模块 lazy-member-routing.module app-routing.module.ts list.component.html

我的角度路由器和命名插座有一些问题。 我有一个成员列表,并且想在命名路由器出口的同一页面上编辑单击的成员。 而且我想让成员在延迟加载的模块中进行处理。

StackBlitz

当模块不是延迟加载时可以使用。 模板如下所示:

<tr *ngFor="let member of members">
...
<a [routerLink]="[''/home/member'',{outlets: {right: [''memberedit'',member._id]}}]"> Edit</a>

和路由数组:

const routes: Routes = [
  {
    path: ''home'',component: HomeComponent,children: [
      {
        path: ''member'',component: MemberComponent,children: [
          {
            path: ''memberlist'',component: MemberListComponent,resolve: { resolvedListData: MemberListResolver },},{
            path: ''memberedit/:id'',component: MemberEditComponent,resolve: { resolvedMember: MemberResolver }
            outlet: ''right'',]
      },]
  },{ path: '''',redirectTo: ''/home'',pathMatch: ''full'' },{ path: ''**'',component: NotFoundComponent }
];

当我尝试删除他对/ home / member的引用以准备延迟加载时,它不再起作用:

我尝试过:

<a [routerLink]="[{outlets: {right: [''memberedit'',member._id]}}]">Edit</a>

这行不通,我认为这是一个已知的错误,路由器在成员列表之后用一个斜杠构造此URL:

/home/member/memberlist/(right:memberedit/5f39748b88457a30e32e909d)

我尝试过:

<a [routerLink]="[''../'',member._id]}}]"> Edit</a>

当我第一次单击成员时,它起作用。并提供以下网址:

/home/member/(memberlist//right:memberedit/5f39748b88457a30e32e909d)

第二次单击另一个成员时,出现此错误:

Error: Two segments cannot have the same outlet name: ''memberedit/5f4227887687e3162d94f5a3'' and ''memberedit/5f39748b88457a30e32e909d''.

URL是:

/home/member/(/(right:memberedit/5f4227887687e3162…f5a3)//right:memberedit/5f39748b88457a30e32e909d)"

我也尝试过:

<a [routerLink]="[{outlets: {primary: [''memberlist''],right: [''memberedit'',member._id]}}]">Edit</a>
<a [routerLink]="[''../'',{outlets: {primary: [''memberlist''],member._id]}}]">Edit</a>

没有运气。

我在延迟加载的模块中尝试使用“完整路径名”。

<a [routerLink]="[''/home/member'',member._id]}}]"> Edit</a>

礼物:

url: "/home/member/(memberlist//right:memberedit/5f39748b88457a30e32e909d)",urlAfterRedirects: "/home/member"

我已经在延迟加载的模块中尝试了以上所有方法。 有什么想法吗?

解决方法

它应该以这种方式与延迟加载的模块一起工作:

lazy-member-routing.module

const routes: Routes = [
    // {
    //     path: '''',component: LazyMemberComponent,//     children: [
            {
                path: ''list'',component: ListComponent,},{
                path: ''edit/:id'',component: EditComponent,outlet: ''lazyright'',//     ]
    // },];

app-routing.module.ts

const routes: Routes = [
  {
    path: ''home'',component: HomeComponent,children: [
      {
        path: ''member'',component: MemberComponent,children: [
          {
            path: ''list'',{
            path: ''edit/:id'',outlet: ''right''
          }
        ]
      },{ 
        path: ''lazymember'',loadChildren: () => import(''./lazy-member/lazy-member.module'').then(m => m.LazyMemberModule),}
    ]
  },{ path: '''',redirectTo: ''home'',pathMatch: ''full''}
];

list.component.html

<a [routerLink]="[''/home/lazymember'',{outlets: {lazyright: [''edit'',member.id]}}]">Edit works - No,no longer</a>

StackBlitz demo


由于Angular Router如何解析导航,因此最初不适用于path: '''',。基本上,对于UrlSegmentGroup的每个子代,它将循环遍历当前路由数组并尝试查找匹配项。

例如,主UrlSegmentGroup如下所示:

{
   children: {
     primary: {
      children: {
         lazyright: {
            children: {},segments: [''edit'',''2'']
         },primary: {
            children: {},segments: [''list''],}
      }
      segments: [''home'',''lazymember'']
     } 
   },segments: []
}

然后

  • ''home''段将匹配path: ''home'',
  • ''lazymember''段将匹配path: ''lazymember'' ...
  • 现在我们有lazyrightprimary,它们都必须在当前路由数组(现在为[{ path: '''',children: [...] }])中找到它们的匹配项;这是它之前不起作用的原因

如果您想了解有关UrlTreeUrlSegmentGroup的更多信息,我已经为此写了article。

我们今天的关于React Routing with Express、Webpack 开发中间件、React 路由器 domreact中路由有哪些常用组件的分享就到这里,谢谢您的阅读,如果想了解更多关于./node_modules/react-router-dom/react-router-dom.js尝试导入错误:未从“ react-router”导出“ Navigate”、645 webpack常用plugins:clean-webpack-plugin,html-webpack-plugin,webpack.DefinePlugin,copy-webpack-plug、angularjs-routing – 使用angular-routing = undefined的ngTable不是一个函数、Angular路由器:命名的插座似乎不适用于相对路由,也不适用于延迟加载的模块 lazy-member-routing.module app-routing.module.ts list.component.html的相关信息,可以在本站进行搜索。

本文标签: