我在Angular中遇到了一个组件问题,我试图将Angular与PHP连接起来。
我在app.component.ts
中有这段代码import { Component, OnInit } from '@angular/core';
import { ArticulosService } from './articulos.service';
import { HttpClient } from '@angular/common/http';
@Component({
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.css']
})
export class AppComponent {
title = 'angular-php';
articulos = null;
art = {
codigo: null,
descripcion: null,
precio: null
};
constructor(private articulosServicio: ArticulosService) {}
ngOnInit(){
this.MostrarTodos();
}
MostrarTodos(){
this.articulosServicio.mostrarTodos().subscribe(result => this.articulos = result);
}
Agregar() {
this.articulosServicio.agregar(this.art).subscribe(datos => {
if (datos['resultado'] === 'OK') {
alert(datos['mensaje']);
this.MostrarTodos();
}
});
}
Eliminar(codigo) {
this.articulosServicio.eliminar(codigo).subscribe(datos => {
if (datos['resultado'] === 'OK') {
alert(datos['mensaje']);
this.MostrarTodos();
}
});
}
Modificar() {
this.articulosServicio.update(this.art).subscribe(datos => {
if (datos['resultado'] === 'OK') {
alert(datos['mensaje']);
this.MostrarTodos();
}
});
}
}
我有两个问题。
第一个问题
第二个问题
我尝试了很多东西,但是就像我说的,我是Angular的新手。
如果有人能帮助我,我会很感激的。
Typescript是你的问题。你应该谷歌一下"初学者打字教程"。
Problem1:当你写
art = {
codigo: null,
descripcion: null,
precio: null
};
你说codigo有类型null
。这就是:
的意思。应该是像
art = {
codigo:string = null,
descripcion:string = null,
precio:string = null
};
表示codigo
具有类型字符串和值null
问题2:Angular不知道typedatos
是什么。
创建一个这样的类/接口:
public class Datos{
mensaje:string
resultado:string
}
像这样使用:
Agregar() {
this.articulosServicio.agregar(this.art).subscribe(datos:Datos => {
if (datos.resultado === 'OK') {
alert(datos.mensaje);
this.MostrarTodos();
}
});
}