要向Google表单提交数据,您可以使用Angular 7的HttpClient模块发送POST请求。以下是一个示例解决方法:
npm install @angular/common@7.0.0 @angular/core@7.0.0 @angular/forms@7.0.0 @angular/http@7.0.0 rxjs@6.3.3
form.service.ts
:import { Injectable } from '@angular/core';
import { HttpClient } from '@angular/common/http';
@Injectable({
providedIn: 'root'
})
export class FormService {
private googleFormUrl = 'https://docs.google.com/forms/d/e/{YOUR_FORM_ID}/formResponse';
constructor(private http: HttpClient) { }
submitForm(data: any) {
return this.http.post(this.googleFormUrl, data);
}
}
请注意,您需要将{YOUR_FORM_ID}
替换为您自己的Google表单ID。您可以在Google表单的URL中找到该ID。
FormService
来提交表单数据。例如,创建一个名为form.component.ts
的组件:import { Component } from '@angular/core';
import { FormService } from './form.service';
@Component({
selector: 'app-form',
template: `
`,
})
export class FormComponent {
constructor(private formService: FormService) { }
submitForm() {
// Get form data here
const formData = {
// Add your form data here
};
this.formService.submitForm(formData).subscribe(
response => {
console.log('Form submitted successfully', response);
// Do something with the response
},
error => {
console.error('Error submitting form', error);
}
);
}
}
在上面的示例中,您需要根据您的表单字段添加适当的表单数据。
请记住,在FormService
中导入了HttpClient
模块,以便发送HTTP请求。确保在您的组件中也导入了该模块。
这就是向Google表单提交数据的一个示例解决方法。您可以根据您的具体需求进行修改和调整。