javascript - Angular 6 错误 "NullInjectorError: No provider for Router!"

标签 javascript asp.net angular frontend

我目前正在做一个项目,我需要用户填写一个 Angular 表单,然后将其发送到我后端的路由来处理数据。后端在 ASP.NET 中,我已经有一个可用的 HTML 函数形式:

<body>
<h2 style="text-align:center">Push notification test</h2>
<form style="align-content:center" action="SendPushNotification" method="post">
    <div>
        <fieldset>
            <legend> Informations </legend>
            <label> Notification name : </label>
            <input name="notificationName" id="notificationName" type="text" value="Bonjour" />
        </fieldset>
        <br />
        <fieldset>
            <label> Server Key : </label>
            <input name="serverKey" id="serverKey" type="text" value="AAAAuTv1XVQ:APA91bHsgOmK-quki_rRehRhON9c_y9INocubgru6_jPePiE_Zt5iVXfJ-XD43RubfIY5WEoIpFEyziByfeNRsoIlpeNi693bGZYfjjb7ULDx23sRzHQcYLCgl7y3vn-K9X8hrmQhw1oY6lSTml2aqzoi8GGBIeZYA" />
        </fieldset>
        <br />
        <fieldset>
            <label> To mobile user : </label>
            <select name="selectMobile" id="selectMobile" style="width:400px" name="mobileUser">
                <option>Select Mobile User</option>
            </select>
        </fieldset>
        <br />
        <fieldset>
            <label> To topic : </label>
            <input name="topicName" id="topicName" type="text" value="news" />
        </fieldset>
        <br />
        <fieldset>
            <label> Message : </label>
            <textarea name="notificationMessage" id="notificationMessage" cols="40" rows="5">Comment tu vas toi ?</textarea>
        </fieldset>
        <br />
        <input type="submit" value="Send Notification" />
    </div>
</form>

HTML 渲染

enter image description here

所以我试图在 Angular 6 中用这个结果做同样的事情,但是当我想将路由“SendNotification”分配给我的“提交”按钮时,我收到以下错误:

Angular 渲染 + 错误

enter image description here

notification-form.component.html

<button [routerLink]="['SendNotification']" type="submit" class="btn btn-success" [disabled]="!notificationForm.form.valid">Submit</button>

当我添加 [routerLink] 或添加私有(private)构造函数时,错误就会发生:

 constructor(private router: Router) {}

到我的 notification-form.component.ts。 我已经尝试了几种解决方案,例如将 HttpClientModule 添加到我的 app.module.ts 但没有任何效果。

我做错了什么吗?

提前感谢您的宝贵时间!

更新:

app.module.ts

import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';
import { FormsModule } from '@angular/forms'; // <-- NgModel lives here
import { RouterModule } from '@angular/router';
import { HttpClientModule } from '@angular/common/http';

import { AppComponent } from './app.component';
import { NotificationFormComponent } from './notification-form/notification-form.component';

@NgModule({
  imports: [
    BrowserModule,
    HttpClientModule,
    FormsModule,
    RouterModule
  ],  
  declarations: [
    AppComponent,
    NotificationFormComponent
  ],
  providers: [],
  bootstrap: [AppComponent]
})

export class AppModule { }

最佳答案

第一次导入 RouteModuleapp.module.ts

import { RouterModule, Routes } from '@angular/router';

然后像下面这样使用它:(您只导入 RouterModule 但还需要添加 forRoot([])。)

imports: [
    RouterModule.forRoot([])
    // other imports here
  ]

如果您有任何路线,请使用它们,如下所示。此示例代码来自 Angular DOC

const appRoutes: Routes = [
  { path: 'crisis-center', component: CrisisListComponent },
  { path: 'hero/:id',      component: HeroDetailComponent },
  {
    path: 'heroes',
    component: HeroListComponent,
    data: { title: 'Heroes List' }
  },
  { path: '',
    redirectTo: '/heroes',
    pathMatch: 'full'
  },
  { path: '**', component: PageNotFoundComponent }
];

@NgModule({
  imports: [
    RouterModule.forRoot(
      appRoutes,
      { enableTracing: true } // <-- debugging purposes only
    )
    // other imports here
  ],
  ...
})
export class AppModule { }

然后在您的主要组件 ( app.component.html ) 中添加 <router-outlet></router-outlet>

希望对您有所帮助!

关于javascript - Angular 6 错误 "NullInjectorError: No provider for Router!",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51419602/

相关文章:

javascript - jQuery显示所有子节点的方法

javascript - Vue 组件的 prop 未定义( fatal error )

javascript - 如何在没有路径问题的情况下在母版页 header 中包含 jquery?

javascript - 套接字未连接到 Angular-Firebase 以获得实时更新

javascript - 浏览器列表错误 : Unknown version 67 of android

javascript - 内置浏览器 DOM 解析器 VS。 Javascript 框架解析器

javascript - 按钮功能 onclick 不显示在文本字段中

javascript - 如何从javascript访问代码隐藏中声明的数组

asp.net - 检查 IE 浏览器是否为 .NET

javascript - 传递给 `selectId` 实现的实体返回未定义