dir.by  
Поиск >>

Компьютер, программы
→
Angular
 В web приложение с Angular 12 добавляем маршрутизацию (router) 
посмотрели 4700 раз
обновлено: 7 Augusta 2021
Шаг 1. Создаем новое angular приложение
Шаг 2. Добавляем маршрутизацию в файл app.module.ts
  в файл app.module.ts добавляем
import { BrowserModule } from '@angular/platform-browser';
import { FormsModule }     from '@angular/forms';
import { AppComponent }     from './app.component';
import { PriceComponent }     from './price/price.component';
import { HomeComponent } from './home/home.component';
import { NgModule } from '@angular/core';
import { Routes, RouterModule } from '@angular/router';
import { APP_BASE_HREF } from '@angular/common';

const appRoutes: Routes = [
     { path: '', component: HomeComponent },
     { path: 'price', component: PriceComponent },
];


@NgModule({
     imports: [ BrowserModule, FormsModule, RouterModule.forRoot(appRoutes) ],
     declarations: [ AppComponent, PriceComponent, HomeComponent ],
     bootstrap: [ AppComponent ],
     providers: [{provide: APP_BASE_HREF, useValue: '/'}]
})
export class AppModule { }
Шаг 3. Добавляем маршрутизацию в файл app.component.html
  Html     в файл app.component.html добавляем
<h1>Добро пожаловать!</h1>

<img src="assets/arrow_right.png" (click)="myNavigate();" />

<router-outlet></router-outlet>
Шаг 4. Добавляем маршрутизацию в файл app.component.ts
  в файл app.component.ts добавляем
import { Component } from '@angular/core';
import { Router } from '@angular/router';

@Component({
     selector: 'app-root',
     templateUrl: './app.component.html',
     styleUrls: ['./app.component.scss']
})

export class AppComponent {

     myText1:string = "Hello!";

     constructor(private router: Router) {
     }

     ngOnInit(){
     }

     myNavigate()
     {
          this.router.navigate(['/price']);
     }

}
Шаг 5. Создаем PriceComponent
  Файл price.component.html
<h1>{{myText1}}!</h1>
  Файл price.component.scss
h1 {
     color: blue;
}
  Файл price.component.ts
import { Component } from '@angular/core';

@Component({
     selector: 'price',
     templateUrl: './price.component.html',
     styleUrls: ['./price.component.scss']
})

export class PriceComponent {

     myText1:string = "Book price!";

     constructor(){
     }

     ngOnInit(){
     }
}
Шаг 6. Создаем HomeComponent
  Файл home.component.html
<h1>{{myText1}}!</h1>
  Файл home.component.scss
h1 {
     color: yellow;
}
  Файл home.component.ts
import { Component } from '@angular/core';

@Component({
     selector: 'home',
     templateUrl: './home.component.html',
     styleUrls: ['./home.component.scss']
})

export class HomeComponent {

     myText1:string = "Home!";

     constructor(){
     }

     ngOnInit(){
     }
}
Важно
Содержимое HomeComponent и PriceComponent при навигации будет вставляться в <router-outlet></router-outlet> находящейся в файле app.component.html
Фотогалерея
Скачать пример
 
← Previous topic
Creating a new web application with Angular 12 in Visual Studio Code. Use: Node.js + Angular (write our component in the ts file) + Typescript (convert ts files to js)
 
Next topic →
Modules in Angular
 
Your feedback ... Comments ...
   
Your Name
Your comment (www links can only be added by a logged-in user)

Новость "Черная пятница" Dreame в Беларуси – две недели скидок до 3400 рублей
 
• What is Angular 8?
New Angular 8 application
Option 1. Creating a new Angular application from the console with the angular/cli command (50 files)
• Creating a new web application with Angular 8. Using the command line: npx -p @angular/cli@8.1.0 ng new
Option 2 (the best for me). Creating a new Angular application yourself (8 files)
• Create a new web application with Angular 8 in Visual Studio Code. We use: Node.js + Angular (write our component in ts file) + Typescript (convert files ts to js)
• What happens when I start an Angular 8 application? Run main.ts file. Creating a module.
Angular 9
• Creating a new web application with Angular 9 in Visual Studio Code. Use: Node.js + Angular (write our component in the ts file) + Typescript (convert ts files to js)
Angular 12
• Creating a new web application with Angular 12 in Visual Studio Code. Use: Node.js + Angular (write our component in the ts file) + Typescript (convert ts files to js)
• Adding routing (router) to the web application with Angular 12
Angular Description
• Modules in Angular
• Components in Angular
• Binding data in Angular
Errors
• Error "npm : The term "npm" is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spell included, verify that the path is correct and try again. " | nodejs npm angular
• Error "npm ERR! While resolving: @angular/flex-layout@12.0.0-beta.35 | Could not resolve dependency: | Conflicting peer dependency: @angular/cdk@12.2.13" | nodejs npm angular
• ng serve Error "ng : The term "ng" is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spellin the path is correct and try again. " | nodejs npm angular
• ng serve Error "File ng.ps1 cannot be loaded. The file ng.ps1 is the current system. " | nodejs npm angular
• Error "Module build failed (from ./node_modules/@ngtools/webpack/src/ivy/index.js)" | nodejs npm angular
Docker for Angular
• Using Docker in a project with Angular (for Windows)
Angular Flex-Layout
• Angular Flex-Layout
WWW Sites to Learn Angular
• Sites to learn Angular

  Ваши вопросы присылайте по почте: info@dir.by