码迷,mamicode.com
首页 > 其他好文 > 详细

angular的跨域

时间:2020-01-11 14:54:34      阅读:76      评论:0      收藏:0      [点我收藏+]

标签:rom   创建   路径   nbsp   mon   port   art   package   根目录   

1.在根目录下创建一个proxy.config.json文件

proxy.config.json

{
    "/api":{
        "target":"http://192.168.1.116:3000/mock/41",//你所要跨域的地址
        "secure":false,//是否开启跨域保护 https的时候 使用该参数
        "logLevel":"debug",//日志级别的调试
        "changeOrigin":true,//是否跨域
        "pathRewirte":{//重写路径
            "^/api":""
        }
    }
}
 
2.在package,json文件中配置或是在angular.json文件中配置
packae.json
在"scripts"中的"start"的值后面加
--proxy-config proxy.config.json
在angular.json中的"serve"下的"options"下加
"proxyConfig":proxy.config.json,
”disableHostCheck“:true
 
3.在app.module.ts中引入
import { HttpClientModule } from ‘@angular/common/http‘
在imports中注入
imports:[
  HttpClientModule
]
 
4.在组件的 组件.component.ts中引入http
import {HttpClient} from ‘@angular/common/http‘
constructor(private http:HttClient){
}
 
 
 

angular的跨域

标签:rom   创建   路径   nbsp   mon   port   art   package   根目录   

原文地址:https://www.cnblogs.com/violinh/p/12179813.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!