【问题标题】:what is the type of URL query parameter in typescript?打字稿中 URL 查询参数的类型是什么?
【发布时间】:2022-11-04 20:58:31
【问题描述】:

我需要从 Vue3 中的 URL 获取选举 id,以便在稍后的调用中将其用作 axios 调用中的查询参数。我的网址是“http://localhost:8080/#/requestPIN/py6lzerjwjntl4dmr9n77e6x73blk925?electionId=thirdElection”

我正在尝试做:

get ID(): string | undefined {
return this.$route.query.electionId

}

这给了我一个打字稿错误:

Type 'LocationQueryValue | LocationQueryValue[]' is not assignable to type 'string | undefined'. Type 'null' is not assignable to type 'string | undefined'.

URL 查询参数的正确类型是什么?

【问题讨论】:

    标签: typescript url vuejs3 query-parameters typescript-eslint


    【解决方案1】:

    使用nullish coalescing 运算符,您可以在表达式为nullundefined 的情况下“回退到”undefined

    get ID(): string | undefined {
        return this.$route.query.electionId ?? undefined;
    }
    

    【讨论】:

      猜你喜欢
      • 2016-11-06
      • 2019-01-10
      • 2022-08-13
      • 1970-01-01
      • 1970-01-01
      • 2021-10-27
      • 2022-01-19
      • 2019-01-26
      • 1970-01-01
      相关资源
      最近更新 更多