简体   繁体   English

到组件内部组件的角度路线

[英]Angular routes to components inside a component

I´m building a nav component in angular, that allows the user to go to a component inside the App.component. 我正在构建一个有角度的导航组件,该组件允许用户转到App.component内部的组件。 This nav component is also inside the App component. 该导航组件也位于App组件内部。 All of the components are shown from the begin, i wanted the nav was able to find the component that is already shown in the App component. 从一开始就显示了所有组件,我希望导航能够找到App组件中已经显示的组件。

I tried to put the nav in the App component hardcoded, but it didn't work. 我试图将nav放入经过硬编码的App组件中,但是没有用。

This is my App.component.html where i call all of the componentes, this is the parent component. 这是我的App.component.html,我在其中调用所有组件,这是父组件。

<app-slideshow></app-slideshow>
<!--others-->
<app-dashboard></app-dashboard>  //<-- component where the nav is
<router-outlet></router-outlet>

This is my app-routing.module.ts where i defined the routes to the components 这是我的app-routing.module.ts,其中定义了到组件的路由

const parentModuleRoutes: Routes = [
{
  path: 'home',            //<---- parent component declared here
  component: AppComponent,
  children: [                          //<---- child components 
declared here
      {
          path:'slideshow',
          component: SlideshowComponent
      },
      {
          path:'about',
          component: AboutComponent
      },
      {
          path:'service',
          component: ServiceComponent
      },
      {
          path:'explore',
          component: WorksComponent
      },
      {
        path:'contact',
        component: ContactComponent
    },
  ]
 }
];

This is the dashboard component where the nav element is build and where I want to create the link with a component 这是构建nav元素的仪表板组件,也是我想与组件创建链接的地方

<nav>
<ul>
  <li>
  <a routerLink="/home/slideshow">
    <p>Home</p>
  </a>
</li>
<li>
  <a routerLink="/home/about">
    <p>About Us</p>
  </a>
</li>
<li>
    <a routerLink="/home/service">
      <p>Service</p>
    </a>
  </li>
  <li>
    <a routerLink="/home/explore">
      <p>Explore</p>
    </a>
  </li>
  <li>
    <a routerLink="/home/contact">
      <p>Contact Us</p>
    </a>
  </li>
</ul>
</nav>

The console prints this three errors, because the router-outlet is defined on the app component(parent): 控制台会显示这三个错误,因为路由出口是在应用程序组件(父)上定义的:

ERROR Error: StaticInjectorError(AppModule)[RouterOutlet -> 
ChildrenOutletContexts]: 
StaticInjectorError(Platform: core)[RouterOutlet -> 
ChildrenOutletContexts]: 
NullInjectorError: No provider for ChildrenOutletContexts!

ERROR CONTEXT DebugContext_
View_AppComponent_0 @ AppComponent.html:16


Error: StaticInjectorError(AppModule)[RouterOutlet -> 
ChildrenOutletContexts]: 
StaticInjectorError(Platform: core)[RouterOutlet -> 
ChildrenOutletContexts]: 
NullInjectorError: No provider for ChildrenOutletContexts!

The trick is create the layout components and call it in the app.component, so when u want to change to another component with a navbar for exemple, u can use it go to /about or /service, with changing the rest of the layout. 诀窍是创建布局组件,然后在app.component中调用它,因此当您想更改为带有导航栏的另一个组件时,您可以将其用于/ about或/ service,并更改其余布局。

App.component.html App.component.html

<app-header></app-header>
<router-outlet></router-outlet>
<app-footer></app-footer>

app-routing.module.ts app-routing.module.ts

const parentModuleRoutes: Routes = [
      {
          path:'slideshow',
          component: SlideshowComponent
      },
      {
          path:'about',
          component: AboutComponent
      },
      {
          path:'service',
          component: ServiceComponent
      },
      {
          path:'explore',
          component: WorksComponent
      },
      {
        path:'contact',
        component: ContactComponent
    }

];

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM