我目前在一个项目中,我需要用户填写一个有角度的表格,然后将其发送到后端的路由中以处理数据。后端在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渲染
因此,我试图在Angular 6中执行相同的操作,但结果是,当我想将路由“ SendNotification”分配给我的“ Submit”按钮时,出现以下错误:
角度渲染+错误
notification-form.component.html
<button [routerLink]="['SendNotification']" type="submit" class="btn btn-success" [disabled]="!notificationForm.form.valid">Submit</button>
我添加[routerLink]
或添加私有构造函数时就会发生错误:
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 { }
RouteModule
中的第一个导入app.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>
希望这对您有帮助!
如果有人使用npm链接,则将执行以下操作:
在.angular-cli.json
中添加/替换:
"defaults": {
"styleExt": "css",
"component": {},
"build": {
"preserveSymlinks": true
}
}
您需要添加<router-outlet></router-outlet>
标签<router-outlet></router-outlet>
是您的应用将呈现不同路线的地方。例如,在app.component.html中,您可以具有:
<header>
<bt-toolbar></bt-toolbar>
</header>
<main>
<router-outlet></router-outlet>
</main>
有了这个模板,我的顶部栏始终可见,下面是不同路线的内容。
在imports数组中添加到您的app.module.ts中:
RouterModule.forRoot([
{ path: 'SendNotification', component: YourComponent }
就我而言,我只输入:
<base href="/">
在head
文件的index.html
标签中。
...
<head>
...
<title>Name of page</title>
<base href="/">
</head>
...
对于那些也很挣扎的人,使用Angular编译器发出的神秘/无助的错误消息,请确保您在某处有此信息:
RouterModule.forRoot([])
我仅使用RouterModule.forChild([])
,在其中一个模块中添加了forRoot()来修复错误。
请检查此链接https://angular.io/api/router/RouterStateSnapshot
import { Component, OnInit } from '@angular/core';
import { RouterState, RouterStateSnapshot, Router } from '@angular/router';
@Component({
templateUrl:'template.html'
})
export class MyComponent {
constructor(router: Router) {
const state: RouterState = router.routerState;
const snapshot: RouterStateSnapshot = state.snapshot;
console.log(state);
console.log(snapshot);
//...
}
}