admin管理员组文章数量:1332865
So I have been working Angular for a time, but I recently started using v18 only, and wanted to make a simple app with standalone components... I am routing all unidentified path to landing
, and from there I have a CTA which would navigate to calculations
using router.navigate(url)
, but it does not... I am facing this issue for days already, tried removing, re-adding parts but still nothing. If I reach the url directly, it works, otherwise it does not... what am I doing wrong? :/
navigateToNextPhase() {
this.router.navigate([this.data.continueUrl.url]); -> URL is a string
}
App.routes.ts
import { Routes } from '@angular/router';
import { environment } from '../environments/environment.prod';
export const appRoutes: Routes = [
{
path: 'landing',
loadComponent: () => import('./pages/landing/landingponent').then((mod) => mod.LandingComponent),
title: environment.appTitle,
},
{
path: 'calculation',
loadComponent: () => import('./pages/calculation/calculationponent').then((mod) => mod.CalculationComponent),
title: environment.appTitle,
},
/*{
path: '',
redirectTo: '/landing',
pathMatch: 'full',
},*/
{
path: '**',
loadComponent: () => import('./pages/error/errorponent').then((mod) => mod.ErrorComponent),
title: environment.appTitle,
},
];
Appponent.ts
import { CommonModule } from '@angular/common';
import { Component } from '@angular/core';
import { RouterModule, RouterOutlet } from '@angular/router';
@Component({
selector: 'app-root',
standalone: true,
imports: [CommonModule, RouterOutlet, RouterModule],
templateUrl: './appponent.html',
styleUrl: './appponent.scss',
})
export class AppComponent {
public appTitle: string = 'v18App';
}
Landingponent.ts
import { Component } from '@angular/core';
import { CardComponent } from '../../components/card/cardponent';
import { cardcontent } from '../../models/cardContents';
@Component({
selector: 'LandingComponent',
standalone: true,
imports: [CardComponent],
templateUrl: './landingponent.html',
styleUrl: './landingponent.scss',
})
export class LandingComponent {}
Calculationponent.ts
import { Component } from '@angular/core';
@Component({
selector: 'CalculationComponent',
standalone: true,
imports: [],
templateUrl: './calculationponent.html',
styleUrl: './calculationponent.scss',
})
export class CalculationComponent {}
App.config.ts
import { ApplicationConfig, provideExperimentalZonelessChangeDetection } from '@angular/core';
import { provideRouter } from '@angular/router';
import { appRoutes } from './app.routes';
import { provideClientHydration, provideProtractorTestingSupport } from '@angular/platform-browser';
export const appConfig: ApplicationConfig = {
providers: [
provideProtractorTestingSupport(),
provideExperimentalZonelessChangeDetection(),
provideRouter(appRoutes),
provideClientHydration(),
],
};
So I have been working Angular for a time, but I recently started using v18 only, and wanted to make a simple app with standalone components... I am routing all unidentified path to landing
, and from there I have a CTA which would navigate to calculations
using router.navigate(url)
, but it does not... I am facing this issue for days already, tried removing, re-adding parts but still nothing. If I reach the url directly, it works, otherwise it does not... what am I doing wrong? :/
navigateToNextPhase() {
this.router.navigate([this.data.continueUrl.url]); -> URL is a string
}
App.routes.ts
import { Routes } from '@angular/router';
import { environment } from '../environments/environment.prod';
export const appRoutes: Routes = [
{
path: 'landing',
loadComponent: () => import('./pages/landing/landingponent').then((mod) => mod.LandingComponent),
title: environment.appTitle,
},
{
path: 'calculation',
loadComponent: () => import('./pages/calculation/calculationponent').then((mod) => mod.CalculationComponent),
title: environment.appTitle,
},
/*{
path: '',
redirectTo: '/landing',
pathMatch: 'full',
},*/
{
path: '**',
loadComponent: () => import('./pages/error/errorponent').then((mod) => mod.ErrorComponent),
title: environment.appTitle,
},
];
Appponent.ts
import { CommonModule } from '@angular/common';
import { Component } from '@angular/core';
import { RouterModule, RouterOutlet } from '@angular/router';
@Component({
selector: 'app-root',
standalone: true,
imports: [CommonModule, RouterOutlet, RouterModule],
templateUrl: './appponent.html',
styleUrl: './appponent.scss',
})
export class AppComponent {
public appTitle: string = 'v18App';
}
Landingponent.ts
import { Component } from '@angular/core';
import { CardComponent } from '../../components/card/cardponent';
import { cardcontent } from '../../models/cardContents';
@Component({
selector: 'LandingComponent',
standalone: true,
imports: [CardComponent],
templateUrl: './landingponent.html',
styleUrl: './landingponent.scss',
})
export class LandingComponent {}
Calculationponent.ts
import { Component } from '@angular/core';
@Component({
selector: 'CalculationComponent',
standalone: true,
imports: [],
templateUrl: './calculationponent.html',
styleUrl: './calculationponent.scss',
})
export class CalculationComponent {}
App.config.ts
import { ApplicationConfig, provideExperimentalZonelessChangeDetection } from '@angular/core';
import { provideRouter } from '@angular/router';
import { appRoutes } from './app.routes';
import { provideClientHydration, provideProtractorTestingSupport } from '@angular/platform-browser';
export const appConfig: ApplicationConfig = {
providers: [
provideProtractorTestingSupport(),
provideExperimentalZonelessChangeDetection(),
provideRouter(appRoutes),
provideClientHydration(),
],
};
Share
asked Nov 22, 2024 at 7:19
CreativeZollerCreativeZoller
3062 silver badges15 bronze badges
4
|
1 Answer
Reset to default 1Since you are navigating to the root path (/
) you should go for absolute routing, which always checks from the root element, so calculation
will be discoverable.
navigateToNextPhase() {
this.router.navigate(['/calculation']); // -> absolute routing
}
The issue might be when set calculation
, it is checking for calculation route relative to the landing
component.
本文标签: typescriptAngular Standalone Component Routing Error Not routing properlyStack Overflow
版权声明:本文标题:typescript - Angular Standalone Component Routing Error: Not routing properly - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742292138a2448004.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
calculation
. The representetive part is below: ``` <form class="form"> <span>{{data.cardContent.contentMain}}</span> <button class="button button--primary button--cta" title="Start" (click)="navigateToNextPhase()">Start</button> </form>``` – CreativeZoller Commented Nov 22, 2024 at 10:36