Angular 开发问题记录
1,can't bind to 'ngModel' since it isn't a known property of 'input'
解决: ngModel 指令存在于 npm package: @angular/forms中,需要引入
import { FormsModule } from "@angular/forms";
并加入到imports中:
imports: [
FormsModule
]
2,angular cli报错:TypeError: Cannot read property 'NullLogger' of undefined
解决: 重新安装angular-cli
npm uninstall -g angular-cli npm cache clean --force npm install -g @angular/cli@latest
3,angular-cli 如何使用代理api
解决:
angular-cli启动前端项目的地址例如为:http://localhost:4200,后台服务的地址为http://localhost:9999,如果直接将请求地址写成http://localhost:9999/api,会发生CORS跨域错误。
首先在根目录下新建proxy.config.json:
{ "/api-admin": { "target": "http://localhost:9999", "changeOrigin":true } }
然后在package.json中,配置
"start": "ng serve --proxy proxy.config.json"
此时对后端的请求就会被代理到9999端口。
在http请求时如下,
this.dataSource = this.http.get("/api-admin/login?username="+this.username+"&password="+this.password,{}).map((res)=> res.json()); this.dataSource.subscribe(function (res) { console.log(res); }, function (err) { console.log(err); });