我用RxJs 6运行角形6。
我试图从paramMap中获取值,并将其分配给一个属性。路线如下:
{
path: 'post/:postName',
component: PostComponent
}post组件当前如下所示:
import { Component, OnInit, ViewEncapsulation } from '@angular/core';
import { ActivatedRoute, ParamMap } from '@angular/router';
import { switchMap } from 'rxjs/operators';
import { Observable, of } from 'rxjs';
import * as matter from 'yaml-front-matter';
import { Post } from '../../models/post';
@Component({
selector: 'blog-post',
templateUrl: './post.component.html',
styleUrls: ['./post.component.css'],
encapsulation: ViewEncapsulation.Emulated
})
export class PostComponent implements OnInit {
created: Date;
postName = '';
markdown = '';
title = '';
loading = true;
author = '';
constructor(private route: ActivatedRoute) { }
async ngOnInit () {
this.route.paramMap.pipe(
switchMap((params: ParamMap) =>
of(params.get('postName'))
)
).subscribe(d => this.postName = d);
const file = await fetch(`/_posts/${this.postName}.md`);
const text = await file.text();
const content = text.trim();
this.parseFrontMatter(content);
}
parseFrontMatter (content: string) {
const frontMatter: Post = matter.loadFront(content);
this.title = frontMatter.title;
this.author = frontMatter.author;
this.created = frontMatter.created;
this.markdown = frontMatter.__content;
this.loading = false;
}
}在ngOnInit中是从paramMap中获取值的代码。除非尝试使用不同的参数路由回post组件,否则此代码可以工作。
这可以在Devbin.io上观察到。
完整的源代码可以在GitHub上找到。
所有的文档和其他堆栈溢出答案总是假设您想要在switchMap函数中调用一个服务。正式文档是这里
我的问题是,如何获得路由参数并将其分配给属性,并在导航回同一路由时保持导航工作?
发布于 2018-06-05 18:46:20
这是因为当您路由到同一个组件时,不会调用ngOnInit。在您的例子中,/post/Angular-6-Router-How-To-Get-Route-Parameters和/post/ your触发了相同的组件: post.component。
因此,要完成这项工作,您可以在paramMap.pipe中调用订阅内部的函数。
你可以这样做:
ngOnInit() {
this.route.paramMap.pipe(
switchMap((params: ParamMap) =>
of(params.get('postName'))
)
).subscribe((d) => {
this.postName = d;
this.postInit();
});
}
async postInit() {
const file = await fetch(`/_posts/${this.postName}.md`);
const text = await file.text();
const content = text.trim();
this.parseFrontMatter(content);
}
parseFrontMatter(content: string) {
const frontMatter: Post = matter.loadFront(content);
this.title = frontMatter.title;
this.author = frontMatter.author;
this.created = frontMatter.created;
this.markdown = frontMatter.__content;
this.loading = false;
}发布于 2018-06-05 18:44:13
所以我克隆了回购并修复了这个问题。
代之以:
async ngOnInit () {
this.route.paramMap.pipe(
switchMap((params: ParamMap) =>
of(params.get('postName'))
)
).subscribe(d => this.postName = d);
const file = await fetch(`/_posts/${this.postName}.md`);
const text = await file.text();
const content = text.trim();
this.parseFrontMatter(content);
}有以下几点:
ngOnInit() {
this.route.paramMap.pipe(
switchMap((params: ParamMap) =>
of(params.get('postName'))
)
).subscribe(d => {
this.postName = d;
fetch(`/_posts/${this.postName}.md`)
.then(data => data.text()
.then(text => {
const content = text.trim();
this.parseFrontMatter(content);
}));
});
}说明:只有在组件初始化时才会获取这些帖子,因为您的文件提取代码在可观察的订阅方法之外。我进入并链接了承诺,然后将最后的值传递给函数parseFrontMatter。
https://stackoverflow.com/questions/50705214
复制相似问题