“React, ‘product/:id’ 页面导致组件资产从错误路径加载(错误路径)”

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

React , "product/:id" page causes components assets to load from incorrect path(wrong path)

问题

I'm creating a Product landing page (single product page) with React, but when loading the navbar component, it causes the component's assets to load from the wrong path. For example, the product page path is (/product/:id), but the navbar component will go to (/product/:id/assist name) to get the assist, which will not load the assist because that is not the right path for the assets. The assets exist in the (./assist name).

app.js file

function App() {
  return (
    <Router>
      <Container>
        <Routes>
          <Route path="/" element={<Home />} />
          <Route path="/products/:id" element={<SingleProductPage />} />
        </Routes>
      </Container>
    </Router>
  );
}

Single product page that has the (/product/:id) path

function SingleProductPage() {
  return (
    <div id="single-Container">
      <Navbar/>
      single page
    </div>
  );
}

The navbar component

<div id="Navbar-SearchIcon">
  <img src="../searchIcon.png" alt="" />
</div>

<div id="Navbar-BasketButton">
  <img src="./basket-icon.png" alt="" />
</div>

When I'm on (/) path or any other path

When I'm on (/product/:id) path

How the navbar should look like

How the navbar looks like on the product/:id page

英文:

im creating a Product linding page (single product page) with react but when loading the navbar component

it couses the componint's assists to load from wrong path for examble product page path is (/product/:id)
the navbar component will go (/product/:id/assist name ) to get the assist witch will not load the assist
because that is not the right path for the asities.
the exists on the (./assist name )

app.js file

function App() {
  return (
    &lt;Router&gt;
      &lt;Container&gt;
        &lt;Routes&gt;
          &lt;Route path=&quot;/&quot;  element={&lt;Home /&gt;} /&gt;     
          &lt;Route path=&quot;/products/:id&quot; element= {&lt;SingleProductPage/&gt;} /&gt;
        &lt;/Routes&gt;
      &lt;/Container&gt;
    &lt;/Router&gt;
  );
}

single product page
that has's the (/product/:id) path

function SingleProductPage() {

 
  
  return (
    

    &lt;div id=&quot;single-Container&quot;&gt;
      
      &lt;Navbar/&gt;
     
      single page

    &lt;/div&gt;
    



    )
}

the navbar component

 &lt;div id=&quot;Navbar-SearchIcon&quot; &gt;
    &lt;img src=&quot;../searchIcon.png&quot; alt=&quot;&quot; /&gt;


&lt;div id=&quot;Navbar-BasketButton&quot; &gt;
       &lt;img src=&quot;./basket-icon.png&quot; alt=&quot;&quot; /&gt;
          &lt;/div&gt;

when im on (/) path or any other path

when im on (/product/:id) path

how the navbar should look like

how the navbar look like on product/:id page

答案1

得分: 0

图片资源使用相对路径而不是PUBLIC目录中的绝对路径。当您进入子路由时,使用相对路径时,资源的路径将与当前URL路径不同。

绝对路径将以"/"开头,这是公共目录。这里是一个示例:

<div id="Navbar-SearchIcon" >
  <img src="/images/searchIcon.png" alt="" />
</div>

<div id="Navbar-BasketButton" >
  <img src="/images/basket-icon.png" alt="" />
</div>
/public
  -/images
    -searchIcon.png
    -basket-icon.png
    ....
  ....
/src
  ....
英文:

The image sources are using relative paths instead of absolute paths in the PUBLIC directory. When you navigate into a sub-route the path to assets will be different when using relative paths, they will be relative from the current URL pathname.

Absolute paths will start with &quot;/&quot; which is the public directory. Here's an Example:

&lt;div id=&quot;Navbar-SearchIcon&quot; &gt;
  &lt;img src=&quot;/images/searchIcon.png&quot; alt=&quot;&quot; /&gt;
&lt;/div&gt;

&lt;div id=&quot;Navbar-BasketButton&quot; &gt;
  &lt;img src=&quot;/images/basket-icon.png&quot; alt=&quot;&quot; /&gt;
&lt;/div&gt;
/public
  -/images
    -searchIcon.png
    -basket-icon.png
    ....
  ....
/src
  ....

huangapple
  • 本文由 发表于 2023年2月24日 04:57:29
  • 转载请务必保留本文链接:https://go.coder-hub.com/75550248.html
匿名

发表评论

匿名网友

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

确定