javascript - 将 Angular 路由器渲染 URL 粘贴到浏览器中?
问题描述
我的路由器配置如下:
const routes: Routes = [
{
path: 'topic/:id',
component: TopicComponent,
resolve: { topic: TopicResolverService }
},
{
path: '',
pathMatch: 'full',
component: SummaryCardListComponent
}
]
如果我像这样直接访问一个主题:
http://localhost:4200/topic/concepts%2Fdemand%2Flead-time-demand
它重定向到路径http://localhost:4200/
。
我们需要做什么才能让路由器渲染粘贴到浏览器中的链接?
主题解析器服务如下所示:
@Injectable({
providedIn: 'root'
})
export class TopicResolverService implements Resolve<Topic> {
constructor( private s: StateService ) { }
resolve(
route: ActivatedRouteSnapshot) {
const id = route.paramMap.get('id')
return this.s.loadingTopicStore$.pipe(
switchMap(()=>of(this.s.topicStore.findOneByID(id))
))
}
}
解决方案
如果我decodeURIComponent('concepts%2Fdemand%2Flead-time-demand')
在您的 URI 参数上使用,它应该是一个:id
,它解析为concepts/demand/lead-time-demand
;
现在这使角度路由器感到困惑,它会搜索嵌套路由,例如:
http://localhost:4200/topic/concepts/demand/lead-time-demand
这显然不存在,所以它回退到基本 URL。
从问题作者编辑
我编写了一个Action
合并事件的代码,并在商店加载完成时Observable
不小心包含了Observable
那个触发器。Topic
该操作允许用户选择一个主题切片 ( Concepts
, Formulas
, Guides
...) 并on
从它导航到的用户中进行选择,''
因为这是显示切片的路线。
无论如何,由于将 URL 粘贴到与路由匹配的浏览器会导致应用程序加载,这反过来会导致this.s.loadingTopicStore$
事件触发,并导致路由器导航到''
.
对于那些感兴趣的人,这是动作的设计:
/**
* Note that are always only rendering
* `searchedTopics$` but we also
* track `selectedTopics$` because
* we search within this subset when
* it's selected.
*
* This also resets `topicStore.query`.
*/
onSelectTopicCategory() {
merge(
this.s.loadingTopicStore$,
this.s.activeTopicCategory$).
pipe(untilDestroyed(this)).subscribe(() => {
this.s.selectedTopics$ = combineLatest(
this.s.all$,
this.s.guides$,
this.s.blogs$,
this.s.concepts$,
this.s.formulas$,
this.s.tasks$,
this.s.activeTopicCategory$,
this.s.loadingTopicStore$,
this.onSelectTopicCategoryFunction)
this.s.searchedTopics$ = this.s.selectedTopics$
this.s.topicStore.query = ''
//We have to subscribe to this otherwise
//The combine latest function will never fire.
//The reason is that we are only using
//searchedTopics in the view, so we
//have to fire selectedTopics$ manually.
this.s.selectedTopics$.
pipe(untilDestroyed(this)).
subscribe()
})
}
以及由以下触发的功能merge
:
/**
* Observe the active topic category.
*
* Note that we navigate to '' when a category
* is selected such that we can see the selections
* rendered.
*/
onSelectTopicCategoryFunction(
all,
guides,
blogs,
concepts,
formulas,
tasks,
active,
loading) {
if (loading == false) {
// this.router.navigate([''])
switch (active) {
case TopicCategories.ALL:
return all
case TopicCategories.GUIDES:
return guides
case TopicCategories.BLOGS:
return blogs
case TopicCategories.CONCEPTS:
return concepts
case TopicCategories.FORMULAS:
return formulas
case TopicCategories.TASKS:
return tasks
default:
return all
}
}
else return []
}
它通过以下方式实现@fireflysemantics/slice
:
推荐阅读
- vue.js - 如何在我的 Vuepress 项目中引用自定义 Javascript 文件,以便 JS 函数在全球范围内可用?
- ios - SpeechRecognizer 后视频不播放
- python - 如何在将值四等分的同时将矩阵的维度加倍?
- linux - 在多个目录中创建多个文件
- java - Spring Cloud Dataflow Kubernetes 从 dockerfile 获取 jar 的属性
- python - lxml 错误:lxml.etree.XPathEvalError:带有后代的无效表达式
- html - 如何使所有表格在 DIV 中响应
- pyspark - 在 Pyspark df 中添加字典键作为列名和字典值作为该列的常量值
- c++ - 迭代器特征中没有名为 value_type 的类型
- javascript - 使用角度形式更改表单元素