加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

angular2学习使用心得

发布时间:2020-12-17 09:04:46 所属栏目:安全 来源:网络整理
导读:脚手架:https://github.com/AngularClass/angular-starter npm下载插件失败的解决方法 : 1)用yarn安装下载失败的插件,先npm -i yarn安装yarn 2)使用cnpm,参考https://cnpmjs.org/,命令几乎全部和npm一致,只是以cnpm代替了npm 3) yarn config set regis

脚手架:https://github.com/AngularClass/angular-starter

npm下载插件失败的解决方法: 1)用yarn安装下载失败的插件,先npm -i yarn安装yarn 2)使用cnpm,参考https://cnpmjs.org/,命令几乎全部和npm一致,只是以cnpm代替了npm 3)

yarn config set registry https://registry.npm.taobao.org --global
yarn config set disturl https://npm.taobao.org/dist --global

npm config set registry https://registry.npm.taobao.org --global
npm config set disturl https://npm.taobao.org/dist --global

需要掌握的知识点

TypeScript https://www.tslang.cn/docs/home.html

Rxjs http://reactivex.io/rxjs/ 项目中用到的主要是Subject作为observable subscribe unsubscribe,以及combineLatest

Immutable.js

Webpack(前期脚手架框架已经配置好了,不需要学习掌握)

Angular2https://angular.cn/docs/ts/latest/

从Angular1转Angular2要注意的一些点,html的表达式中|| &&等左右一定要留空格

*ngFor="let item of array"

*ngIf

(click)=

(dblclick)= 双击,但是会触发(click)事件,如果要同时click和double click处理不同的事件,建议用click的时间间隔来实现double click

(blur)= (focus)=

[hidden]=

[(ngModel)]= 双向绑定的括号不能少 但也可以用[ngModel] +(ngModelChange)来实现双向绑定

父子控件相互通信, 子控件@Input()注解变量接受父视图的变量,@OutPut() EventEmiiter将值和事件传递给父控件

父组件中:
< child-item [namestr] = “fatherItemName”(childEvent)="fatherFunction(event)" #child>
@ViewChild(ChildComponent) child: ChildComponent; //另外一种父视图访问子控件的方式

child.nativeElement就相当于页面上的元素
子组件
@Input() namestr
@Output() childEvent = new EventEmitter<boolean>();
childEvent.emit(Object)


有很多的Angular2组件 https://github.com/AngularClass/awesome-angular

项目中用到的有:ng2-paginator分页组件


一些代码

在通用服务common-data-service中

public myNoticeSource = new Subject<any>();
public myNoticeSource$ = this.myNoticeSource.asObservable();

public myNotice(data) {
this.myNoticeSource.next(data);
}

在页面中

public mySub: any;
public ngOnInit() {
this.mySub = this.cds.myNoticeSource$.subscribe((data) => {
...
});
}
public ngOnDestroy() {
this.mySub.unsubscribe();
}


一些思路

路径参数变更刷新页面内容

思路一,监听路由数据

this.router.events
.filter(event => event instanceof NavigationEnd)
.map(() => this.route) // 将filter处理后的Observable再次处理
.subscribe((event) => {
this.projectType = this.route.snapshot.params.projectType;
...清空数据
...请求数据1
...请求数据2
});

思路二,用官网的switchMap,按照官网的说法是这种写法有一个好处就是如果参数变更以后上一次的请求如果没有结束会被取消

this.route.params
.switchMap((params: Params) => {
this.projectType = +params['projectType'];
...清空数据

return Observable.combineLatest(
...api1,
...api2
);
}).subscribe((res) => {
if (res[0].meta.code === 200) {
...api1数据处理
} else {
this.cs.responseErrorHandler(res[0],false,this.router);
}
if (res[1].meta.code === 200) {
...api2数据处理
} else {
this.cs.responseErrorHandler(res[1],this.router);
}
});


遇到过的问题

1)hashingTypeError: Data must be a string or a buffer

新写了一个模块,叫做应用服务,我取名ApplicationServiceModule和ApplicationServiceComponent,编译一直报错通过不了,网上有人说在index.ts中使用 export * from 来替代 export {moduleName} from的写法,但是解决不了我的问题,最后重命名为AppServiceModule和AppServiceComponent问题解决了,但是angular组件中也没有看到哪里有ApplicationServiceModule只有一个ApplicationModule,总之感觉怪怪的

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读