首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >错误:秘密(承诺):错误:不能匹配任何路由。URL段:“详细信息”

错误:秘密(承诺):错误:不能匹配任何路由。URL段:“详细信息”
EN

Stack Overflow用户
提问于 2020-02-13 01:39:26
回答 1查看 1.8K关注 0票数 0

我想在角应用程序中使用routerLink。

app.module.ts

代码语言:javascript
复制
imports: [
    BrowserModule,
    AppRoutingModule,
    HttpClientModule,
    RouterModule.forRoot([
      {
         path: 'details/:title',
        component: NewsDetailsComponent
      },
      {
        path: '',
        component: AppComponent
      }
    ])
  ],
    exports: [RouterModule]

app.component.html

代码语言:javascript
复制
    <ul>
  <li *ngFor="let article of articles"  >
    <img src="{{article.urlToImage}}">
    <a [routerLink]="['/details',article.title]">
    <h1 (click)="goToOtherComponent()" >{{article.title}}</h1>
  </a>
    <p>{{article.description}}</p>
    <p>By <span>{{article.author}}</span></p>
  </li>
</ul>

app.component.ts

代码语言:javascript
复制
import { Component, OnInit } from '@angular/core';
import { Http } from '@angular/http';
import { NewsService } from './news.service';
import { Router } from '@angular/router';


@Component({
  selector: 'app-root',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.css']
})
export class AppComponent implements OnInit {
  ngOnInit() {
    this.getArticles();
  }
  constructor(public service: NewsService, private router: Router) {}

  title = 'google-news';

  articles;

  // get data from service
  getArticles() {
    this.service.getNews().subscribe(data => {
      console.log(data);
      this.articles = data['articles'];
    });
  }
  goToOtherComponent() {
    this.router.navigate(['/details']);
  }
}

我尝试了从app组件到news-details组件的编程导航,保存了一些数据,但是我不知道我的code..any帮助有什么问题吗?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-02-13 04:57:02

@R.cs

您可以根据组件将呈现的路由器在router-outlet中添加app.component.html。保持路由器-插座在您的app.component.html,其他的只是删除在您的组件.

参考文献:https://stackblitz.com/edit/angular-6wfpnx?file=src%2Fapp%2Fapp.component.ts

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/60199294

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档