简体   繁体   English

成功路由到组件后,为什么我的组件的html无法显示?

[英]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.

相关问题 尽管在我的控制台中成功编译,为什么我的反应应用程序在导入和使用材料 ui 中的图标后显示空白屏幕? - Why is my react app displaying a blank screen after importing and using an icon from material ui despite compiling successfully in my console? 为什么反应要立即显示所有组件? - Why is react displaying all my components at once? 为什么我的 HTML 表单不显示我的输入? - Why is my HTML form not displaying my input? 为什么我下载的 HTML 文件没有显示为 HTML? - Why aren't my downloaded HTML file displaying as HTML? 为什么我的jquery数据未显示在HTML页面中 - Why is my jquery data not displaying in my HTML page 为什么我的 Vue js 应用在路由到其他组件时会失去焦点? - Why does my Vue js app lose focus when routing to other components? HTML 在我的 JS 文件中的某个点之后不显示任何内容? - HTML not displaying anything after a certain point in my JS file? 我的 html 在使用 javascript 获取文件后不显示本地 json 文件 - My html not displaying the local json file after fetching it using javascript 为什么我的 Google Maps API 被嵌入但没有显示在 HTML 页面上? - Why is my Google Maps API being embedded but not displaying on an HTML Page? 为什么我的JS代码没有将消息显示到HTML页面? - Why isn't my JS code displaying the message to the HTML page?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM