简体   繁体   English

Angular-将JSON文件部署到Firebase

[英]Angular - Deploying a JSON file to firebase

Summary 摘要

  • I have an angular project with a JSON file with mock data 我有一个角度项目,其中包含带有模拟数据的JSON文件
  • When I build the project, I stick the JSON file into the 'dist' folder 构建项目时,我将JSON文件粘贴到“ dist”文件夹中
  • I have an angular service which references the JSON file 我有一个引用JSON文件的角度服务
  • I deploy to firebase and receive an error 我部署到Firebase并收到错误
  • Please excuse lack of knowledge here, exceptionally new to this 请原谅这里缺乏知识的人

Questions 问题

  1. Is it possible to use a JSON file whilst working in LocalHost? 在LocalHost中工作时可以使用JSON文件吗?
  2. What have I done wrong with the deployment to firebase? 我将Firebase部署到哪里做错了什么?

Service 服务

 import { IProjects } from './projects.interface'; @Injectable() export class ProjectsService { private _projectURL = '/dist/projects-list.json'; constructor(private _http: Http) { } getProjects(): Observable<IProjects[]> { return this._http.get(this._projectURL) .map((response: Response) => <IProjects[]> response.json()) .do(data => console.log('All: ' + JSON.stringify(data))) .catch(this.handleError); } getProject(id: number): Observable<IProjects> { return this.getProjects() .map((projects: IProjects[]) => projects.find(p => p.projectId === id)); } private handleError(error: Response) { console.error(error); return Observable.throw(error.json().error || 'Server error'); } } 

List Component that displays the list of projects 显示项目列表的列表组件

 export class ProjectsListComponent implements OnInit { pageTitle = "Project List"; errorMessage: string; projects: IProjects[]; constructor(private _projectsService: ProjectsService) { } ngOnInit(): void { this._projectsService.getProjects() .subscribe(projects => this.projects = projects, error => this.errorMessage = <any>error); } onRatingClicked(message: string): void { this.pageTitle = 'Project List: ' + message; } } 

Error Received in the browser 浏览器中收到错误 在此处输入图片说明

Any help would be greatly appreciated :) 任何帮助将不胜感激 :)

you need add your mock json file in the assets folder in your angular project. 您需要将您的模拟json文件添加到您的角度项目中的assets文件夹中。

and you can access like this 你可以这样访问

getProjects(): Observable<IProjects[]> {
        return this._http.get("assets/projects-list.json")
            .map((response: Response) => <IProjects[]> response.json())
            .do(data => console.log('All: ' +  JSON.stringify(data)))
            .catch(this.handleError);
    }

Note:- you need add static file like image or config file in the assets folder. 注意:-您需要在资产文件夹中添加静态文件,例如图像或配置文件。 angular can only access assets folder. 角度只能访问资产文件夹。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM