如何使用routerLink将用户数据传递给其他组件

发布于 2025-01-11 12:55:46 字数 2029 浏览 0 评论 0原文

我用烧瓶以角度工作。我有包含教师名单的数据库 我可以在 Teachers.component.html 中显示教师列表。 目标:当我按下给定用户的按钮时,我希望网站通过显示 routerLink 的新组件来向我显示包含该教师数据的用户配置文件。我的

teacher.component.html:

<div *ngFor="let teacher of teachersList">
  <button (click)="teachersave(teacher.title)" routerLink="teacher" >Show teacher</button> {{teacher.title}}
 </div>

teacher.component.ts:

import {Component, Input, OnInit, Output} from '@angular/core';

import {Subscription} from "rxjs";
import {Teachers} from "../../teachers.model";
import {TeachersApiService} from "../../api.services";


@Component({
  selector: 'app-teachers',
  templateUrl: './teachers.component.html',
  styleUrls: ['./teachers.component.css']
})
export class TeachersComponent implements OnInit {


  teachersListSubs: Subscription = new Subscription;
  teachersList: Teachers[] = [];
  teacher: Teachers = new Teachers('','');
  
  constructor(private teachersApi: TeachersApiService) {
  }



teachersave(event:string){
    this.title2 = event}
  title2 = this.teacher.title
  ngOnInit() {
    this.teachersListSubs = this.teachersApi.getTeachers().subscribe({
        next: res => this.teachersList = res,
        error: console.error
      }
    )
  }
  ngOnDestroy() {
    this.teachersListSubs.unsubscribe();
    
  }
}

teacher.Component.ts

import {Component, Input, OnInit} from '@angular/core';

import * as ts from 'src/management/teachers/teachers.component'
import {ActivatedRoute} from "@angular/router";


@Component({
  selector: 'app-teacher',
  templateUrl: './teacher.component.html',
  styleUrls: ['./teacher.component.css']
})
export class TeacherComponent implements OnInit {

  title3 = ts.TeachersComponent.arguments.title2 // <------??????????


  ngOnInit(): void {

  }

}

Teacher.component.html

<div>
{{title3}}
<router-outlet></router-outlet>
</div>

有一些操作方式,使用Linkrouter传输数据库对象数据到其他组件

I work in angular with flask. I have database with list of teachers
I can displaying a list of teachers in teachers.component.html.
goal: When I press the button for a given user, I want the site to show me a user profile with that teacher's data by displaying a new component from routerLink. I

teachers.component.html:

<div *ngFor="let teacher of teachersList">
  <button (click)="teachersave(teacher.title)" routerLink="teacher" >Show teacher</button> {{teacher.title}}
 </div>

teachers.component.ts:

import {Component, Input, OnInit, Output} from '@angular/core';

import {Subscription} from "rxjs";
import {Teachers} from "../../teachers.model";
import {TeachersApiService} from "../../api.services";


@Component({
  selector: 'app-teachers',
  templateUrl: './teachers.component.html',
  styleUrls: ['./teachers.component.css']
})
export class TeachersComponent implements OnInit {


  teachersListSubs: Subscription = new Subscription;
  teachersList: Teachers[] = [];
  teacher: Teachers = new Teachers('','');
  
  constructor(private teachersApi: TeachersApiService) {
  }



teachersave(event:string){
    this.title2 = event}
  title2 = this.teacher.title
  ngOnInit() {
    this.teachersListSubs = this.teachersApi.getTeachers().subscribe({
        next: res => this.teachersList = res,
        error: console.error
      }
    )
  }
  ngOnDestroy() {
    this.teachersListSubs.unsubscribe();
    
  }
}

teacher.Component.ts

import {Component, Input, OnInit} from '@angular/core';

import * as ts from 'src/management/teachers/teachers.component'
import {ActivatedRoute} from "@angular/router";


@Component({
  selector: 'app-teacher',
  templateUrl: './teacher.component.html',
  styleUrls: ['./teacher.component.css']
})
export class TeacherComponent implements OnInit {

  title3 = ts.TeachersComponent.arguments.title2 // <------??????????


  ngOnInit(): void {

  }

}

teacher.component.html

<div>
{{title3}}
<router-outlet></router-outlet>
</div>

There is some way to operate, transport database object data to other components using Linkrouter

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

笑红尘 2025-01-18 12:55:46

教师.component.html:

<div *ngFor="let teacher of teachersList">
  <button (click)="teachersave(teacher.title)">Show teacher</button> {{teacher.title}}
 </div>

教师.component.ts:

   import {Component, Input, OnInit, Output} from '@angular/core';
    
    import {Subscription} from "rxjs";
    import {Teachers} from "../../teachers.model";
    import {TeachersApiService} from "../../api.services";
    
    
    @Component({
      selector: 'app-teachers',
      templateUrl: './teachers.component.html',
      styleUrls: ['./teachers.component.css']
    })
    export class TeachersComponent implements OnInit {
    
    
      teachersListSubs: Subscription = new Subscription;
      teachersList: Teachers[] = [];
      teacher: Teachers = new Teachers('','');
      
      constructor(private teachersApi: TeachersApiService) {
      }
    
    
    
    teachersave(event:string){
        this.title2 = event
 this.router.navigate([`/teacher`],
      {
        state: {
          data: {
           title:teacher.title
          }
        }
      });
}


      title2 = this.teacher.title
      ngOnInit() {
        this.teachersListSubs = this.teachersApi.getTeachers().subscribe({
            next: res => this.teachersList = res,
            error: console.error
          }
        )
      }
      ngOnDestroy() {
        this.teachersListSubs.unsubscribe();
        
      }
    }

教师.Component.ts

import {Component, Input, OnInit} from '@angular/core';

import * as ts from 'src/management/teachers/teachers.component'
import {ActivatedRoute} from "@angular/router";


@Component({
  selector: 'app-teacher',
  templateUrl: './teacher.component.html',
  styleUrls: ['./teacher.component.css']
})
export class TeacherComponent implements OnInit {

  title3 = history.state?.data?.title;


  ngOnInit(): void {

  }

}

teachers.component.html:

<div *ngFor="let teacher of teachersList">
  <button (click)="teachersave(teacher.title)">Show teacher</button> {{teacher.title}}
 </div>

teachers.component.ts:

   import {Component, Input, OnInit, Output} from '@angular/core';
    
    import {Subscription} from "rxjs";
    import {Teachers} from "../../teachers.model";
    import {TeachersApiService} from "../../api.services";
    
    
    @Component({
      selector: 'app-teachers',
      templateUrl: './teachers.component.html',
      styleUrls: ['./teachers.component.css']
    })
    export class TeachersComponent implements OnInit {
    
    
      teachersListSubs: Subscription = new Subscription;
      teachersList: Teachers[] = [];
      teacher: Teachers = new Teachers('','');
      
      constructor(private teachersApi: TeachersApiService) {
      }
    
    
    
    teachersave(event:string){
        this.title2 = event
 this.router.navigate([`/teacher`],
      {
        state: {
          data: {
           title:teacher.title
          }
        }
      });
}


      title2 = this.teacher.title
      ngOnInit() {
        this.teachersListSubs = this.teachersApi.getTeachers().subscribe({
            next: res => this.teachersList = res,
            error: console.error
          }
        )
      }
      ngOnDestroy() {
        this.teachersListSubs.unsubscribe();
        
      }
    }

teacher.Component.ts

import {Component, Input, OnInit} from '@angular/core';

import * as ts from 'src/management/teachers/teachers.component'
import {ActivatedRoute} from "@angular/router";


@Component({
  selector: 'app-teacher',
  templateUrl: './teacher.component.html',
  styleUrls: ['./teacher.component.css']
})
export class TeacherComponent implements OnInit {

  title3 = history.state?.data?.title;


  ngOnInit(): void {

  }

}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文