将json嵌入ng-packagr [英] Embed json in ng-packagr

查看:78
本文介绍了将json嵌入ng-packagr的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在一个有角度的cli项目中,我设法像这样嵌入json

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

  • 添加到typings.d.ts
declare module "*.json" {
    const value: any;
    export default value;
}

  • 导入.json
  • import * as data from '../assets/data.json';
    

    但是,如果我想使用ng-packagr将其编译为Angular 5模块,则会出现以下错误:

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

    .../.ng_pkg_build/my-module/ts/src/app/my-module.module发生错误. ts:28:33:找不到模块'../assets/data.json'.

    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?

    推荐答案

    版本线程.

    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;
    }
    

    这篇关于将json嵌入ng-packagr的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆