angular怎么将markdown格式转换成html格式渲染在页面上

最近有个需求 后端返回markdown格式的数据前端要进行渲染
查阅资料后具体操作步骤如下
参考链接https://jfcere.github.io/ngx-markdown/cheat-sheet

  1. 安装 ngx-markdown
// 我这里项目时angular8的 所以也选择相对应的版本
npm i ngx-markdown@8.2.2 --save
  1. 在AppModule中引用
import {
    MarkdownModule } from 'ngx-markdown';

@NgModule({
   
  declarations: [
  ],
  imports: [
    MarkdownModule.forRoot()  // 需要加的
  ],
  providers: [
  ],
  bootstrap: [AppComponent],
})

3.在子module重复上一部操作

4.在html使用

<div class="markdown" markdown ngPreserveWhitespaces>
      # front-end-template

      This project was generated with [Angular CLI](https://github.com/angular/angular-cli) version 8.3.19.

      ## Development server

      Run `ng serve` for a dev server. Navigate to `http://localhost:4200/`. The app will automatically reload if you
      change any of the source files.

      ## Code scaffolding

      Run `ng generate component component-name` to generate a new component. You can also use `ng generate
      directive|pipe|service|class|guard|interface|enum|module`.

      ## Build

      Run `ng build` to build the project. The build artifacts will be stored in the `dist/` directory. Use the `--prod`
      flag for a production build.

      ## Running unit tests

      Run `ng test` to execute the unit tests via [Karma](https://karma-runner.github.io).


 </div>

5.注:这时候样式可能不大对,大家可以网上找样式贴上去,注意必须加ng-deep,否则不会生效

::ng-deep {
   

    h1,
    h2,
    h3,
    h4 {
   
      color: #111111;
      font-weight: 400;
      margin-top: 1em;
    }

    h1,
    h2,
    h3,
    h4,
    h5 {
   
      font-family: Georgia, Palatino, serif;
    }

    h1,
    h2,
    h3,
    h4,
    h5,
    p,
    dl {
   
      margin-bottom: 16px;
      padding: 0;
    }

    h1 {
   
      font-size: 48px;
      line-height: 54px;
    }

    h2 {
   
      font-size: 36px !important;
      line-height: 42px;
    }

    h1,
    h2 {
   
      border-bottom: 1px solid #EFEAEA;
      padding-bottom: 10px;
    }

    h3 {
   
      font-size: 24px;
      line-height: 30px;
    }

    h4 {
   
      font-size: 21px;
      line-height: 26px;
    }

    h5 {
   
      font-size: 18px;
      list-style: 23px;
    }

    a {
   
      color: #0099ff;
      margin: 0;
      padding: 0;
      vertical-align: baseline;
    }

    a:hover {
   
      text-decoration: none;
      color: #ff6600;
    }

    a:visited {
   }

    ul,
    ol {
   
      padding: 0;
      padding-left: 24px;
      margin: 0;
    }

    li {
   
      line-height: 24px;
    }

    p,
    ul,
    ol {
   
      font-size: 16px;
      line-height: 24px;
    }

    ol ol,
    ul ol {
   
      list-style-type: lower-roman;
    }



    code,
    pre {
   
      border-radius: 3px;
      background-color: #f7f7f7;
      color: inherit;
      color: red;
    }

    code {
   
      font-family: Consolas, Monaco, Andale Mono, monospace;
      margin: 0 2px;
    }

    pre {
   
      line-height: 1.7em;
      overflow: auto;
      padding: 6px 10px;
      border-left:
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值