我正在使用Angular Maps构建地图应用程序,并希望将JSON文件导入为定义位置的标记列表.我希望在app.component.ts中使用这个JSON文件作为marker []数组.而不是在TypeScript文件中定义硬编码的标记数组.
导入此JSON文件以在我的项目中使用的最佳过程是什么?任何方向都非常感谢!
在Angular2中,你可以在那里有一个文件夹/ data /和一个json文件,你可以在localhost:4200/data/something.json访问它.
Angular4中不再可能这样.
任何想法如何让它工作?
如果你想帮我检查一下发生了什么,这里是Github的存储库:https://github.com/sandrocsimas/ngx-material-palette-picker
我正在尝试创建我的第一个角度库.我使用angular cli命令生成代码:
ng new ngx-material-palette-picker-app
ng generate library ngx-material-palette-picker
Run Code Online (Sandbox Code Playgroud)
在NgxMaterialPalettePickerComponent我正在导入json文件的组件中,但构建引发了以下错误:
projects/ngx-material-palette-picker/src/lib/ngx-material-palette-picker.component.ts(2,31): error TS2307: Cannot find module './palettes.json'.
Run Code Online (Sandbox Code Playgroud)
这是我的组成部分:
import {Component, OnInit} from '@angular/core';
import * as palettesJSON from './palettes.json';
@Component({
selector: 'ngx-mpp',
template: `,
styles: []
})
export class NgxMaterialPalettePickerComponent implements OnInit {
constructor() {
}
}
Run Code Online (Sandbox Code Playgroud)
我创建了一个types在根路径中调用的目录,并将以下代码添加到index.d.ts:
declare module '*.json' {
const value: any;
export default value;
}
Run Code Online (Sandbox Code Playgroud)
我还添加了目录types到typeRoots主tsconfig.json.
"typeRoots": [
"node_modules/@types",
"types"
],
Run Code Online (Sandbox Code Playgroud)
该项目在Sublime编辑器中没有显示任何错误,但是当我尝试使用该命令构建项目时,ng build …