简体   繁体   English

如何在 nextjs 中使用动态 url

[英]How to use dynamic url in nextjs

I am working with nextjs,I am fetching image(via api),Right now i am getting "Full image path"(for example,"https//myurl.com/image/imagename.jpg") so i am fetching without any problem,but i want to know that if i have only "image name"(for example,"imagename.jpg") so how can we fetch in front end ?我正在使用 nextjs,我正在获取图像(通过 api),现在我正在获取“完整图像路径”(例如,“https//myurl.com/image/imagename.jpg”)所以我没有任何获取问题,但我想知道如果我只有“图像名称”(例如,“imagename.jpg”),那么我们如何在前端获取? in other words how can i pass "url/hostname" before imagename ?换句话说,我如何在 imagename 之前传递“url/hostname”? Should i create seprate "component" and mention "host url" and fetch whereever i want OR Should i create "variable" like following code or is there another way ?我应该创建单独的“组件”并提及“主机 url”并获取我想要的任何地方或者我应该像下面的代码一样创建“变量”还是有其他方法?

class Company extends React.Component {
    const orig = 'http://anyurl.com';

    state = {
        trending: []
      }
    componentDidMount() {
        axios.get('https://myurl.com/apipath',
        )
          .then(res => {
            const trending = res.data;
            this.setState({ trending });
          })
      }

    render() {
            return 
                (

                    <img src={post.image} /> 
                );      
            );
        }

If you know the base URL, you can use Template Literals for the same.如果您知道基本 URL,则可以使用相同的模板文字 For Example:例如:

const orig = 'https://myurl.com/apipath';
const imageName = 'image1.jpg';
const imgUrl = `${orig}/${imageName}`

Make a separate Variable and add them like this ${} in string literal.创建一个单独的变量并将它们像这样 ${} 添加到字符串文字中。 then you will use dynamic fetching.那么您将使用动态提取。

You can create a variable baseURL that contains a constant path to your backend URL.您可以创建一个变量baseURL ,其中包含指向后端 URL 的恒定路径。 This would be static.这将是静态的。 The only dynamic part is your imageFileName.唯一的动态部分是您的 imageFileName。 You can store this in state or get it from props or where ever you are getting the imagename.jpg from.您可以将其存储在 state 中或从 props 或从哪里获取imagename.jpg获取。

When you want to define some constant values like style or image URLs then it's always better to define that outside of component.当您想定义一些常量值(例如样式或图像 URL)时,最好在组件之外定义它。 It will become global values and available inside each function/class of that file.它将成为全局值并在该文件的每个函数/类中可用。

config/urlConfig.js配置/urlConfig.js

export const urlConfig={
    baseURL: 'http://myurl.com/image/'
}

//Your component //你的组件

import { urlConfig } from 'path/to/your/config/urlConfig';
class Company extends React.Component {
    state = {
        trending: [],
        imageFileName: 'imagename.jpg'   // Assign any value to this
      }
    componentDidMount() {
        axios.get(urlConfig.baseURL + this.state.imageFileName,
        )
          .then(res => {
            const trending = res.data;
            this.setState({ trending });
          })
      }

    render() {
            return 
                (

                    <img src={post.image} /> 
                );      
            );
        }

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM