2015-12-26 12 views
6

Yani benim AuthenticationService içine aşağıdaki kod parçası var.Eğik 2: '...' geçti parametreleri dahil edilmemiştir için Rota jeneratör

authentication.service.ts: oturum açma kimlik bilgileri kontrol edilir sonra kullanıcıdan profilinize yönlendirilir en olsun

redirectUser(username:string):void { 
    // Redirect user to profile on successful login 
    this.router.navigate(['../Profile/Feed', {username: username}]); 
} 

Ve hepsi iyi çalıştı, ama hiç ben ProfileComponent içindeki bazı alt yolları tanıtılan beri, bazı hatalar koştu .

app.ts: Gördüğünüz gibi

import {Component, ViewEncapsulation} from 'angular2/core'; 
import {RouteConfig,ROUTER_DIRECTIVES} from 'angular2/router'; 
import {HTTP_PROVIDERS} from 'angular2/http'; 

import {HomeComponent} from '../home/home'; 

import {AuthenticationService} from '../../services/authentication.service'; 
import {LoginComponent} from '../login/login'; 
import {ProfileComponent} from '../profile/profile'; 
import {ProfileService} from '../../services/profile.service'; 

@Component({ 
    selector:  'app', 
    viewProviders: [AuthenticationService, ProfileService, HTTP_PROVIDERS], 
    encapsulation: ViewEncapsulation.None, 
    directives: [ 
    ROUTER_DIRECTIVES 
    ], 
    templateUrl: './components/app/app.html' 
}) 

@RouteConfig([ 
    {path: '/', component: HomeComponent, as: 'Home'}, 
    {path: '/inloggen', component: LoginComponent, as: 'Login'}, 
    {path: '/profile/:username/...', component: ProfileComponent, as: 'Profile'} 
]) 

export class AppComponent { 
    ... 
} 

, bazı yeni rotalar ProfileComponent içinde tanımlanır Her Öncelikle, bu RouteConfig ile benim AppComponent olduğunu. Burada görüldüğü gibi:

ProfileComponent.ts: Ben ProfileComponent içindeki bazı alt yolları tanıtılan önce

import {Component, OnInit} from 'angular2/core'; 
import {RouteConfig,ROUTER_DIRECTIVES, RouteParams, RouterLink, RouterOutlet} from 'angular2/router'; 
import {ProfileService} from '../../services/profile.service'; 
import {PROFILE_IMAGES} from '../../constants/constants'; 
import {WorkoutsComponent} from '../workouts/workouts'; 
import {FeedComponent} from '../feed/feed'; 

interface IProfileVM { 
    username: string; 
    profileUser: Object; 
    getProfileData(username:string): void; 
} 

@Component({ 
    selector: 'profile', 
    templateUrl: './components/profile/profile.html', 
    directives: [RouterOutlet, RouterLink], 
    providers: [ProfileService] 
}) 

@RouteConfig([ 
    {path: '/nieuwsfeed', component: FeedComponent, as: 'Feed'}, 
    {path: '/workouts', component: WorkoutsComponent, as: 'Workouts'} 
]) 

export class ProfileComponent implements OnInit, IProfileVM { 

    public username:string; 
    public profileUser:any; 

    constructor(private _profileService:ProfileService, 
       private _params:RouteParams) { 
    this.username = this._params.get('username'); 
    } 

    ngOnInit() { 
    this.getProfileData(this.username); 
    } 

    getProfileData(username) { 
    // Do stuff.. 
    } 
} 

Yani burada sorun, her şey iyi çalıştı edilir. Kullanıcının yönlendirmesi, kullanıcı adı URL’de görünür ve her şey yolundaydı. Bunları çocuk yolları eklemiş olduğundan, aşağıdaki hata var:

"Route generator for 'username' was not included in parameters passed."

biri bana yardım edebilecek olan harika olurdu !! Şimdiden teşekkürler!

+4

'this.router.navigate ([ '../ Profil', {adı: adı}, 'Yem']) ile deneyin;' –

+1

Gerçekten ?? Bu sadece yaptı ... :-) Çok teşekkürler! Eğer '../Profile/Feed, varken –

+1

Evet, {username: username}' sen Feed' değil 'Profile'' parametreleri geçen. Sevindim;) –

cevap

2

ben size rota tanımları içinde yuvalanmış profil bileşenini yukarı bir seviyede, hareketli daha iyi olacağını düşünüyorum. username keyfi değeri, profil bileşeni yönlendirmesi üzerinde hiçbir etkisi yoktur. Ayrıca, değer tanımlanması gereken yerde çocuk rotasındaki değer gereklidir.

@RouteConfig([ 
    {path: '/:username/nieuwsfeed', component: FeedComponent, as: 'Feed'}, 
    {path: '/:username/workouts', component: WorkoutsComponent, as: 'Workouts'} 
]) 

Bu yaklaşım daha görünüyor:

@RouteConfig([ 
    {path: '/', component: HomeComponent, as: 'Home'}, 
    {path: '/inloggen', component: LoginComponent, as: 'Login'}, 
    {path: '/profile/...', component: ProfileComponent, as: 'Profile'} 
]) 

yerine profil bileşeni :username rota parametresini tanımlarsınız: profil rotadan :username kaybeder ve böyle bir şey olmazdı

Yani ana bileşen yolları sezgiseldir ve iç içe geçmiş yol bileşenleri arasında gezinirken daha az sorun çıkarır.

+0

Her şeyden önce, zaman ayırdığınız için teşekkürler. Kulağa hoş geliyor, dün çalıştığım bir şey ve işe yarayacak. Ayrıca bence bu durumda daha sezgisel. Ve bu konuda daha fazla araştırma yaptıktan sonra, ebeveyn bileşenlerinden param olmak için herhangi bir belge bulamadım. Öyleyse, artık, ebeveyn bileşenlerinden param olmak, isteksiz ve hatta imkansız olmayan bir yol değildir. –

+0

Oh, I (http://stackoverflow.com/questions/34500147/angular-2-getting-routeparams-from-parent-component) [üst denetleyicileri gelen params alma] hakkında yayınlanan daha yeni bir soru karışık, ancak bu soru ilgilenen insanlar için çok ilgili. –