简体   繁体   English

如何将 Public 文件夹中的图像/内容加载到 NextJs 中的动态路由页面?

[英]How do I load images/content from the Public folder into a dynamic routed page in NextJs?

How do I load an image from the Public folder into a dynamic route in NextJs?如何将 Public 文件夹中的图像加载到 NextJs 中的动态路由中?

In the docs I saw nothing about this... It is stated that we can add content from the Public file - for example <img src="/Content/image.jpg" alt="some alt" /> .在文档中,我什么也没看到......据说我们可以从公共文件中添加内容 - 例如<img src="/Content/image.jpg" alt="some alt" /> Yet, if I am in a dynamic routed page, for example /pages/post/[id].js The solution presented in Next's docs does not work, nothing is loaded.然而,如果我在一个动态路由页面中,例如/pages/post/[id].js Next's docs 中提供的解决方案不起作用,没有加载任何内容。

How is this tackled, best practices, etc?这是如何解决的,最佳实践等?

All assets that are in the public folder will be available like any resource on the internet. public文件夹中的所有资产都将像 Internet 上的任何资源一样可用。

It is probably a matter of path, if you serving your app from example.com , and your images has /Content/image.jpg (pay attention to the first slash, this tells to the browser to start at the root ) in this case, the absolute path is relative to the domain and not to the path that the page is served at.这可能是路径问题,如果您从example.com为您的应用程序提供服务,并且您的图像具有/Content/image.jpg (注意第一个斜杠,这告诉浏览器从开始)在这种情况下,绝对路径是相对于域的,而不是相对于提供页面的路径。

This means that there is no affect of dynamic routes on the images path.这意味着动态路由对图像路径没有影响。

Just make sure that your public folder has Content (make sure that the folder starts with capital C ) folder.只要确保你的public文件夹中有Content (请确保该文件夹开始与资本C )文件夹中。

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

相关问题 如何从另一个页面加载 a.php 页面并加载动态内容 - How do I load a .php page from another page and load dynamic content 如何从NextJS的公共文件夹中获取()? - How to fetch() from public folder in NextJS? NextJS:如何让这个组件在页面加载时显示宽度? - NextJS: How do I get this component to display width on page load? 如何访问我的 React 项目的公共文件夹中的图像? - How do I access images in the public folder of my React project? 如何在jsp页面上将Web内容文件夹中的图像显示为缩略图 - How to display images as thumbnails from web content folder on a jsp page 如何使用vue-gallery从文件夹加载图像? - How do I load images from a folder using vue-gallery? 如何检测何时从动态页面加载中加载所有图像? - How to detect when all images are loaded from dynamic page load? 如何在 NextJS+React 中使用 api 生成的内容创建用户可自定义的动态 url? - How do I create user-customizable dynamic url with api-generated content in NextJS+React? 我如何使用相关的 jquery 函数将内容从另一个页面加载到页面中,使其工作(滑块) - how do i load content into a page from another page with the associated jquery function that makes it work (slider) 如何从网页浏览文件夹并在页面上显示其内容? - How do i browse from a webpage for a folder and display its content to the page?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM