简体   繁体   English

如何在另一个组件文件夹中使用组件

[英]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等。

Error message 错误信息

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.

相关问题 如何在另一个组件中有条件地添加组件 - How to add components conditionally inside another component 如何将组件放置在另一个组件下? - How to place components under another component? 如何从另一个组件更改组件状态? - How to change components states from another component? Angular 4 如何在同级模块组件中使用其他模块组件 - Angular 4 How do I use another modules components in a sibling modules component 如何使用样式组件来设置组件的打印样式? - how to use styled components to style the printing of a component? 如何在反应组件中使用样式组件 - How to use styled-components in react component 如何为作为 props 传递的组件使用样式化组件 - How to use Styled Components for a component passed as props 如何从功能组件中的另一个组件中的组件获取 id - How to get id from component in another component in functional components 如何基于单击将一个组件中存在的 function 的相同功能用于另一个组件以在 angular 中的两个组件中使用相同的 class 绑定 - How to use same functionalities of function present in one component to another based on click to use same class binding in both components in angular 如何在另一个组件上使用来自组件的信息 - How to use the info from a component on another component
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM