Angular 在typescript中编辑JSON文件
我正在尝试更新资产文件夹Angular 在typescript中编辑JSON文件,angular,typescript,Angular,Typescript,我正在尝试更新资产文件夹assets/firstboot.json中的一个文件。我正在尝试使用http.patch更新它。但它实际上并没有更新文件。有没有办法用typescript更新磁盘上的JSON文件内容?提前谢谢 我正在尝试的代码是: assets/firstboot.json { "firstbootCompleted": false } 在职: import { Injectable } from '@angular/core'; import { HttpClient }
assets/firstboot.json
中的一个文件。我正在尝试使用http.patch
更新它。但它实际上并没有更新文件。有没有办法用typescript更新磁盘上的JSON文件内容?提前谢谢
我正在尝试的代码是:
assets/firstboot.json
{
"firstbootCompleted": false
}
在职:
import { Injectable } from '@angular/core';
import { HttpClient } from '@angular/common/http';
@Injectable({
providedIn: 'root'
})
export class ConfigService {
constructor(private http: HttpClient) { }
public setConfigStatus(config) {
console.log(config);
return this.http.patch('assets/firstboot.json', config);
}
}
在组件中:
import { Component, OnInit } from '@angular/core';
import { AuthService } from '../auth/auth.service';
import { SettingService } from '../setting/setting.service';
import { Router } from '@angular/router';
import { ConfigService } from './config.service';
@Component({
selector: 'app-config',
templateUrl: './config.component.html',
styleUrls: ['./config.component.css']
})
export class ConfigComponent implements OnInit {
public errorFlag = false;
public alertMessage: string;
public username: string = "admin";
public domain: string = "domain";
public password: string = "password";
public userFormData: any = {"username":this.username+'@'+this.domain,"password":this.password};
constructor(private authService: AuthService, private settingService: SettingService, private configService: ConfigService, private router: Router) { }
ngOnInit() {
this.authService.login({"username":this.username,"authSource":this.domain,"password":this.password}).subscribe();
}
public updatePassword(userFormValue) {
this.settingService.updateLocalUser(userFormValue).subscribe(
res => {
this.setConfigStatus();
this.authService.logout();
this.router.navigateByUrl('login');
}, err => {
this.alertMessage = 'Password was not updated.';
this.errorFlag = true;
} );
}
public setConfigStatus(){
return this.configService.setConfigStatus({"firstbootCompleted": true});
}
}
不如果有任何其他方法代替http.patch,我也可以。在jasmine测试框架中,他们正在磁盘上编写文件。因此,我认为一定有一种方法可以用typescript将文件写入/编辑到磁盘。据我所知,您将无法使用任何东西来修改客户端文件。写入/编辑文件是由服务器端语言完成的,typescript无论如何对此无能为力。资产只是可以与网站文件一起使用的静态文件。要实际更新它们,您必须调用服务器并通过服务器端代码更新文件。其实没什么大不了的,尽管有不同之处,但您提出的http请求是什么样的