V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
dfgxcvbcv
V2EX  ›  程序员

同学的前端作业,大家觉得这代码什么水平?

  •  
  •   dfgxcvbcv · 2022-05-12 03:13:58 +08:00 · 601 次点击
    这是一个创建于 882 天前的主题,其中的信息可能已经有所发展或是发生改变。
    import {Component, HostListener, OnInit} from '@angular/core';
    import {MenuItem, MessageService} from "primeng/api";
    import {HttpClient} from "@angular/common/http";
    import {ActivatedRoute, ParamMap, Router} from "@angular/router";
    import {CategoryForm} from "./category-form";
    
    @Component({
      selector: 'app-articles-category-add-edit',
      templateUrl: './category-add-edit.component.html',
      providers: [MessageService]
    })
    export class CategoryAddEditComponent implements OnInit {
      apiUrl = "/api/Articles/Categories";
    
      loading = true;
      model = new CategoryForm("", "");
      home: MenuItem = {icon: 'pi pi-home', routerLink: '/admin/dashboard'};
      paths = [
        {label: '管理文章', routerLink: '/admin/articles'},
        {label: '新建文章分类'}
      ];
      categoryId?: string;
      invalidNameReason = "";
      pendingChanges = false;
      saving = false;
    
      constructor(private http: HttpClient,
                  private messageService: MessageService,
                  private route: ActivatedRoute, private router: Router) {
      }
    
      ngOnInit() {
        this.route.paramMap.subscribe((params: ParamMap) => {
          if (params.get('id') != "create") {
            this.categoryId = params.get('id');
            this.paths[1].label = "编辑文章分类 " + this.categoryId;
            this.http.get(this.apiUrl + this.categoryId).subscribe(data => {
              this.model = data as CategoryForm;
              this.loading = false;
            });
          } else this.loading = false;
        })
      }
    
      @HostListener('window:beforeunload')
      canDeactivate() {
        return !this.pendingChanges;
      }
    
      save() {
        if (this.saving) return;
    
        this.model.categoryName = this.model.categoryName.trim();
        if (this.model.categoryName.length == 0) {
          this.invalidNameReason = "分类名称不能为空。";
          return;
        }
        this.model.categoryNameEn = this.model.categoryNameEn.trim();
        this.saving = true;
    
        const url = this.categoryId ? `${this.apiUrl}/${this.categoryId}` : this.apiUrl;
    
        this.http[this.categoryId?"put":"post"](url, this.model).subscribe(
          () => {
            this.pendingChanges = false;
            this.router.navigate(['../../'], {relativeTo: this.route});
          },
          () => {
            this.messageService.add({severity: 'error', summary: '错误', detail: '保存失败。'});
            this.saving = false;
          }
        );
      }
    }
    
    目前尚无回复
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   实用小工具   ·   971 人在线   最高记录 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 20ms · UTC 20:28 · PVG 04:28 · LAX 13:28 · JFK 16:28
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.