代码之家  ›  专栏  ›  技术社区  ›  Stefan

仅将lodash中的“get”函数导入typescript

  •  0
  • Stefan  · 技术社区  · 7 年前

    如何仅导入 get 功能来自 lodash .

    收到

    import { Component, OnInit } from '@angular/core';
    import _ from "lodash";
    
    @Component({
      selector: 'app-lodash-get',
      templateUrl: './lodash-get.component.html',
      styleUrls: ['./lodash-get.component.scss']
    })
    export class LodashGetComponent implements OnInit {
    
      readonly test = {
        level1: {
          level2: {
            level3: 'level3',
            array: [
              {
                data: "test"
              }
            ]
          }
        }
      };
    
      r1 = false;
      r2= false;
      _ : any = null;
    
      constructor() {
        this._ = _; 
       }
    
      ngOnInit() {
        this.r1 = _.get(this.test, 'level1.level2.level3', "No found");
        this.r2 = _.get(this.test, 'level1.level2.foo', "No found");
      }
    }
    
    1 回复  |  直到 7 年前
        1
  •  2
  •   chen ginat    7 年前

    mdn import 说明可以从带括号的模块导入某些函数: import {get} from 'lodash';