【问题标题】:add const to a string?将 const 添加到字符串?
【发布时间】:2018-10-03 07:11:28
【问题描述】:

我刚开始学习 React Native 遇到了问题。

我从我的服务器获取一个 php 文件,需要在 url 中添加一个 ID 参数。 ID 参数保存在 const 中。

如何将我的 const 添加到 fetch url 的末尾?

const Round = 1;

return fetch('http://myurl/data.php?ID=')

【问题讨论】:

标签: javascript


【解决方案1】:

您只需要连接Round 变量。

const Round = 1;
return fetch('http://myurl/data.php?ID='+Round);

您也可以使用Template literals

return fetch(`http://myurl/data.php?ID=${Round}`);

【讨论】:

    【解决方案2】:

    使用template literals:

    const round = 1;
    return fetch(`http://myurl/data.php?ID=${round}`);
    

    注意:按照惯例,常规变量的名称应为小写。

    【讨论】:

      【解决方案3】:

      只要把 const 变量放在最后

      例如:

      const Round = 1;
      
      return fetch('http://myurl/data.php?ID='+Round)
      

      【讨论】:

        猜你喜欢
        • 2022-01-10
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2010-12-11
        • 1970-01-01
        • 2017-03-10
        • 1970-01-01
        相关资源
        最近更新 更多