Javascript 角度2路线选项

Javascript 角度2路线选项,javascript,angular,routes,angular-ui-router,Javascript,Angular,Routes,Angular Ui Router,我是Angular和Node JS的新手,目前正在开发一个应用程序,在决定如何在不同组件之间进行最佳导航时,我遇到了一些问题 单击一本书的名称后,我希望导航到与该书关联的组件,专门提取该条目的信息,例如注释 使用Angular 2 MEAN stack,我想了解我应该如何解决这样一个问题——我应该存储任何特定的值,以便更容易地导航和从数据库中提取有关书籍的信息 <article class="panel panel-default"> <div class="pane

我是Angular和Node JS的新手,目前正在开发一个应用程序,在决定如何在不同组件之间进行最佳导航时,我遇到了一些问题

单击一本书的名称后,我希望导航到与该书关联的组件,专门提取该条目的信息,例如注释

使用Angular 2 MEAN stack,我想了解我应该如何解决这样一个问题——我应该存储任何特定的值,以便更容易地导航和从数据库中提取有关书籍的信息

<article class="panel panel-default">
     <div class="panel-body">
            <div class="author">
                 {{ author.fullName }}
            </div>
            <div class="addbook">
                <book-input [author]='author'></book-input>
            </div>
    </div>
    <footer class="panel-footer">
        <div>
            <ul *ngFor="let book of author.books"> 
                <li><a [routerLink]="['book']">{{ book }}</a></li>
            </ul>
        </div> 
    </footer>
</article>

如果您有任何建议,我们将不胜感激。

我这样做的方式是在路由器链接的末尾添加一个id,这样它就可以路由到类似book/thebookid的内容

然后在路由模块中添加如下内容:

{path: 'book/:id, component: BookComponent}
然后在book组件中,您希望在URL中查找该id,并告诉它从您的服务中获取该图书。你需要导入一些东西来实现它

import {Router, ActivatedRoute, Params} from '@angular/router';
然后在构造函数中,您必须实例化其中一些:

constructor(private router: Router, private route: ActivatedRoute){}
实际上,您可能也希望从“@angular/core”导入OnInit,因此它运行初始化组件所需的逻辑。因此,也将其添加到组件的顶部

 import {OnInit} from '@angular/core';
并确保您的组件在声明时实现了它

export class BookComponent implements OnInit {...}
然后在您的组件中,声明您的ngOnInit方法,然后在其中您可以拥有从API获取书籍的逻辑:

ngOnInit() {
    if (this.route.snapshot.params && this.route.snapshot.params['id']) { // make sure id is present
        bookService.getBookById(this.route.snapshot.params[id).subscribe((res)=>{
          this.book = res.json(); // this is just an example of where to put your call, not how to make it
 } else {
//no id received, throw error whatever you need, show blank form etc
}

我看不到你的截图链接,上面写着503个错误谢谢你的邮件,奇怪-我已经回复了请不要在同一个问题中同时标记Angularjs和Angularjs;尽管它们的名字很容易混淆,但它们是完全不同的框架。