项目作者: Hancoson

项目描述 :
:smile:An other zhihuDaily app by angular6, angular-cli, router, and element-angular !:heart::heart::heart:
高级语言: TypeScript
项目地址: git://github.com/Hancoson/ng-zhihuDaily.git
创建时间: 2018-06-08T07:40:43Z
项目社区:https://github.com/Hancoson/ng-zhihuDaily

开源协议:MIT License

下载


angular 实现的 知乎日报 App

A angular zhihuDaily app!

DEMO >>

技术栈

  • angular 6
  • rxjs
  • element-angular

使用

Start

  1. npm i / yarn
  2. npm start

Build

  1. npm run build / npm run prod

添加功能

  1. ng g cl my-new-class #新建 class
  2. ng g c my-new-component #新建组件
  3. ng g d my-new-directive #新建指令
  4. ng g e my-new-enum #新建枚举
  5. ng g m my-new-module #新建模块
  6. ng g p my-new-pipe #新建管道
  7. ng g s my-new-service #新建服务
  8. ng g m route --routing #新建 route

说明:

  • g - generate
  • cl - class
  • c - component
  • d - directive
  • e - enum
  • m - module
  • p - pipe
  • s - service

备注

不生成单元测试文件

  1. --spec=falses

设置默认样式

新建项目:

  1. ng new {project-name} --style=scss

在已有项目中设置:

手动修改angular.json文件,添加如下内容即可:

  1. "schematics": {
  2. "@schematics/angular:component": {
  3. "styleext": "scss"
  4. }
  5. },

自定义管道

切割字符串

  • 新建管道文件

    1. import { Pipe, PipeTransform } from '@angular/core'
    2. @Pipe({
    3. name: 'SliceStr'
    4. })
    5. export class SliceStrPipe implements PipeTransform {
    6. // start和end及extraStr后面都跟随了一个问好,代表这个为可选参数而不是必选的
    7. // 功能就是给出截图的启示,然后是否拼接你自定义的字符串(...)等
    8. transform(value: string, start?: number, end?: number, extraStr?: string): string {
    9. if (value) {
    10. if (typeof (start) === 'number' && typeof (end) === 'number') {
    11. if (value.length > end && extraStr && typeof (extraStr) === 'string') {
    12. return value.slice(start, end) + extraStr.toString();
    13. } else {
    14. return value.slice(start, end);
    15. }
    16. } else {
    17. return value;
    18. }
    19. } else {
    20. return value;
    21. }
    22. }
    23. }
  • 引入

    1. import { SliceStrPipe } from '../pipe/slicestr.pipe'
    2. // ...
    3. @NgModule({
    4. declarations: [
    5. SliceStrPipe
    6. ],
    7. // ...
    8. })
  • 使用

    1. <p class="title">{{item.title|SliceStr: 0:20:'...'}}</p>

innerHTML 设置 CSS 样式

  • 新建

    1. import { Pipe, PipeTransform } from "@angular/core";
    2. import { DomSanitizer } from '@angular/platform-browser';
    3. @Pipe({
    4. name: 'safeHtml'
    5. })
    6. export class SafeHtmlPipe implements PipeTransform {
    7. constructor(private sanitized: DomSanitizer) { }
    8. transform(value) {
    9. console.log(value)
    10. return this.sanitized.bypassSecurityTrustHtml(value);
    11. }
    12. }