English 中文(简体)
模版许可证持有人?
原标题:Type placeholder in Template Literals?

因此,我试图这样做:

    if(location.href == `${base}/tours`) {
      console.log("in list")
    } else if (location.href == `${base}/tours/${string}`) {
      console.log("in detail");
    }

我犯了这一错误:

<代码>仅指一种类型,但此处用作一种数值。

。 我只是想成为任何扼杀的避风港。

我很想知道,在模板字面上是否甚至有可能有作为股东的一类? 在第二个<条码>中 www.un.org/Depts/DGACM/index_chinese.htm

问题回答

模板字面只能形成一种单一的、具体的示意图。 他们不能表达“任何扼杀”的概念。

相反,使用<条码>启示/代码:

if (location.href.startsWith(`${base}/tours/`)) {
  console.log("in detail");
}

或者使用定期表述:

const pattern = new RegExp(`^${base}/tours/(.*)`);
const result = pattern.exec(location.href);
if (result) {
  console.log("in detail");
  // The part matched by `(.*)` will also be available as `result[1]`
}

Stepping back, the cleanest solution would be to use a router library that handles these URLs for you. For example, in React you can use react-router.

In typescript, you can just use it like

type Route = `/tours/${string}`;

之后

// Should succeed:

goToRoute("/tours/users");
goToRoute("/tours/");
goToRoute("/tours/admin/users");

// Should error:

// @ts-expect-error
goToRoute("users/1");
// @ts-expect-error
goToRoute("http://facebook.com");

As you noticed this will replace ${string} with whatever your string is but it has to start with the /.

我刚刚学到这段话,在“总书”课程之后,你可以从<>Matt Pocock本人的here获得更多详情。

另外,从 官方TS Docs/a>获取更多信息。





相关问题
selected text in iframe

How to get a selected text inside a iframe. I my page i m having a iframe which is editable true. So how can i get the selected text in that iframe.

How to fire event handlers on the link using javascript

I would like to click a link in my page using javascript. I would like to Fire event handlers on the link without navigating. How can this be done? This has to work both in firefox and Internet ...

How to Add script codes before the </body> tag ASP.NET

Heres the problem, In Masterpage, the google analytics code were pasted before the end of body tag. In ASPX page, I need to generate a script (google addItem tracker) using codebehind ClientScript ...

Clipboard access using Javascript - sans Flash?

Is there a reliable way to access the client machine s clipboard using Javascript? I continue to run into permissions issues when attempting to do this. How does Google Docs do this? Do they use ...

javascript debugging question

I have a large javascript which I didn t write but I need to use it and I m slowely going trough it trying to figure out what does it do and how, I m using alert to print out what it does but now I ...

Parsing date like twitter

I ve made a little forum and I want parse the date on newest posts like twitter, you know "posted 40 minutes ago ","posted 1 hour ago"... What s the best way ? Thanx.

热门标签