前言

之前在使用typescript开发angular模块(发布npm包)一文中基本晓得了怎么发布一个typescript写的npm包。但是离目标还有段距离。

开始开发模块

开发过程不是自己想了那么顺利,但是还是有点可取的地方。

安装依赖项目

"dependencies": {
    "@angular/common": "^5.0.2","@angular/core": "^5.0.2","moment": "^2.22.1","rxjs": "^5.5.2","zone.js": "^0.8.4"
  },"devDependencies": {
    "@types/core-js": "^0.9.35","typescript": "^2.8.1","typings": "^2.1.1"
  }

配置tsconfig.json文件

{
  "compilerOptions": {
    /* Basic Options */
    "typeRoots": [                 // 主要配置了这个
      "node_modules/@types"
    ],"lib": [                // 还有这个
      "es2017","dom"
    ],"target": "es5",/* Specify ECMAScript target version: 'ES3' (default),'ES5','ES2015','ES2016','ES2017','ES2018' or 'ESNEXT'. */
    //"module": "commonjs",/* Specify module code generation: 'none','commonjs','amd','system','umd','es2015',or 'ESNext'. */
    // "lib": [],/* Specify library files to be included in the compilation. */
    // "allowJs": true,/* Allow javascript files to be compiled. */
    // "checkJs": true,/* Report errors in .js files. */
    // "jsx": "preserve",/* Specify JSX code generation: 'preserve','react-native',or 'react'. */
    "declaration": true,/* Generates corresponding '.d.ts' file. */
    // "sourceMap": true,/* Generates corresponding '.map' file. */
    // "outFile": "./",/* Concatenate and emit output to single file. */
    // "outDir": "./",/* Redirect output structure to the directory. */
    // "rootDir": "./",/* Specify the root directory of input files. Use to control the output directory structure with --outDir. */
    // "removeComments": true,/* Do not emit comments to output. */
    // "noEmit": true,/* Do not emit outputs. */
    // "importHelpers": true,/* Import emit helpers from 'tslib'. */
    // "downlevelIteration": true,/* Provide full support for iterables in 'for-of',spread,and destructuring when targeting 'ES5' or 'ES3'. */
    // "isolatedModules": true,/* Transpile each file as a separate module (similar to 'ts.transpileModule'). */
    /* Strict Type-Checking Options */
    "strict": true,/* Enable all strict type-checking options. */
    // "noImplicitAny": true,/* Raise error on expressions and declarations with an implied 'any' type. */
    // "strictnullchecks": true,/* Enable strict null checks. */
    // "strictFunctionTypes": true,/* Enable strict checking of function types. */
    // "strictPropertyInitialization": true,/* Enable strict checking of property initialization in classes. */
    // "noImplicitThis": true,/* Raise error on 'this' expressions with an implied 'any' type. */
    // "alwaysstrict": true,/* Parse in strict mode and emit "use strict" for each source file. */

    /* Additional Checks */
    // "noUnusedLocals": true,/* Report errors on unused locals. */
    // "noUnusedParameters": true,/* Report errors on unused parameters. */
    // "noImplicitReturns": true,/* Report error when not all code paths in function return a value. */
    // "noFallthroughCasesInSwitch": true,/* Report errors for fallthrough cases in switch statement. */

    /* Module Resolution Options */
    // "moduleResolution": "node",/* Specify module resolution strategy: 'node' (Node.js) or 'classic' (TypeScript pre-1.6). */
    // "baseUrl": "./",/* Base directory to resolve non-absolute module names. */
    // "paths": {},/* A series of entries which re-map imports to lookup locations relative to the 'baseUrl'. */
    // "rootDirs": [],/* List of root folders whose combined content represents the structure of the project at runtime. */
    // "typeRoots": [],/* List of folders to include type deFinitions from. */
    // "types": [],/* Type declaration files to be included in compilation. */
    // "allowSyntheticDefaultImports": true,/* Allow default imports from modules with no default export. This does not affect code emit,just typechecking. */
    "esModuleInterop": true,/* Enables emit interoperability between Commonjs and ES Modules via creation of namespace objects for all imports. Implies 'allowSyntheticDefaultImports'. */
    // "preserveSymlinks": true,/* Do not resolve the real path of symlinks. */

    /* Source Map Options */
    // "sourceRoot": "./",/* Specify the location where debugger should locate TypeScript files instead of source locations. */
    // "mapRoot": "./",/* Specify the location where debugger should locate map files instead of generated locations. */
    // "inlinesourceMap": true,/* Emit a single file with source maps instead of having a separate file. */
    // "inlinesources": true,/* Emit the source alongside the sourcemaps within a single file; requires '--inlinesourceMap' or '--sourceMap' to be set. */

    /* Experimental Options */
    "experimentalDecorators": true,/* Enables experimental support for ES7 decorators. */
    "emitDecoratorMetadata": true         /* Enables experimental support for emitting type Metadata for decorators. */
  }
}

编写代码

像写普通的angular模块一样

index.ts

import { NgModule } from '@angular/core';
import {CommonModule} from "@angular/common";
import {BlogApiService} from "./provider";

@NgModule({
    imports: [
        CommonModule
    ],providers:    [ BlogApiService ],})
export class MzcNgApiModule { }

发布使用

似乎哪里没有配置正确,引入MzcNgApiModule 来使用时编译要报错。但是引入BlogApiService使用却很正常

在我们的angular项目中安装
npm i mzc-ng-api
能正常使用的情况如下
import { Injectable } from '@angular/core';
import {HttpClient} from '@angular/common/http';
import {BlogApiService} from  "mzc-ng-api/src/provider"
export * from "mzc-ng-api/src/provider"

@Injectable()
export class BlogService extends BlogApiService{
  constructor(protected http: HttpClient) {
    super(http)
  }
}

编译通过,在下面的地方能正确调用BlogApiService里的方法

import {BlogService,PreNoteDto,GetNoteDto} from "../blog.service";

export class NoteListComponent implements OnInit {
  preNoteList:PreNoteDto[]=[];
  loadMore = false;
  loading =false;
  key="";

  constructor(private router: Router,private blogService :BlogService
  ) { }

  ngOnInit() {
    this.getNoteList(true);
  }
  getNoteList(f=false){
    this.loading= true;
    if(f)this.preNoteList =[];
    const param = new GetNoteDto();
    param.key = this.key;
    param.SkipCount = this.preNoteList.length;
    this.blogService.GetNoteList(param).do(()=>{
      this.loading = false;
    }).subscribe(m=> {
      m.items.forEach((v,i)=>{
        v.content = marked(v.content);
        this.preNoteList.push(v);
      });
      this.loadMore = m.totalCount>this.preNoteList.length;
    });
  }
}
不能正常使用的方法如下

在app.module.ts中

import {MzcNgApiModule} from 'mzc-ng-api'

  imports: [
    RouterModule,MzcNgApiModule,browserModule,NgZorroAntdModule.forRoot(),RoutesModule,browserAnimationsModule
  ],

这样引入进来,还没有使用编译都会报错。错误内容几乎也时看不明白,先记录下来,日后慢慢解决。

未完待续

源码地址

https://github.com/yiershan/MZC-Ng-Api

使用typescript开发angular模块(编写模块)的更多相关文章

  1. Android Studio是否支持用于Android UI设计的AngularJS?

    我对AndroidStudio有疑问:AS在设计XML文件时是否支持AngularJS代码,例如:对于小动画或效果?

  2. 使用Android中的Google Maps API从用户位置获取附近地点的结果

    location=-33.8670,151.1957&radius=500&types=food&name=cruise&key=API_KEY和响应JSON像这样:以下示例返回英国伦敦附近的医院列表.有关详细信息,请参阅here.

  3. android – 如何使用ClientID和ClientSecret在Phonegap中使用Angularjs登录Google OAuth2

    我正尝试使用Angularjs(使用IonicFramework)通过GoogleOAuth2从我的Phonegap应用程序登录.目前我正在使用http://phonegap-tips.com/articles/google-api-oauth-with-phonegaps-inappbrowser.html进行登录.但是当我使用Angular-UI-RouterforIonic时,它正在创建非常

  4. Android:SQLite一对多设计

    任何人都有很好的建议,如何使用ContentProvider实现sqlite的一对多映射?如果你看UriContentProvider#insert,你可以看到它有ContentValues参数包含要插入的数据.问题是在当前的实现中,ContentValues不支持put方法,而class是final,所以我无法扩展它.为什么是一个问题?

  5. 利用require.js与angular搭建spa应用的方法实例

    这篇文章主要给大家介绍了关于利用require.js与angular搭建spa应用的方法实例,文中通过示例代码给大家介绍的非常详细,对大家的理解和学习具有一定的参考学习价值,需要的朋友们下面跟着小编来一起看看吧。

  6. 详解Angular动态组件

    本文主要介绍了Angular动态组件,对此感兴趣的同学,可以亲自实验一下。

  7. 详解如何使用webpack+es6开发angular1.x

    本篇文章主要介绍了详解如何使用webpack+es6开发angular1.x,具有一定的参考价值,有兴趣的可以了解一下

  8. angular2系列之路由转场动画的示例代码

    本篇文章主要介绍了angular2系列之路由转场动画的示例代码,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧

  9. 一种angular的方法级的缓存注解(装饰器)

    本篇文章主要介绍了一种angular的方法级的缓存注解(装饰器),小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧

  10. 动手写一个angular版本的Message组件的方法

    本篇文章主要介绍了动手写一个angular版本的Message组件的方法,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧

随机推荐

  1. Angular2 innerHtml删除样式

    我正在使用innerHtml并在我的cms中设置html,响应似乎没问题,如果我这样打印:{{poi.content}}它给了我正确的内容:``但是当我使用[innerHtml]=“poi.content”时,它会给我这个html:当我使用[innerHtml]时,有谁知道为什么它会剥离我的样式Angular2清理动态添加的HTML,样式,……

  2. 为Angular根组件/模块指定@Input()参数

    我有3个根组件,由根AppModule引导.你如何为其中一个组件指定@input()参数?也不由AppModalComponent获取:它是未定义的.据我所知,你不能将@input()传递给bootstraped组件.但您可以使用其他方法来做到这一点–将值作为属性传递.index.html:app.component.ts:

  3. angular-ui-bootstrap – 如何为angular ui-bootstrap tabs指令指定href参数

    我正在使用角度ui-bootstrap库,但我不知道如何为每个选项卡指定自定义href.在角度ui-bootstrap文档中,指定了一个可选参数select(),但我不知道如何使用它来自定义每个选项卡的链接另一种重新定义问题的方法是如何使用带有角度ui-bootstrap选项卡的路由我希望现在还不算太晚,但我今天遇到了同样的问题.你可以通过以下方式实现:1)在控制器中定义选项卡href:2)声明一个函数来改变控制器中的散列:3)使用以下标记:我不确定这是否是最好的方法,我很乐意听取别人的意见.

  4. 离子框架 – 标签内部的ng-click不起作用

    >为什么标签标签内的按钮不起作用?>但是标签外的按钮(登陆)工作正常,为什么?>请帮我解决这个问题.我需要在点击时做出回复按钮workingdemo解决方案就是不要为物品使用标签.而只是使用divHTML

  5. Angular 2:将值传递给路由数据解析

    我正在尝试编写一个DataResolver服务,允许Angular2路由器在初始化组件之前预加载数据.解析器需要调用不同的API端点来获取适合于正在加载的路由的数据.我正在构建一个通用解析器,而不是为我的许多组件中的每个组件设置一个解析器.因此,我想在路由定义中传递指向正确端点的自定义输入.例如,考虑以下路线:app.routes.ts在第一个实例中,解析器需要调用/path/to/resourc

  6. angularjs – 解释ngModel管道,解析器,格式化程序,viewChangeListeners和$watchers的顺序

    换句话说:如果在模型更新之前触发了“ng-change”,我可以理解,但是我很难理解在更新模型之后以及在完成填充更改之前触发函数绑定属性.如果您读到这里:祝贺并感谢您的耐心等待!

  7. 角度5模板形式检测形式有效性状态的变化

    为了拥有一个可以监听其包含的表单的有效性状态的变化的组件并执行某些组件的方法,是reactiveforms的方法吗?

  8. Angular 2 CSV文件下载

    我在springboot应用程序中有我的后端,从那里我返回一个.csv文件WheniamhittingtheURLinbrowsercsvfileisgettingdownloaded.现在我试图从我的角度2应用程序中点击此URL,代码是这样的:零件:服务:我正在下载文件,但它像ActuallyitshouldbeBook.csv请指导我缺少的东西.有一种解决方法,但您需要创建一个页面上的元

  9. angularjs – Angular UI-Grid:过滤后如何获取总项数

    提前致谢:)你应该避免使用jQuery并与API进行交互.首先需要在网格创建事件中保存对API的引用.您应该已经知道总行数.您可以使用以下命令获取可见/已过滤行数:要么您可以使用以下命令获取所选行的数量:

  10. angularjs – 迁移gulp进程以包含typescript

    或者我应该使用tsc作为我的主要构建工具,让它解决依赖关系,创建映射文件并制作捆绑包?

返回
顶部