Embed json in ng-packagr

柔情痞子 提交于 2021-02-08 15:10:26

问题


In an angular cli project, I managed to embed json like this

  • add to typings.d.ts
declare module "*.json" {
    const value: any;
    export default value;
}
  • import .json
import * as data from '../assets/data.json';

But if I want to compile this into an Angular 5 module with ng-packagr, I get the following error:

Error at .../.ng_pkg_build/my-module/ts/src/app/my-module.module. ts:28:33: Cannot find module '../assets/data.json'.

Did anyone encountered this issue and knows how to solve it?


回答1:


Ng-packagr in version 4.4.0 finally added support for resolveJsonModule. See a thread with this issue.

import { Component, Input } from '@angular/core';
import jsonData from './angular.component.json';

@Component({
  selector: 'ng-component',
  template: '<h1>{{title}}</h1>{{data}}',
  styleUrls: ['./angular.component.scss']
})
export class AngularComponent {
  @Input()
  title = 'Angular';

  data = jsonData;
}


来源:https://stackoverflow.com/questions/48240081/embed-json-in-ng-packagr

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!