首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如何以角度从本地Web (ASP.NET Core)获取数据

如何以角度从本地Web (ASP.NET Core)获取数据
EN

Stack Overflow用户
提问于 2021-05-14 12:44:17
回答 1查看 1.1K关注 0票数 0

下面是我生成数据以创建Treeview时的代码:

代码语言:javascript
运行
复制
import { Injectable } from '@angular/core';
export class Product {
     id: string;
     text: string;
     expanded?: boolean;
     items?: Product[];
     price?: number;
     image?: string;
}

我以这样的角度创建了数据:

代码语言:javascript
运行
复制
var products: Product[] = [{
id: "1",
text: "Stores",
expanded: true,
items: [{
    id: "1_1",
    text: "Super Mart of the West",
    expanded: true,
    items: [{
        id: "1_1_1",
        text: "Video Players",
        items: [{
            id: "1_1_1_1",
            text: "HD Video Player",
            price: 220,
            image: "images/products/1.png"
        }, {
            id: "1_1_1_2",
            text: "SuperHD Video Player",
            image: "images/products/2.png",
            price: 270
        }]
    }, {
        id: "1_1_2",
        text: "Televisions",
        expanded: true,
        items: [{
            id: "1_1_2_1",
            text: "SuperLCD 42",
            image: "images/products/7.png",
            price: 1200
        }, {
            id: "1_1_2_2",
            text: "SuperLED 42",
            image: "images/products/5.png",
            price: 1450              
        }
            ...
        }]
    }

这是我的输出以角的形式运行:

我不想用这种方式设置数据。我可以从本地主机web (从ASP.NET核心创建)获取数据吗?

谢谢你的帮助。我只是个新手,用角(请原谅我的问题是愚蠢的)!

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2021-05-15 04:03:29

我不想用这种方式设置数据。我可以从本地主机web (从ASP.NET核心创建)获取数据吗?

如果您想从您的角度前端向API后端发出http请求以获取数据,正如注释中所提到的那样,您可以尝试使用HttpClient。

注入HttpClient服务

代码语言:javascript
运行
复制
import { HttpClient } from '@angular/common/http';

提出http请求

代码语言:javascript
运行
复制
products: Product[];

GetProducts() {
    this.http.get<Product[]>('https://localhost:44386/api/nodeitems')
    .subscribe(data => {
      this.products = data;
    });
}

此外,您可能需要启用app后端应用程序上的CORS来设置允许的源。

https://learn.microsoft.com/en-us/aspnet/core/security/cors?view=aspnetcore-5.0

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/67534561

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档