[英]How to use a component in another components folder
I am new to Angular, and wanted to have a page with the directory of /cities/denver
. 我是Angular的新手,并且想创建一个目录为/cities/denver
。 I have the file structure 我有文件结构
|-- app
|-- app.module.ts
|-- app-routing.module.ts
|-- [+] cities
|-- cities.component.html
|-- cities.component.scss
|-- cities.component.spec.ts
|-- cities.component.ts
|-- [+] denver
|-- denver.component.html
|-- denver.component.scss
|-- denver.component.spec.ts
|-- denver.component.ts
I then declared it in app.module.ts
然后我在app.module.ts
声明了它
import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';
import { NgbModule } from '@ng-bootstrap/ng-bootstrap';
import { FontAwesomeModule } from '@fortawesome/angular-fontawesome';
import { AppRoutingModule } from './app-routing.module';
import { AppComponent } from './app.component';
import { DashboardComponent } from './dashboard/dashboard.component';
import { CitiesComponent } from './cities/cities.component';
import { DenverComponent } from './cities/denver/denver.component';
@NgModule({
declarations: [
AppComponent,
DashboardComponent,
CitiesComponent,
DenverComponent
],
imports: [
BrowserModule,
AppRoutingModule,
NgbModule,
FontAwesomeModule
],
providers: [],
bootstrap: [AppComponent]
})
export class AppModule { }
and then in app-routing.module.ts
然后在app-routing.module.ts
import { NgModule } from '@angular/core';
import { RouterModule, Routes } from '@angular/router';
import { DashboardComponent } from './dashboard/dashboard.component';
import { CitiesComponent } from './cities/cities.component';
import { DenverComponent } from './cities/denver/denver.component';
const routes: Routes = [
{ path: '', redirectTo: 'dashboard', pathMatch: 'full' },
{ path: 'dashboard', component: DashboardComponent },
{ path: 'cities', component: CitiesComponent },
{ path: 'cities/denver', component: DenverComponent },
];
@NgModule({
imports: [ RouterModule.forRoot(routes) ],
exports: [ RouterModule ]
})
export class AppRoutingModule {}
I can get to the page fine using a routerLink, but when I type it into the browser or reload it I get a 404 error saying that runtime.js, polyfill.js, etc cannot be found. 我可以使用routerLink进入页面,但是当我在浏览器中键入或重新加载页面时,出现404错误,提示找不到runtime.js,polyfill.js等。
Now that I'm thinking about it, maybe cities needs to be a module? 现在,我正在考虑,也许城市需要成为一个模块? Any help is appreciated, thanks. 任何帮助表示赞赏,谢谢。
Issue sounds like it's related to how you're serving your application and not related to component folder nesting/structure. 问题听起来好像与您服务应用程序的方式有关,与组件文件夹的嵌套/结构无关。
You always need to serve index.html page with Angular routing otherwise you will be trying to pull in the server resource absolutely and not letting Angular routing take over. 您始终需要为Angular路由提供index.html页面,否则您将尝试绝对引入服务器资源,而不是让Angular路由接管。
Your first example with routerlink probably works because you've started the app with http://localhost:4200 and then clicked on link from there and thus not causing a server reload so you will still be within the Angular client side environment. 您第一个使用routerlink的示例可能会起作用,因为您已经使用http:// localhost:4200启动了应用程序,然后从那里单击了链接,因此不会导致服务器重新加载,因此您仍将位于Angular客户端环境中。
For example if you're serving your app via Web API you would do something like below to always force index.html to be served on a server request without changing your URL. 例如,如果您通过Web API提供应用程序,则可以执行以下操作,始终在不更改URL的情况下强制在服务器请求中提供index.html。
.
.
.
builder.Use((context, next) =>
{
context.Request.Path = new PathString("/index.html");
return next();
});
.
.
.
Routed apps must fallback to index.html: 路由的应用必须回退到index.html:
https://angular.io/guide/deployment#fallback https://angular.io/guide/deployment#fallback
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.