我想使用laravel和angular插入,我想设置HEADERS,但这给了我这个错误。
未找到模块:错误:无法在'D:\ ask \ src \ app \ service'中解析'@ angular / http'
laravel
public / index.php
setState
角形
app.module.ts
<?php
header('Access-Control-Allow-Origin: *');
header('Access-Control-Allow-Methods: GET, POST, PATCH, PUT, DELETE, OPTIONS');
header('Access-Control-Allow-Headers: Origin, Content-Type, X-Auth-Token');
/**
* Laravel - A PHP Framework For Web Artisans
*
* @package Laravel
* @author Taylor Otwell <taylor@laravel.com>
*/
task.service.ts
import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';
import { RouterModule , Routes} from '@angular/router';
import { RoutingModuleModule } from './routing-module.module';
import { FormsModule } from '@angular/forms';
import { HttpClientModule } from '@angular/common/http' ;
import { AppComponent } from './app.component';
import { HeaderComponent } from './components/header/header.component';
import { FooterComponent } from './components/footer/footer.component';
import { TaskManagerComponent } from './components/task-manager/task-manager.component';
import { TaskFormComponent } from './components/task-manager/task-form/task-form.component';
import { TaskListComponent } from './components/task-manager/task-list/task-list.component';
import { TaskDetailComponent } from './components/task-manager/task-detail/task-detail.component';
import { TaskItemComponent } from './components/task-manager/task-list/task-item/task-item.component';
import { from } from 'rxjs';
@NgModule({
declarations: [
AppComponent,
HeaderComponent,
FooterComponent,
TaskManagerComponent,
TaskFormComponent,
TaskListComponent,
TaskDetailComponent,
TaskItemComponent,
],
imports: [
BrowserModule,
RoutingModuleModule,
FormsModule,
HttpClientModule
],
providers: [],
bootstrap: [AppComponent]
})
export class AppModule { }
答案 0 :(得分:1)
您似乎正在使用不推荐使用的API和功能。
请阅读https://angular.io/guide/deprecations并查看您使用的模块。
import { HttpClient, HttpHeaders } from '@angular/common/http';
const httpOptions = {
headers: new HttpHeaders({ 'Content-Type': 'application/json' })
};
.
.
.
this.http.put<any>(url, o, httpOptions);
.
.
.
答案 1 :(得分:0)
在您的服务import { HttpHeaders, HttpRequest } from '@angular/common/http'
中,而不是已弃用的Headers
和RequestOptions
中。
答案 2 :(得分:0)
在task.service.ts中,而不是下面的代码
import { Headers , RequestOptions } from '@angular/http';
使用此:
import { HttpClient, HttpHeaders } from '@angular/common/http';
并设置如下选项:
const httpOptions = {
headers: new HttpHeaders({
'enctype':'multipart/form-data',
'Content-Type': 'application/json',
'X-Requested-with':'XMLHttpRequest'
})
};
addTask(title):Observable<Task>{
const newTask=new Task(title);
//console.log(newTask);
return this.http.post<Task>(this.serveur+'add',newTask, httpOptions);
}
注意:拦截器服务添加标头的最佳方法。 要添加拦截器,请参考:best way to add interceptor in angular
答案 3 :(得分:0)
请修改此工作代码
import { Injectable } from '@angular/core';
import { Task } from '../Task';
import { Observable } from 'rxjs';
import { HttpClient, HttpHeaders } from '@angular/common/http';
@Injectable({
providedIn: 'root'
})
export class TaskService {
Headers:Headers = new Headers();
Options:any;
const httpOptions = {
headers: new HttpHeaders({
'enctype':'multipart/form-data',
'Content-Type': 'application/json',
'X-Requested-with':'XMLHttpRequest'
})
};
serveur : string='http://127.0.0.1:8000/';
constructor(public http:HttpClient) {
this.Headers.append('enctype','multipart/form-data');
this.Headers.append('Content-Type','application/json');
this.Headers.append('X-Requested-with','XMLHttpRequest');
this.Options = new RequestOptions({Headers:this.Headers});
}
addTask(title):Observable<Task>{
const newTask=new Task(title);
//console.log(newTask);
return this.http.post<Task>(this.serveur+'add',newTask, httpOptions);
}
}