[英]Why is the html of my components not displaying after successfully routing to it?
I'm developing an Angular 2 app and I made a router to navigate around the app. 我正在开发Angular 2应用,并制作了一个路由器来浏览该应用。 While it will successfully go to the correct urls, it won't actually render the html of any component other than AppComponent.
虽然它将成功地转到正确的url,但实际上不会呈现AppComponent以外的任何组件的html。
route.module.ts route.module.ts
import { DashboardComponent } from './dashboard/dashboard.component';
import { LoginComponent } from './login/login.component';
import { NgModule } from '@angular/core';
import { RouterModule, Routes } from '@angular/router';
const routes: Routes = [
{ path: '', redirectTo: 'login', pathMatch: 'full' },
{ path: 'login', component: LoginComponent },
{ path: ":name", component: DashboardComponent}
];
@NgModule({
imports: [ RouterModule.forRoot(routes) ],
exports: [ RouterModule ]
})
export class AppRoutingModule {}
app.module.ts app.module.ts
import { AppRoutingModule } from './route.module';
import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';
import { AppComponent } from './app.component';
import { LoginComponent } from './login/login.component';
import { FormsModule } from '@angular/forms';
import { DashboardComponent } from './dashboard/dashboard.component';
@NgModule({
declarations: [
AppComponent,
LoginComponent,
DashboardComponent
],
imports: [
BrowserModule,
AppRoutingModule,
FormsModule
],
providers: [],
bootstrap: [AppComponent]
})
export class AppModule { }
login.component.ts login.component.ts
import { Component, OnInit } from '@angular/core';
@Component({
selector: 'app-login',
templateUrl: './login.component.html',
styleUrls: ['./login.component.css']
})
export class LoginComponent implements OnInit {
name: String;
pass: String;
constructor() {
}
ngOnInit( ) {
}
login(){
if (this.name == "Shai" && this.pass=="jon"){
location.href = "http://localhost:4200/" + this.name;
}
}
}
app.component.ts (the only one that will display) app.component.ts(将显示的唯一一个)
import { Component } from '@angular/core';
@Component({
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.css']
})
export class AppComponent {
;
}
app.component.html app.component.html
<app-login></app-login>
Note: This worked perfectly for getting the login component to display but obviously it causes it to display everywhere which isn't a good idea. 注意:这非常适合使登录组件显示,但显然会使它显示在任何地方,这不是一个好主意。
I will be very grateful to anybody who can figure out what's wrong with my code. 我将非常感谢任何能弄清楚我的代码出了什么问题的人。
Edit: Added Templates 编辑:添加模板
login.component.html login.component.html
<div class="text-center col-sm-2 col-centered" style="padding:50px 0">
<div class="logo"><h1>login</h1></div>
<!-- Main Form -->
<div class="login-form-1">
<form id="login-form" class="text-left">
<div class="login-form-main-message"></div>
<div class="main-login-form">
<div class="login-group">
<div class="form-group">
<label for="lg_username" class="sr-only">Username</label>
<input type="text" [(ngModel)]="name" class="form-control" id="lg_username" name="lg_username" placeholder="username">
</div>
<div class="form-group">
<label for="lg_password" class="sr-only">Password</label>
<input type="password" [(ngModel)]="pass"class="form-control" id="lg_password" name="lg_password" placeholder="password">
</div>
</div>
<button class="login-button" (click)="login()"><i>Login</i></button>
</div>
</form>
</div>
<!-- end:Main Form -->
</div>
dashboard.component.html dashboard.component.html
<p>Dashboard Works</p>
If you are using angular routing then app.component.html should have <router-outlet></router-outlet>
not <app-login></app-login>
. 如果您使用角度路由,则app.component.html应该具有
<router-outlet></router-outlet>
而不是<app-login></app-login>
。
Then router then loads the component you want to view based on the path you enter, IE "/login" or "" will load the login component, and inject <app-login></app-login>
into the router outlet. 然后,路由器会根据您输入的路径加载要查看的组件,IE“ / login”或“”将加载登录组件,并将
<app-login></app-login>
注入路由器插座。
See: https://angular.io/guide/router#router-outlet 请参阅: https : //angular.io/guide/router#router-outlet
The guide here shows you how to structure an app, and implement the routing. 此处的指南向您展示了如何构建应用和实施路由。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.