Javascript 添加路由时,Angular Cli应用程序停止加载
我正在尝试一种新的Angular Cli应用程序,由于某种原因,当我添加路由时,主页(仪表板)停止加载 下面是我要添加路由的组件:Javascript 添加路由时,Angular Cli应用程序停止加载,javascript,ruby-on-rails,angular,angular-cli,Javascript,Ruby On Rails,Angular,Angular Cli,我正在尝试一种新的Angular Cli应用程序,由于某种原因,当我添加路由时,主页(仪表板)停止加载 下面是我要添加路由的组件: import { Component, OnInit } from '@angular/core'; import { OrganizationService } from './shared/organization.service'; import {Organization} from "./shared/organization"; import { Rou
import { Component, OnInit } from '@angular/core';
import { OrganizationService } from './shared/organization.service';
import {Organization} from "./shared/organization";
import { Router, ActivatedRoute } from '@angular/router';
@Component({
selector: 'app-organization',
templateUrl: './organizations.component.html',
styleUrls: ['./organizations.component.scss']
})
export class OrganizationsComponent implements OnInit {
title: string;
organization: Organization = new Organization();
constructor(
private organizationService: OrganizationService,
private router: Router,
private route: ActivatedRoute
) { }
ngOnInit() {
var id = this.route.params.subscribe(params => {
var id = params['id'];
this.title = id ? 'Edit Faq Organization' : 'Create Faq Organization';
if (!id)
return;
this.organizationService.getOrganization(id)
.subscribe(
organization => this.organization = organization,
response => {});
});
}
save() {
var result;
if (this.organization.id){
result = this.organizationService.updateOrganization(this.organization);
} else {
result = this.organizationService.addOrganization(this.organization);
}
result.subscribe(data => this.router.navigate(['/']));
}
}
这就是模型:
export class Organization {
id: number;
nome: string;
sigla: string;
cnpj: string;
}
这是我的服务:
import { Injectable } from '@angular/core';
import { Http } from '@angular/http';
import 'rxjs/add/operator/map';
import 'rxjs/add/operator/do';
import 'rxjs/add/operator/catch';
import { Observable } from 'rxjs/Rx';
@Injectable()
export class OrganizationService {
private url: string = "http://localhost:3000/organizations";
constructor(private http: Http) { }
getOrganizations(){
return this.http.get(this.url)
.map(res => res.json());
}
getOrganization(id){
return this.http.get(this.url + '/' + id)
.map(res => res.json());
}
addOrganization(organization){
return this.http.post(this.url, {'organization': organization})
.map(res => res.json());
}
updateOrganization(organization){
return this.http.put(this.url + '/' + organization.id, {'organization': organization})
.map(res => res.json());
}
deleteOrganization(id){
return this.http.delete(this.url + '/' + id)
.map(res => res.json());
}
}
路线:
import { NgModule } from '@angular/core';
import { Routes, RouterModule } from '@angular/router';
import { ModuleWithProviders } from '@angular/core';
//Layouts
import { OrganizationsComponent } from './organizations/organizations.component';
import { FullLayoutComponent } from './layouts/full-layout.component';
export const routes: Routes = [
{
path: '',
redirectTo: 'dashboard',
pathMatch: 'full',
},
{
path: '',
component: FullLayoutComponent,
data: {
title: 'Página Inicial'
},
children: [
{
path: 'dashboard',
loadChildren: './dashboard/dashboard.module#DashboardModule'
},
]
},
//THIS IS THE PART THAT MAKES THE PAGE NOT LOAD
{ path: 'organizations/new', component: OrganizationsComponent},
{ path: 'organizations/:id', component: OrganizationsComponent},
{ path: 'organizations/:id/edit', component: OrganizationsComponent},
];
export const routing: ModuleWithProviders = RouterModule.forRoot(routes);
@NgModule({
imports: [ RouterModule.forRoot(routes) ],
exports: [ RouterModule ]
})
export class AppRoutingModule {}
我是rails和angular的新手,如果我遗漏了一些明显的东西,我很抱歉
谢谢
@编辑
这也是我的app.module:
import { NgModule } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import { LocationStrategy, HashLocationStrategy } from '@angular/common';
import { AppComponent } from './app.component';
import { BsDropdownModule } from 'ng2-bootstrap/dropdown';
import { TabsModule } from 'ng2-bootstrap/tabs';
import { NAV_DROPDOWN_DIRECTIVES } from './shared/nav-dropdown.directive';
import { SIDEBAR_TOGGLE_DIRECTIVES } from './shared/sidebar.directive';
import { AsideToggleDirective } from './shared/aside.directive';
import { BreadcrumbsComponent } from './shared/breadcrumb.component';
// Routing Module
import { AppRoutingModule } from './app.routing';
//Layouts
import { FullLayoutComponent } from './layouts/full-layout.component';
import { OrganizationsComponent } from './organizations/organizations.component';
import { OrganizationService } from './organizations/shared/organization.service';
@NgModule({
imports: [
BrowserModule,
AppRoutingModule,
BsDropdownModule.forRoot(),
TabsModule.forRoot(),
],
declarations: [
AppComponent,
FullLayoutComponent,
NAV_DROPDOWN_DIRECTIVES,
BreadcrumbsComponent,
SIDEBAR_TOGGLE_DIRECTIVES,
AsideToggleDirective,
// OrganizationsComponent
],
providers: [OrganizationService,
{
provide: LocationStrategy,
useClass: HashLocationStrategy
}],
bootstrap: [ AppComponent ]
})
export class AppModule { }
// Dependências do Angular 2
import { FormsModule } from '@angular/forms';
import { HttpModule } from '@angular/http';
// Adicionamos o arquivo routing
import { routing } from './app.routing';
组织组件的声明将被注释掉
@NgModule({
// ...
声明:[
// ...
//这里修正:这不应该被注释掉。
组织组件
],
// ...
})
导出类AppModule{}
已解决,我缺少表单模块导入
import { FormsModule } from '@angular/forms';
你可以发布你的角路由器模块的路由配置吗?对不起,忘记了最重要的部分,哈哈,刚刚添加了它!t还有一件事,你能添加你的app.module.ts文件吗?是否可能未将组织组件
添加到NgModule.declarations
数组中?我还注意到,您有两条路径为路径:'
。您确定这不会导致错误吗?(检查您的浏览器控制台)如果我删除页面loadsyep的{path:'organizations/new',component:OrganizationsComponent},{path:'organizations/:id',component:OrganizationsComponent},{/path:'OrganizationsComponent/:id',component:OrganizationsComponent},
部分,我在测试时评论说,如果我取消注释,问题仍然存在,页面无法加载。我需要对声明和路线进行注释,以使其加载。