【问题标题】:UUID npm packg is returing undefined .. I want to make id for link i try to generate id but it's retruing undefinedUUID npm packg is returing undefined .. I want to make id for link i try to generate id but it\'s retruing undefined
【发布时间】:2023-01-06 21:34:18
【问题描述】:

This is the model where I import the uuid pack

import * as uuid from "uuid";

export class Note {
  id: string;

  constructor(public title: string, public content: string) {
    this.id = uuid.v4();
  }
}

in the component class :

import { ActivatedRoute, ParamMap } from "@angular/router";
import { Component, OnInit } from "@angular/core";
 
export class EditNoteComponent implements OnInit {
    notes: Note[]
    constructor(private activateRouter: ActivateRouter)

ngOnInit(): void {

    this.activeRoute.paramMap.subscribe((paramMap: ParamMap) => {
      const idParam = paramMap.get("id");
      console.log(idParam);
    });
  }

and in the HTML file where I put the link so when the user clicks the notecard it should give me a unique id something like (9b1deb4d-3b7d-4bad-9bdd-2b0d7b3dcb6d) but then console only show undefine!!!

<div class="noteContainer">
    <div class="notes-lists">
      <a [routerLink]="[note.id]" *ngFor="let note of notes">
        <app-note-cards [note]="note"></app-note-cards>
      </a>
    </div>
  </div>

【问题讨论】:

    标签: angular typescript angular-ui-router syntax-error


    【解决方案1】:

    You've probably already solved this months ago.. But, try putting this in your constructor:

    activateRouter.params.subscribe(() => {
        if(route.snapshot.params.id){
    
            console.log(route.snapshot.params.id);
        }
    );
    

    I also notice that in your constructor parameters you have 'activateRouter' whereas in your ngOnInit you have 'activeRoute'

    【讨论】:

      猜你喜欢
      • 2022-11-20
      • 1970-01-01
      • 2022-12-01
      • 2022-12-26
      • 2022-09-27
      • 1970-01-01
      • 2022-12-02
      • 2011-12-24
      • 2022-12-19
      相关资源
      最近更新 更多