Angular RouterLink破坏整个布局。

huangapple go评论63阅读模式
英文:

Angular RouterLink breaks the whole Layout

问题

我已经构建了一个具有约8个页面和完全功能的路由的Angular应用程序。

现在在另一台机器上,我在设置路由时遇到了问题,我看不到我的错误。

在创建应用程序时,我启用了路由,因此Angular已经创建了AppRoutingModule并将其导入AppModule。

在定义一些简单的路由之后,仍然不起作用。

app.component.html:

<router-outlet></router-outlet>

app-routing.module.ts:

import { NgModule } from '@angular/core';
import { RouterModule, Routes } from '@angular/router';
import { TestComponent } from './test/test.component';

const routes: Routes = [
  { path: 'test', component: TestComponent },
  { path: '', redirectTo: 'test' },
  { path: '**', redirectTo: 'test' }
];

@NgModule({
  imports: [RouterModule.forRoot(routes)],
  exports: [RouterModule]
})
export class AppRoutingModule { }

这些代码会显示一个空白屏幕。

当将app.component.html的内容更改为以下内容时:

<app-test></app-test>

一切都正常:

Angular RouterLink破坏整个布局。

test.component.html:

<div class="flex">
    <div class="item i-1">Flex Item 1</div>
    <div class="item i-2">Flex Item 2</div>
</div>

与以下样式一起使用:

.flex {
    display: flex;
    height: 100vh;
    width: 100vw;
}

.item {
    flex: 1;
    text-align: center;
}

.i-1 {
    background-color: aqua;
}

.i-2 {
    background-color: greenyellow;
}

但现在是我在标题中提到的问题:
当我在HTML中提供routerLink时,页面会中断:

<div class="flex">
    <div routerLink="test" class="item i-1">Flex Item 1</div>
    <div class="item i-2">Flex Item 2</div>
</div>

Angular RouterLink破坏整个布局。

routerLink-div中的文本和第二个flex-box项目都不会呈现。

我会认为这些问题是相关的,但除了定义所需的路由之外,还有什么其他事情要做?

我尝试将所有路由逻辑移到app-module中,这在我的其他项目中对我有效,但没有改变任何东西。

我遵循了官方Angular文档提供的步骤:https://angular.io/guide/router

英文:

I already built an Angular-App with ~8 Pages and fully functional Routing.

Now on another machine, I struggle to set up routing again and I don't see my error.

I activated routing when creating the app, so angular already created the AppRoutingModule and imported it in the AppModule.

After defining some simple routes, this is still not working.

app.component.html:

&lt;router-outlet&gt;&lt;/router-outlet&gt;

app-routing.module.ts:

import { NgModule } from &#39;@angular/core&#39;;
import { RouterModule, Routes } from &#39;@angular/router&#39;;
import { TestComponent } from &#39;./test/test.component&#39;;

const routes: Routes = [
  {path: &#39;test&#39;, component: TestComponent},
  {path: &#39;&#39;, redirectTo: &#39;test&#39;},
  {path: &#39;**&#39;, redirectTo: &#39;test&#39;}
];

@NgModule({
  imports: [RouterModule.forRoot(routes)],
  exports: [RouterModule]
})
export class AppRoutingModule { }

This somehow shows an empty, white screen.

When exchanging the content of the app.component.html to this:

&lt;app-test&gt;&lt;/app-test&gt;

everything is fine:

Angular RouterLink破坏整个布局。

The test.component.html:

&lt;div class=&quot;flex&quot;&gt;
    &lt;div class=&quot;item i-1&quot;&gt;Flex Item 1&lt;/div&gt;
    &lt;div class=&quot;item i-2&quot;&gt;Flex Item 2&lt;/div&gt;
&lt;/div&gt;

which following styling:

.flex {
    display: flex;
    height: 100vh;
    width: 100vw;
}

.item {
    flex: 1;
    text-align: center;
}

.i-1 {
    background-color: aqua;
}

.i-2 {
    background-color: greenyellow;
}

But now the problem to which I referred in the title:
When I provide a routerLink in the html, the page breaks:

&lt;div class=&quot;flex&quot;&gt;
    &lt;div routerLink=&quot;test&quot; class=&quot;item i-1&quot;&gt;Flex Item 1&lt;/div&gt;
    &lt;div class=&quot;item i-2&quot;&gt;Flex Item 2&lt;/div&gt;
&lt;/div&gt;

Angular RouterLink破坏整个布局。

Neither the text in the routerLink-div, nor the second flex-box-item is rendering.

I would assume that these problems are connected, but what else is there to do, other than defining the needed routes?

I tried moving all the routing-logic to the app-module which is working for me in my other project, but it changed nothing.

I followed the steps provided by the official angular documentation: https://angular.io/guide/router

答案1

得分: 1

This is due to the default route missing an argument:

Angular RouterLink破坏整个布局。

Changing

{path: &#39;&#39;, redirectTo: &#39;test&#39;}

to

{path: &#39;&#39;, redirectTo: &#39;test&#39;, pathMatch: &#39;full&#39;}

resolves the routes and also the routerLink issue.

(I somehow missed the console errors while testing)

英文:

This is due to the default route missing an argument:

Angular RouterLink破坏整个布局。

Changing

{path: &#39;&#39;, redirectTo: &#39;test&#39;}

to

{path: &#39;&#39;, redirectTo: &#39;test&#39;, patchMatch: &#39;full&#39;}

resolves the routes and also the routerLink issue.

(I somehow missed the console errors while testing)

huangapple
  • 本文由 发表于 2023年2月18日 23:03:18
  • 转载请务必保留本文链接:https://go.coder-hub.com/75494206.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定