下面的代碼提供了三種不同的途徑,包括
/
,/route2
和/route3
同一組件AppComponent
。使用角2路由器爲不同的值傳遞到同一個組件問題是當選擇不同的路由時
AppComponent
的title
和bodyHTML
屬性不會更改值。需要對下面的代碼進行哪些特定的更改,以便當用戶選擇每個不同的路由時,應用程序將爲
title
和bodyHTML
提供不同的值?下面是重現問題任何計算機上幾分鐘步驟:
創建種子應用:
首先,我創建了一個種子應用程序在以下步驟中使用Angular-CLI:
cd C:\projects\angular-cli
ng new routes-share-component
cd C:\projects\angular-cli\routes-share-component
ng serve
個
變化只有4個文件:
接下來,我改變了只有4個文件如下:
我加app.routing.ts
具有以下內容:
import { Routes, RouterModule } from '@angular/router';
import { AppComponent } from './app.component';
const appRoutes: Routes = [
{ path: '', component: AppComponent },
{ path: 'route2', component: AppComponent },
{ path: 'route3', component: AppComponent }
];
export const routing = RouterModule.forRoot(appRoutes);
我改變app.component.ts
成爲如下:
import { Component, OnInit, OnChanges } from '@angular/core';
import { ActivatedRoute } from '@angular/router';
import { Router } from '@angular/router';
@Component({
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.css']
})
export class AppComponent implements OnInit, OnChanges {
title = 'This is the title!';
bodyHTML = 'Here is the content!'
constructor(private _router:Router, private route:ActivatedRoute) {
console.log('inside the constructor!');
console.log(route.url);
console.log(_router.url);
}
ngOnInit() {
console.log('inside ngOnInit()');
let currentUrl = this._router.url; /// this will give you current url
console.log(currentUrl);
if(currentUrl=='/'){this.title='Home Page Title';this.bodyHTML='Body goes here.';}
if(currentUrl=='/route2'){this.title='Route 2 Title';this.bodyHTML='Body goes here.';}
if(currentUrl=='/route3'){this.title='Route 3 Title';this.bodyHTML='Body goes here.';}
console.log(this.route.url);
}
ngOnChanges() {
console.log('inside ngOnChanges()!');
let currentUrl = this._router.url; /// this will give you current url
console.log(currentUrl);
if(currentUrl=='/'){this.title='Home Page Title';this.bodyHTML='Body goes here.';}
if(currentUrl=='/route2'){this.title='Route 2 Title';this.bodyHTML='Body goes here.';}
if(currentUrl=='/route3'){this.title='Route 3 Title';this.bodyHTML='Body goes here.';}
console.log(this.route.url);
}
}
同樣,app.component.html
簡化爲以下:
<div style="text-align:left">
<h1>{{title}}</h1>
<p>{{bodyHTML}}</p>
</div>
而且app.module.ts
變得下文中,app.routing.ts
包括:
import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';
import { AppComponent } from './app.component';
import { routing } from './app.routing';
@NgModule({
declarations: [
AppComponent
],
imports: [
BrowserModule, routing
],
providers: [],
bootstrap: [AppComponent]
})
export class AppModule { }
注意,在控制檯中ngOnInit()
塊打印,但該ngOnChanges()
塊纔不是。這意味着標題總是Home Page Title
,無論選擇哪條路線。
需要對上述代碼進行哪些特定更改,以便每條路徑在瀏覽器中打印title
和bodyHTML
的不同值?
@陰間大法師的建議:
每@陰間大法師的建議,我嘗試了AppComponent
以下新的版本,但它顯示編譯錯誤的行routerSub:Subscription
,並在該行this.routerSub = this.router.events.filter(....)
。
import { Component, OnInit, OnChanges } from '@angular/core';
import { NavigationEnd, Router, ActivatedRoute } from '@angular/router';
import { Subscription } from 'rxjs/Subscription';
import 'rxjs/add/operator/filter';
import 'rxjs/add/operator/map';
@Component({
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.css']
})
export class AppComponent implements OnInit {
title = 'This is the title!';
bodyHTML = 'Here is the content!';
routerSub:Subscription;
constructor(private router:Router) {
console.log('inside the constructor!');
console.log(router.url);
}
ngOnInit(){
// listen to NavigationEnd events
this.routerSub = this.router.events.filter(e=>e instanceof NavigationEnd)
// capture the new URL
.map(e.NavigationEnd => e.url)
.subscribe(url => {
/* TODO: use URL to update the view */
});
}
// When the component is destroyed, you must unsubscribe to avoid memory leaks
ngOnDestroy(){
this.routerSub.unsubscribe();
}
}
還需要改變什麼?
在OP的哪個類中添加您建議的代碼?爲什麼不可能讓多個結果解析爲相同的內容?這個應用程序的目的是使多個搜索引擎友好的路線使用相同的模板。 – CodeMed
@CodeMed該代碼旨在運行在'app.component.ts'中。我對搜索引擎優化知之甚少,所以我不能建議如何使一個Angular應用程序搜索引擎友好。但是,您正在使用多個路線來解決路線參數旨在解決的問題。我不知道'/ route1','/ route2'比'/ route/1','/ route/2'更好(最後參數) – BeetleJuice
我剛剛添加了一個新的' AppComponent'和你的建議,並描述其餘的編譯錯誤。我還需要改變它嗎? – CodeMed