[英]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.