reactjs 相关问题

React是一个用于构建用户界面的JavaScript库。它使用声明范式,旨在既高效又灵活。



如何在React项目中更改生产构建静态文件夹路径?

npm run build <p><code>index.html</code><strong> </strong>您可以看到,路径集为<pre><code>&lt;script src=&#34;./static/js/5.a4bfdba9.chunk.js&#34;&gt;&lt;/script&gt; </code></pre><pre> </pre>,但我希望将路径设置为</p><code>./static/js/</code><pre></pre> <p>我无法弄清楚在何处或何处可以确保每次运行构建时,它都会采用我指定的路径而不是默认路径?<pre></pre> </p><p>注:<pre>我在</pre><code>./static/dashboard/js/5.a4bfdba9.chunk.js</code></p>中查看<p><code>&#34;homepage&#34;: &#34;.&#34;</code></p>属性,但是更改此属性只会在<p><code>package.json</code><strong></strong>之前附加一条路径 <pre> </pre> today有一个更好的解决方案。来自<pre>hashRocket</pre>:<pre> </pre>当我构建CRA应用程序时,我会为以 /静态开头的资产(CS,图像)提供一条路径。如果我将应用程序部署到</p><code>/static/js/</code></question>,那么该应用程序将尝试访问<answer tick="false" vote="15"><code>https://something.com/myapp</code><p>处的这些资产路径。那不是资产所居住的地方。资产居住在<a href="https://til.hashrocket.com/posts/xtgpx9kssz-set-the-relative-path-of-assets-in-a-cra-app" rel="noreferrer"><code>https://something.com/static/asset.css</code></a>。 Create React应用程序允许您更改A内置资产的前缀,该资产具有</p><code>https://something.com/myapp/static/asset.css</code><p>文件中的主页属性。 您可以将其设置为MyApp:<pre> </pre><code>package.json</code><pre> 然后资产将具有</pre><code>&#34;homepage&#34;: &#34;/myapp&#34; </code><pre></pre>的路径 </p> <p>在我的情况下,我对我的<pre><code>/myapp/static/asset.css</code></pre>进行了以下更改:</p> <p><code>package.json</code></p> <pre>这是因为我有一个nginx反向代理,可服务于另一个端点,因此我所有的静态资产都必须从</pre><code> &#34;homepage&#34;: &#34;/search&#34;, &#34;build&#34;: &#34;react-scripts build &amp;&amp; mkdir build/search &amp;&amp; mv build/static build/search/static&#34; </code><p>提供。 <pre> </pre> </p><hr/>update:<p><pre> 您将需要更新WebPack配置以实现此目的。有多种方法:</pre> </p> <pre>反应式弹出(不建议)</pre> <p>-patch-package<pre> </pre>反理申请 - 涂层 <pre> 我正在为</pre><code>/search</code></p>。 您需要更改React-Scripts软件包的文件</answer><code>/search/static</code><answer tick="true" vote="8">文件。这是您需要做出的一些更改的差异:<p> <strong><code>patch-package</code></strong> </p>现在,如果您对<p><code>config/webpack.config.js</code></p>进行这些更改,则将输出的文件输出到所需的文件夹和index.html也将具有正确的路径。 但是,如果您安装/卸载任何软件包,则Node_modules的更改将被覆盖。为了坚持这些更改,您可以使用<ol>Patch-package<li>,它将在安装后写下您的node_modules的更改。 </li>这里是设置补丁包装的步骤,您可以阅读其<li>readme文件</li>有关更多详细信息:<li> </li> </ol><p><pre><code>diff --git a/node_modules/react-scripts/config/webpack.config.js b/node_modules/react-scripts/config/webpack.config.js index 26c2a65..ad29fbd 100644 --- a/node_modules/react-scripts/config/webpack.config.js +++ b/node_modules/react-scripts/config/webpack.config.js @@ -212,13 +212,13 @@ module.exports = function (webpackEnv) { // There will be one main bundle, and one file per asynchronous chunk. // In development, it does not produce real files. filename: isEnvProduction - ? &#39;static/js/[name].[contenthash:8].js&#39; + ? &#39;static/dashboard/js/[name].[contenthash:8].js&#39; : isEnvDevelopment &amp;&amp; &#39;static/js/bundle.js&#39;, // TODO: remove this when upgrading to webpack 5 futureEmitAssets: true, // There are also additional JS chunk files if you use code splitting. chunkFilename: isEnvProduction - ? &#39;static/js/[name].[contenthash:8].chunk.js&#39; + ? &#39;static/dashboard/js/[name].[contenthash:8].chunk.js&#39; : isEnvDevelopment &amp;&amp; &#39;static/js/[name].chunk.js&#39;, // webpack uses `publicPath` to determine where the app is being served from. // It requires a trailing slash, or the file assets will get an incorrect path. @@ -676,8 +676,8 @@ module.exports = function (webpackEnv) { new MiniCssExtractPlugin({ // Options similar to the same options in webpackOptions.output // both options are optional - filename: &#39;static/css/[name].[contenthash:8].css&#39;, - chunkFilename: &#39;static/css/[name].[contenthash:8].chunk.css&#39;, + filename: &#39;static/dashboard/css/[name].[contenthash:8].css&#39;, + chunkFilename: &#39;static/dashboard/css/[name].[contenthash:8].chunk.css&#39;, }), // Generate an asset manifest file with the following content: // - &#34;files&#34; key: Mapping of all asset filenames to their corresponding </code></pre><br/> <pre> </pre></p>在package.json的脚本部分中添加了此内容:<pre> </pre><code>node_modules/react-scripts/config/webpack.config.js</code><p><pre> </pre> <br/><a href="https://www.npmjs.com/package/patch-package" rel="noreferrer">run patch-ackage以创建.patch文件:</a> </p><code>yarn add patch-package postinstall-postinstall</code><p><a href="https://github.com/ds300/patch-package#readme" rel="noreferrer"> </a> </p><ol>召集补丁文件以与您的团队共享修复程序<li> <p><code>&#34;postinstall&#34;: &#34;patch-package&#34;</code><pre></pre> </p><code>npx patch-package some-package</code></li><li> <p> <br/> <pre>我也创建了一个</pre>库存储库</p>供您参考。 </li><li>答案:<p><br/> <pre></pre>战争:</p>不适用于移动构建文件夹的内容。只能移动整个构建文件夹。例如:</li><code>git add patches/react-scripts+4.0.3.patch</code><li><p> <br/>改变创建React应用程序的设置,以便将其输出到不同文件夹将非常复杂。但是您可以在构建完成后移动文件,这很简单。 <pre>在您的包裹中。 </pre><code>git commit -m &#34;change output dir structure in react-scripts&#34;</code><br/> <pre>您可以添加另一个称为the build后进行操作的脚本,并将由NPM/YARN自动调用。 </pre><code>mv build/ dist/</code></p> </li>该文件应将文件移至其他文件夹。 </ol> <p> <a href="https://github.com/vaibhavhrt/cra-custom-build-dir-structure" rel="noreferrer">1-纱线添加 @craco/craco 2-创建craco.config.js文件 3 craco.config.js = </a><code>&#34;scripts&#34;: { &#34;build&#34;: &#34;react-scripts build&#34; </code></p>$ {staticfileName}/js/[name]。[contentHash].chunk.js<p><code>postbuild</code><strong>$ {staticfilename}/js/[name] </strong><code>&#34;postbuild&#34;: &#34;mv build/ dist/&#34; </code></p> <p>};`<strong> </strong> <pre> </pre>中其他任何人都遇到了类似的问题。我的问题是,项目名称已被置于静态路径上。 </p>路径是<p><code>module.exports = { webpack: { configure: (webpackConfig) =&gt; { webpackConfig.output.chunkFilename = </code></p>而不是<p><code>; webpackConfig.output.filename = </code></p><pre> </pre>关注解决方案,只要我遇到了一个注释,即从主页推断出“公共路径”。在我的主页URL中包括项目名称。 <p><pre>wrong</pre> “主页”:“ https:// {url} .io/project-name”</p> <pre></pre>纠正 “主页”:“ https:// {url} .io/project-name”<p> 从此URL中删除项目名称解决了问题。 </p> </answer><answer tick="false" vote="0">

回答 0 投票 0

如何通过url reactjs

存储在变量中的日期,现在如何在URL

回答 3 投票 0

自动登录无需提示登录页面之后,请在Next-authoauth

// src/app/api/auth/[...nextauth]/route.ts import NextAuth from "next-auth"; import { LinkedInProvider } from "next-auth/providers/linkedin"; const handler = NextAuth({ providers: [ LinkedInProvider({ clientId: process.env.LINKEDIN_CLIENT_ID || "", clientSecret: process.env.LINKEDIN_CLIENT_SECRET || "", client: { token_endpoint_auth_method: "client_secret_post" }, issuer: "https://www.linkedin.com", profile: (profile: LinkedInProfile) => ({ id: profile.sub, name: profile.name, email: profile.email, image: profile.picture, }), wellKnown: "https://www.linkedin.com/oauth/.well-known/openid-configuration", authorization: { params: { scope: "openid profile email", }, }, }), ], callbacks: { async jwt({ token, account, profile }) { if (account) { token.accessToken = account.access_token; } if (profile) { token.id = profile.sub; } return token; }, async session({ session, token }) { session.accessToken = token.accessToken as string; session.user.id = token.id as string; return session; }, }, }); export { handler as GET, handler as POST };

回答 1 投票 0

用auth0和react

我似乎无法检索用户在我的前端中的角色。令牌索赔日志没有给我任何信息,这些信息是login触发器的工作,并且log的角色使我不确定。 我试图用...

回答 1 投票 0

如何在React/Electron应用中实现文件夹的Dropzone并处理文件夹内容?

基于电子的应用程序有一种方法可以让用户将文件夹放在反应组件上并处理掉落事件,以便主过程可以完全访问并递归扫描文件夹的con ...

回答 1 投票 0

const ref = useref(null) const {scrollyprogress} = usecroll({{ 目标:参考, 偏移:['0%','0%50%'] })) const scale = usetransform(scrollyprogress,[1,0],[0.9,1]) 返回 ( ...

我正在尝试构建一个组件,该组件将元素缩放为用户滚动。这只是微妙的 当组件在“折叠上方”时,我遇到了问题。动画“跳跃”一旦您开始滚动,检查Framer的直列样式表明,页面上的加载元素的比例为

回答 1 投票 0

如何在Agular JS Project中使用静态文件,例如图像,视频和字体文件

我有一个简单的html项目,我想在Angular Js中转换该项目,请告诉我如何在项目中以及JS文件中使用所有不同的CSS文件,还告诉我如何使用S ...

回答 1 投票 0

优化的React服务器组件以提高水合性能?

回答服务器组件(RSC)进行实验,并在服务器和客户端之间传递大型数据集时注意到水合瓶颈。我了解RSC应该最大程度地减少客户端的JavaScript,但是有哪些实用方法可以优化水合性能并降低有效载荷尺寸?

回答 1 投票 0

在react/nextjs

创建全天移动的跨度元素,具体取决于当前元素应为x%的父级div的时间。 相关代码的片段: const [TimePecentage,settimeperc ...

回答 1 投票 0







我正在尝试使用(我认为)使用

[{productName, amountSold}, ...]

回答 1 投票 0

如何在反应虚拟化的可扩展面板内虚拟化行? 我正在使用虚拟化的反应来渲染使用可扩展面板的列表。问题在于,当面板扩展时,它内部的所有500多行都会立即加载,使UI非常慢。 外部列表是

我正在使用虚拟化的反应来渲染使用可扩展面板的列表。问题是,当面板扩展时,它内部的所有500多行都会立即加载,使UI非常慢。

回答 0 投票 0

我在实施路线方面有一些问题。 我的情况是我有一个组件,该组件内的内容是从服务器中的JSON数据中获取的。在内容中有一些

import React, {Component} from 'react' ............. other imports .... const htmlContent=`<div> <p> Lorem ipsum content goes here </p> <a href="/home">Home</a> </div>`; export class InnerPage extends Component{ ...... other stuffs ....... render(){ return( <div> // static content <div dangerouslySetInnerHTML={{__html: htmlContent}} /> </div> ) } } 在此处,它可以按预期呈现HTML内容,但是该路由无法正常工作。任何帮助将不胜感激。 最佳处理方法是将事件侦听器添加到HTML内容上的锚点元素。 在单击事件上,获取HREF值,然后使用该值进行路线更改。 样本代码 <div> // static content <div ref={dynamic_content} dangerouslySetInnerHTML={{__html: htmlContent}} /> </div> Function: const handleAnchorClick=(event)=>{ event.preventDefault(); const path = event.target.href; //Put the redirect here with react route } //Add this in the useEffect hook once the content is loaded useEffect(()=>{ if(dynamic_content.current){ dynamic_content.current.getElementByTag('a').addEventListener('click', handleAnchorClick) } //Remove the eventlistener on component unmount },[htmlContent])

回答 0 投票 0

最新问题
© www.soinside.com 2019 - 2025. All rights reserved.