如何在 Typescript 中连接字符串和数字

新手上路,请多包涵

我正在使用方法来获取数据

function date() {
    let str = '';

    const currentTime = new Date();
    const year = currentTime.getFullYear();
    const month = currentTime.getMonth();
    const day = currentTime.getDate();

    const hours = currentTime.getHours();
    let minutes = currentTime.getMinutes();
    let seconds = currentTime.getSeconds();
    if (month < 10) {
        //month = '0' + month;
    }
    if (minutes < 10) {
        //minutes = '0' + minutes;
    }
    if (seconds < 10) {
        //seconds = '0' + seconds;
    }
    str += year + '-' + month + '-' + day + ' ' + hours + ':' + minutes + ':' + seconds + ' ';

    console.log(str);
}

作为输出我得到

2017-6-13 20:36:6

我想得到同样的东西,但喜欢

2017-06-13 20:36:06

但是,如果我尝试其中一条,我注释掉了,例如这一行

month = '0' + month;

我收到错误

Argument of type 'string' is not assignable to parameter of type 'number'.

我怎么能连接字符串和数字?

原文由 Anna F 发布,翻译遵循 CC BY-SA 4.0 许可协议

阅读 915
1 个回答

你可以像这样使用它。 角度环境.ts

 const URL = "http://127.0.0.1";
const PORT = "8080";
const API_version = "/api/v1/";

export const environment = {
    production: false,
    BASE_URL: `${URL}:${PORT}${API_version}`
};

原文由 Bercove 发布,翻译遵循 CC BY-SA 4.0 许可协议

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题