我是新的角2,我試圖創建一個新聞網站,這意味着主頁例如包含不同類別的不同文章。如何在角度2中創建動態路由器連接?
所以我得到的,例如從API滑動部JSON結果包含鏈接前的文章:「/滑塊/第一條」,「/運動/第三條」等。
當我嘗試循環在html中的結果routerlink不起作用!
這裏是一個示例代碼
Home.component.html //可以說是鏈接中包含 '/體育'
<a [routerLink]="[item.link]">{{item.title}}</a>
Route.config.ts
export const rootRouterConfig: Routes = [
{ path: '', redirectTo: 'home', pathMatch: 'full' },
{ path: 'home', component: HomeComponent },
{ path: 'sport', component: SportComponent }
];
**我忘記提及錯誤
Uncaught (in promise): TypeError: Cannot read property 'outlets' of null
Home.component.ts
import { Component, OnInit } from '@angular/core';
import { HomeService } from '../../_services/home.service';
import { ContentInfo } from '../../_models/content';
@Component({
selector: 'app-home',
templateUrl: './home.component.html',
styleUrls: ['./home.component.css'],
providers: [HomeService]
})
export class HomeComponent implements OnInit {
mainNewsItem: ContentInfo[];
constructor(private _homeService: HomeService) {
this.mainNewsItem = [];
}
ngOnInit() {
// Get Main News Item
this._homeService.getMainNewsItem()
.subscribe(content => this.mainNewsItem = content,
error => this._homeService = <any>error)
;
}
}
我用* ngif
*ngIf="mainNewsItem?.length > 0"
股利走了,但爲什麼呢?我的意思是mainNewsItem包含數據!
謝謝:)
能你在盜賊身上重現你的問題?和控制檯中的錯誤是什麼 –
[根據文檔](https://angular.io/docs/ts/latest/guide/router.html#!#route-def-with-parameter),你應該使用路線參數。如果你不明白,隨時問! – trichetriche
錯誤 - 未被捕獲(承諾):TypeError:無法讀取屬性'outlets'爲空, –