首页 文章

npm模块和angular-cli @ webpack

提问于
浏览
0

应该如何在mpm模块中打包angular2组件,以便可以在使用angular-cli @ webpack编译的应用程序中导入和使用它 .

应该在npm包中发布什么?源(* .ts,* .css,* .html)或webpack编译版本?

看到我的相关问题:angular-cli@webpack : Components imported in npm dependencies don't work

1 回答

  • 0

    首先,使用npm安装Yeoman和generator-angular2-library(假设您已经预安装了node.js) .

    $ npm install -g yo
    $ npm install -g generator-angular2-library
    

    创建一个新目录并进入它:

    $ mkdir angular2-library-name
    $ cd angular2-library-name
    

    并生成您的新库:

    $ yo angular2-library
    

    并为您创建以下文件:

    .
    ├── README.MD
    ├── index.ts
    ├── package.json
    ├── src
    │   ├── sample.component.ts
    │   ├── sample.directive.ts
    │   ├── sample.pipe.ts
    │   └── sample.service.ts
    ├── tsconfig.json
    ├── tslint.json
    └── typings.json
    

    然后,您可以在src /目录中添加或编辑* .ts文件并运行:

    $ npm run tsc
    $ npm run lint
    

    将库发布到NPM注册表后,可以先在任何Angular应用程序中导入它,方法是先使用NPM进行安装:

    $ npm install sample-library # use the name you used to publish to npm
    

    详细描述here .

相关问题