博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Angualr6访问API
阅读量:4568 次
发布时间:2019-06-08

本文共 3369 字,大约阅读时间需要 11 分钟。

参照 草根专栏- ASP.NET Core + Ng6 实战: 

 

1、environment.ts 添加apiUrlBase(资源访问Api地址):

export const environment = {  production: false ,  apiUrlBase: 'https://localhost:6001/api'};

2、添加父类service:

        ng g s shared/base

import { Injectable } from '@angular/core';import { environment } from '../../environments/environment';@Injectable({  providedIn: 'root'})export class BaseService {  apiUrlBase = environment.apiUrlBase;  constructor() { }}

 

3、添加 Post service

       ng g s blog/services/post

 

4、blog.module.ts 引用 service

providers: [    PostService  ]

 

5、ng g c blog/components/post-list

6、添加二层路由: sidenav.component.html

 

7、注册二层子路由

const routes: Routes = [  {    path: '', component: BlogAppComponent,    children : [      {path: 'post-list' , component: PostListComponent },      {path: '**' , redirectTo: 'post-list' }    ]}];

8、service获取数据:

 

9、跨域配置

public void ConfigureServices(IServiceCollection services)        {             //配置跨域            services.AddCors(options =>            {                options.AddPolicy("AllowAngularDevOrigin",                    builder => builder.WithOrigins("http://localhost:4200")                        .WithExposedHeaders("X-Pagination")                        .AllowAnyHeader()                        .AllowAnyMethod());            });            services.Configure
(options => { options.Filters.Add(new CorsAuthorizationFilterFactory("AllowAngularDevOrigin"));//跨域配置 var policy = new AuthorizationPolicyBuilder() .RequireAuthenticatedUser() .Build(); options.Filters.Add(new AuthorizeFilter(policy)); }); } // This method gets called by the runtime. Use this method to configure the HTTP request pipeline. public void Configure(IApplicationBuilder app, IHostingEnvironment env,ILoggerFactory loggerFactory) { app.UseCors("AllowAngularDevOrigin");//跨域配置 }

 

10、建立   proxy.conf.js  配置

const PROXY_CONFIG = [    {        context: [            "/api"        ],        target: "http://localhost:3000",        secure: false    }]module.exports = PROXY_CONFIG;

 

11、angular.json中配置:

        "proxyConfig": "src/proxy.conf.js"

 

12、获取api header数据:

getPosts() {    this.postService.getPagedPosts(this.postParameter).subscribe(resp => {      this.pageMeta = JSON.parse(resp.headers.get('X-Pagination')) as PageMeta;    });  }

13、获取body数据:

        建立entity.ts    post.ts   link.ts      result-with-links      page-meta.ts   接受body传输的数据:

 

14、post-list.component.ts 中解析

@Component({  selector: 'app-post-list',  templateUrl: './post-list.component.html',  styleUrls: ['./post-list.component.scss']})export class PostListComponent implements OnInit {  postParameter = new PostParameters({ orderBy: 'id desc', pageSize: 10, pageIndex: 0 });  posts: Post[];  pageMeta: PageMeta;  constructor(private postService: PostService) { }  ngOnInit() {    this.getPosts();  }  getPosts() {    this.postService.getPagedPosts(this.postParameter).subscribe(resp => {      this.pageMeta = JSON.parse(resp.headers.get('X-Pagination')) as PageMeta;      const result = {...resp.body} as ResultWithLinks
; this.posts = result.value; }); }}

 

15、post-list.component.html显示数据

{
{item.title}}

 

转载于:https://www.cnblogs.com/fuyouchen/p/9601816.html

你可能感兴趣的文章
apk文件分析原则
查看>>
GCC Inline ASM(转载)
查看>>
scrum 10.31
查看>>
synergy
查看>>
校内通知-Notifications表增加老师,家长,学生发送范围字段
查看>>
.NET下获取应用程序目录的一些方法
查看>>
9260与SAM-BA连接(转)
查看>>
Apache与Nginx的优缺点
查看>>
38.Subsets(子集和)
查看>>
贪心策略---判断是否为子序列
查看>>
python必备的库
查看>>
div css布局中CSS图片大小自动按比例等比例缩小图片不变形解决技巧(转)
查看>>
Android之ClassLoader的工作机制
查看>>
不要忽略'\'
查看>>
require php中引用函数
查看>>
字符串操作练习:星座、凯撒密码、99乘法表、词频统计预处理
查看>>
pycharm添加文件头注释
查看>>
Linux工具之Vim使用
查看>>
Arcgis for android 100.4天地图无法显示
查看>>
Enhanced PagedList for ASP.NET MVC
查看>>