【问题标题】:Angular 6 Children/Parent CommunicationAngular 6 孩子/家长沟通
【发布时间】:2018-08-06 11:52:52
【问题描述】:

我在替换父组件中的 div 以包含子组件时遇到问题。 (我想要像 AJAX 但有角度的东西) 例如我有这个 html 父母和孩子:

parent-component.html

<div class="col-md-12">
  <h1>{{parent.name}}</h1>
  <p> this is the parent component div. click the link below to replace this div with the children div</p>
  <a>Click here</a>
  <children></children>
</div>



children-component.html

<div class="col-md-12">
  <h1>{{parent.children.name}}</h1>
  <p> this is the children div </p>
</div>

所以我基本上想要的是,如果我们点击链接,父组件文件中的整个 div 被子组件中的 div 替换(我们转到子路由 /parent/:id/children/:id )

编辑:我会解释为什么我需要它,这样我就清楚了。 我有课程部分。每门课程都包含一系列段。每个段都包含类型和数据属性。如果 type = video,则使用 url 归档数据类型。如果类型是测验,则数据为空。此外,每个部分都有一系列问题。如果类型是视频,则数组为空。

当我单击测验链接时,我想要一个具有“开始测验”链接的页面,当我单击它时,整个 div 将替换为问题列表(子组件)。

接口和关系:

export interface ICourse {
  id: number;
  title: string;
  autor: string;
  segments: ISegment[];
}

export interface ISegment {
  id: number;
  unit_id: number;
  unit_title: string;
  name: string;
  type: string;
  data: string;
  questions: IQuestion[];
}

export interface IQuestion {
  id: number;
  question: string;
  answer1: string;
  answer2: string;
  answer3: string;
  answer4: string;
  correct: number;
}

这是'parent'的html文件(子组件是course-quiz)

  <div class="row content" *ngIf="course">
    <!-- Side nav-bar -->
    <div class="col-md-3">
      <!-- Image Logo -->
      <div id="head_sidebar">
        <img src="./assets/images/lg-white.png" class="d-inline-block align-top logo" alt="" routerLink="/courses" style="outline: none">
        <h3>{{course.title}}</h3>
      </div>

      <div class="col-md-12 sidenav">
        <!-- Menu elemets -->
        <div class="nav nav-pills nav-stacked" *ngFor="let unit of course.segments | groupBy: 'unit_title'; let i = index">
          <h6 class="course_play_title">Unit {{ i+1 }}: {{ unit.key }} </h6>
          <ul>
            <li class="course_play_item"  *ngFor="let lesson of unit.value">
              <a class="nav-link" routerLink="/courses/{{course.id}}/segments/{{lesson.id}}" (click)=getCurrentSegment(lesson.id)>{{lesson.name}} </a>
            </li>
          </ul>
        </div>
      </div>
    </div>

    <!-- Body -->
    <div class="col-md-9 no-gutters" *ngIf="currentSegment">
      <!-- Video Div -->
      <div class="col-md-12 course_play_body text-center" *ngIf="currentSegment.segment_type === 'Video'">
        <h1>{{currentSegment.name}}</h1>
        <p class="small-text" *ngIf="course.segments?.length > 0">lesson {{currentSegment.id}} of {{course.segments?.length}}</p>
        <hr>
        <iframe frameborder="0" allowfullscreen="true" [src]='currentSegment.data | safe'></iframe>
      </div>

      <!-- Quiz Div -->
      <div class="col-md-12 course_play_body text-center" *ngIf="currentSegment.segment_type === 'Quiz'">
        <h1>{{currentSegment.name}}</h1>
        <p class="text-left"> Now that you've finished this unit, It's time to take a short quiz and see what you learned so far!
            You'll need to choose one out of four answers which you think is correct.
            After you've finished the quiz, you'll get your grade. feel free to re-take this quiz as much as you like.
            Good Luck!
        </p>
        <p class="big-text" *ngIf="currentSegment.questions?.lenght > 0"> {{currentSegment.questions?.lenght}} questions </p>
        <a><h4>Start Quiz</h4></a>
        <quiz-course></quiz-course>
      </div>
    </div>

  </div>

路由:

import { NgModule } from '@angular/core';
import { Routes, RouterModule } from '@angular/router';

import { CourseListComponent } from './courses/course-list/course-list.component';
import { CourseDetailComponent } from './courses/course-detail/course-detail.component';
import { CoursePlayComponent } from './courses/course-play/course-play.component';
import { PageNotFoundComponent } from './page-not-found/page-not-found.component';
import { CourseQuizComponent } from './courses/course-play/course-quiz/course-quiz.component';


// Routing array - set routes to each html page
const appRoutes: Routes = [
  { path: '', redirectTo: '/courses', pathMatch: 'full', runGuardsAndResolvers: 'always' },
  { path: 'courses', component: CourseListComponent,  pathMatch: 'full', runGuardsAndResolvers: 'always' },
  { path: 'courses/:id', component: CourseDetailComponent, pathMatch: 'full', runGuardsAndResolvers: 'always' },
  { path: 'courses/:id/segments/:id', component: CoursePlayComponent, pathMatch: 'full', runGuardsAndResolvers: 'always',
    children: [{ path: 'questions/:id', component: CourseQuizComponent }]
  },
  { path: '**', component: PageNotFoundComponent, pathMatch: 'full', runGuardsAndResolvers: 'always' }];

@NgModule({
  imports: [RouterModule.forRoot(appRoutes, { onSameUrlNavigation: 'reload' })],
  exports: [RouterModule]
})

export class AppRoutingModule {  }

【问题讨论】:

  • 这样做的目的和逻辑是什么?
  • 嗨,你注意点击后你需要路由到这个页面中的 url '/parent/:id/children/:id' '

    {{parent.children .name}}

    ' 对吗?
  • 在我看来,您可以使用 *ngIf="showChildren" 和标志或更好地使用 route.naviage(...) 并在新组件中处理此路线。更好读。
  • 使用inputoutputs
  • 我添加了我的情况以及我需要它的原因。这有点复杂,所以我没有在中提及

标签: html angular typescript parent children


【解决方案1】:

你应该像这样在应用模块中为你的子组件配置路由

const appRoutes: Routes = [
    { path: 'courses/:courseid/:lessonId', component: desire component },
];

在 imports 数组中导入为:

RouterModule.forRoot(appRoutes),

终于在 HTML 中

 <a class="nav-link" [routerLink]="['/courses',course.id,lesson.id]">{{lesson.name}} </a>

此外,你为什么在同一个锚标签上使用点击事件以及路由器链接?

注意:我没有测试代码我只是举个例子

【讨论】:

  • 我做了,但它不起作用。我现在添加了我的路由数组
  • 你也导入了路由器模块吗?
  • 是的,在其他组件中它正在工作,只是在这个它不是,它与 url 重复
  • 尝试解决 url 问题我只是举了一个简单的例子
【解决方案2】:

又快又脏,你可以这样解决:

<a *ngIf="!showQuiz" (click)="showQuiz = true"><h4>Start Quiz</h4></a>
<quiz-course *ngIf="showQuiz" [questions]="currentSegment?.questions"></quiz-course> 

你必须设置一个@Input() 问题;在您的子组件中。


补充,可以使用

route: ActivatedRoute,

在加载时设置 currentSegment。

【讨论】:

  • 只是对或错。 (加载时应该是假的)对不起这个名字,我会把它改成 showQuiz
  • 我使用了您的代码,但现在我遇到了一个问题,即路线没有改变,当我尝试输入 /courses/3/segments/7/questions/1 时,它没有显示
猜你喜欢
  • 2019-02-04
  • 2020-01-29
  • 2017-07-01
  • 2021-04-25
  • 1970-01-01
  • 2018-10-13
  • 1970-01-01
  • 2011-02-20
  • 2011-02-21
相关资源
最近更新 更多