Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/421.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript Can';t在动态路由中获取端口3000_Javascript_Angular - Fatal编程技术网

Javascript Can';t在动态路由中获取端口3000

Javascript Can';t在动态路由中获取端口3000,javascript,angular,Javascript,Angular,目前,我正在努力从本地主机4200上提供的Angular应用程序中获取localhost:3000 角度版本:9.1.7 environment.ts(保存API url的位置): blogpost.component.ts(我遇到问题的组件): 有关此问题的其他一些信息: import { Component, OnInit } from "@angular/core"; import { environment } from "../../environme

目前,我正在努力从本地主机4200上提供的Angular应用程序中获取localhost:3000

  • 角度版本:9.1.7
environment.ts(保存API url的位置):

blogpost.component.ts(我遇到问题的组件):

有关此问题的其他一些信息:

import { Component, OnInit } from "@angular/core";
import { environment } from "../../environments/environment";

@Component({
    selector: "app-blogpost",
    templateUrl: "./blogpost.component.html",
    styleUrls: [ "./blogpost.component.scss" ]
})
export class BlogpostComponent implements OnInit {
    id: string;
    postdata;

    async ngOnInit() {
        console.log(`${environment.apiURL}/api/posts/get/id/${this.id}`);
        // This console.logs http://localhost:3000/api/posts/get/id/undefined
        let req = await fetch(`${environment.apiURL}/api/posts/get/id/${this.id}`);
        // This sends a request to http://localhost:4200/api/posts/get/id/someid
        this.postdata = await req.json();
    }
}
  • 我已经试过使用代理了,但不起作用

  • fetch命令向其他组件上的端口3000发送请求

  • 这个组件与其他组件的唯一区别在于它有一个动态路由

import { Component, OnInit } from "@angular/core";
import { environment } from "../../environments/environment";

@Component({
    selector: "app-blogpost",
    templateUrl: "./blogpost.component.html",
    styleUrls: [ "./blogpost.component.scss" ]
})
export class BlogpostComponent implements OnInit {
    id: string;
    postdata;

    async ngOnInit() {
        console.log(`${environment.apiURL}/api/posts/get/id/${this.id}`);
        // This console.logs http://localhost:3000/api/posts/get/id/undefined
        let req = await fetch(`${environment.apiURL}/api/posts/get/id/${this.id}`);
        // This sends a request to http://localhost:4200/api/posts/get/id/someid
        this.postdata = await req.json();
    }
}
import { BlogpostComponent } from "./blogpost/blogpost.component";
...
{ path: "post/:id", component: BlogpostComponent }