Compon...">

Angular 2 routing without navbar

Every topic in Angular2 routing has a fixed navigation bar

<nav>
    <a [routerLink]="['/component-one']">Component One</a>
    <a [routerLink]="['/component-two']">Component Two</a>
</nav>
<router-outlet></router-outlet>

Thus, basically when you click on the link to Component One, this component will be displayed under <nav></nav>

What I need when you click on Component One, the full view (full page) changes to the Component One view (thus, without <nav></nav>)

I tried to put <router-outlet>in a separate@Component

// view.component.ts    
import { Component } from '@angular/core';
import { ROUTER_DIRECTIVES } from '@angular/router';

@Component({
    selector: 'view',
    template:`<router-outlet></router-outlet>`,
    directives: [ROUTER_DIRECTIVES]
})

export class ViewComponent {

}

and then

// home.component.ts
import { Component } from '@angular/core';
import { ROUTER_DIRECTIVES } from '@angular/router';

@Component({
    selector: 'home',
    template: `
        <nav>
            <a [routerLink]="['/component-one']">Component One</a>
            <a [routerLink]="['/component-two']">Component Two</a>
        </nav>
        <router-outlet></router-outlet>
    `,
    directives: [ROUTER_DIRECTIVES]
})

export class HomeComponent {

}

App component is as follows:

import { Component } from '@angular/core';
import { ROUTER_DIRECTIVES } from '@angular/router';

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

@Component({
  moduleId: module.id,
  selector: 'app-root',
  template: `
  <h1>{{title}}</h1>
  <view></view>
  <home></home>
  `,
  styleUrls: ['app.component.css'],
  directives:  [HomeComponent, ROUTER_DIRECTIVES]
})
export class AppComponent {
  title = 'app works';
}

also tried to put <home>inside <view>but nothing

Always get the same error:

Error: Cannot find the main outlet to download "OneComponent"

Anyone who can help me? thank!

change

// app.routes.ts
import { provideRouter, RouterConfig } from '@angular/router';

import { ViewComponent } from './containers/view.component';
import { HomeComponent } from './containers/home.component';
import { OneComponent } from './containers/one.component';
import { TwoComponent } from './containers/two.component';

const routes: RouterConfig = [
    {
        path: '',
        redirectTo: '',
        pathMatch: 'full'
    },
    { 
        path: '', 
        component: HomeComponent, 
    },
    { 
        path: 'one', 
        component: OneComponent 
    },
    { 
        path: 'two', 
        component: TwoComponent 
    }
];

export const appRouterProviders = [
    provideRouter(routes)
];
+4
3

<nav>
    <a [routerLink]="['/component-one']">Component One</a>
    <a [routerLink]="['/component-two']">Component Two</a>
</nav>

, , , , , .

<router-outlet></router-outlet>  

0

:

<nav>
  <a [routerLink]="['/component-one']">Component One</a>
  <a [routerLink]="['/component-two']">Component Two</a>
</nav>

 { 
    path: 'one', 
    component: OneComponent 
},
{ 
    path: 'two', 
    component: TwoComponent 
}

:

<nav>
  <a [routerLink]="['/one']">Component One</a>
  <a [routerLink]="['/two']">Component Two</a>
</nav>
0

... .

:

(<view></view>)

// app.component.ts
import { Component } from '@angular/core';
import { ROUTER_DIRECTIVES } from '@angular/router';

import { ViewComponent } from './containers/view.component';

@Component({
  moduleId: module.id,
  selector: 'app-root',
  template: `
  <h1>
    {{title}}
  </h1>
  <view></view>
  `,
  directives:  [ViewComponent, ROUTER_DIRECTIVES]
})
export class AppComponent {
  title = 'app works!';
}

<router-outlet></router-outlet>

// view.component.ts
import { Component } from '@angular/core';
import { ROUTER_DIRECTIVES } from '@angular/router';

@Component({
    selector: 'view',
    template:`<router-outlet></router-outlet>`,
    directives: [ROUTER_DIRECTIVES]
})

export class ViewComponent {  
}

Routes must be correct (oops)

// app.routes.ts
import { provideRouter, RouterConfig } from '@angular/router';

import { HomeComponent } from './containers/home.component';
import { OneComponent } from './containers/one.component';
import { TwoComponent } from './containers/two.component';

const routes: RouterConfig = [
    {
        path: '',
        redirectTo: '/home',
        pathMatch: 'full'
    },
    { 
        path: 'home', 
        component: HomeComponent, 
    },
    { 
        path: 'one', 
        component: OneComponent 
    },
    { 
        path: 'two', 
        component: TwoComponent 
    }
];

export const appRouterProviders = [
    provideRouter(routes)
];

Then it just builds the views (pages)

// home.components.ts
import { Component } from '@angular/core';
import { ROUTER_DIRECTIVES } from '@angular/router';

import { NavigationComponent } from './navigation.component';

@Component({
    selector: 'home',
    template: `
        <div><h1>{{title}}</h1></div>
        <navigation></navigation>
    `,
    directives: [ROUTER_DIRECTIVES, NavigationComponent]
})

export class HomeComponent {
    title = 'This is home';
}

navigation component used in HomeComponent:

// navigation.component.ts
import { Component } from '@angular/core';
import { ROUTER_DIRECTIVES } from '@angular/router';

@Component({
    selector: 'navigation',
    template: `
        <nav>
            <a [routerLink]="['/one']">Comp one</a>
            <a [routerLink]="['/two']">Comp two</a>
        </nav>
    `,
    directives: [ROUTER_DIRECTIVES]
})

export class NavigationComponent {
}

and

// one.component.ts
import { Component } from '@angular/core';

import { ROUTER_DIRECTIVES } from '@angular/router';

@Component({
    selector: 'one',
    template: `
        <p>ONE: {{message}}</p>
        <a [routerLink]="['/two']">GOTO Comp two</a>
    `,
    directives: [ROUTER_DIRECTIVES]
})

export class OneComponent {
    message = 'ONE comp here';
}

and

// two.component.ts
import { Component } from '@angular/core';

import { ROUTER_DIRECTIVES } from '@angular/router';

@Component({
    selector: 'two',
    template: `
        <p>TWO: {{message}}</p>
        <a [routerLink]="['/one']">GOTO Comp one</a>
    `,
    directives: [ROUTER_DIRECTIVES]
})

export class TwoComponent {
    message = 'TWO here';
}
0
source

Source: https://habr.com/ru/post/1650782/


All Articles