[英]Path aliases to paths in angular
I'm trying to make URL aliases in an angular 6 application. 我正在尝试在angular 6应用程序中创建URL别名。 The problem is the following: 问题如下:
I have a URL in my angular app defined as article/:id
, that maps to a component called ArticleViewComponent
, but, the thing is I'm trying to make a different url where I can send the title of the article as the path for the article. 我在我的角度应用程序中有一个URL定义为article/:id
,该URL映射到名为ArticleViewComponent
的组件,但是问题是我试图创建一个不同的url,可以在其中将文章标题发送为文章。 For example, if I have an article called Article about angular
with id 1
, I want the URL to be /article/article-about-angular
and still map it to the article with id 1
(because the id is what the API handles, not the title). 例如,如果我有一篇名为ID为1
Article about angular
的文章,我希望该URL为/article/article-about-angular
,但仍将其映射到ID为1
的文章(因为ID是API处理的,不是标题)。 Thanks in advance for your help, I know this seems impossible. 在此先感谢您的帮助,我知道这似乎是不可能的。
You can have them both map to the same component using article/:id
as you are already using. 您可以使用已经使用的article/:id
将它们都映射到相同的组件。 Instead of just being a numeric id, this will now contain either an article title or a numeric id. 现在,它不仅包含数字 ID,还包含文章标题或数字ID。 Then when fetching the article, simply handle it differently based on whether the id is a number or a string: 然后,在获取文章时,只需根据id是数字还是字符串来进行不同处理即可:
// Check if id is "not a number"
if(isNaN(id)) {
// Define a function to convert from titles to id numbers
var idNumber = getIdNumberFromTitle(id);
getArticleFromTitleName(idNumber);
} else {
getArticleFromIdNumber(id);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.