1

我使用 angular 2 和 ng2-material 来显示单选和复选框组件,在每个组件中我看到我必须导入所有 ng2-material 内容,最后只使用一个组件

=> 结果,它会为我的应用程序充电并使其变慢。

我想知道我是否可以只导入我需要的组件,尽管在 ng2-material 文档中他们正在导入所有组件???

import {Component} from 'angular2/core';
import {bootstrap} from 'angular2/platform/browser';

import {MATERIAL_DIRECTIVES, MATERIAL_PROVIDERS} from "ng2-material/all"; <<<<<

import {ROUTER_DIRECTIVES} from 'angular2/router';
import { CORE_DIRECTIVES, FORM_DIRECTIVES } from 'angular2/common';

@Component({
    selector: 'radiobox',
    templateUrl: '/radiobox.html',
    styleUrls: ['radiobox.css'],
    directives: [MATERIAL_DIRECTIVES] <<<<<<
4

2 回答 2

1

根据all.ts文件,您可以通过自己的模块导入每个组件。该文件只是简单地export * from创建并创建了一些常量。

以下是该文件的一些内容:

import {MdAnchor, MdButton} from "./components/button/button";
import {MdCheckbox} from "./components/checkbox/checkbox";
import {MdContent} from "./components/content/content";
import {MdDataTable, MdDataTableHeaderSelectableRow, MdDataTableSelectableRow} from './components/data_table/data_table';
import {MdDialog} from "./components/dialog/dialog";
import {MdDivider} from "./components/divider/divider";
import {MdIcon} from "./components/icon/icon";
import {MdInk} from "./components/ink/ink";
import {
  MdPatternValidator,
  MdMaxLengthValidator,
  MdMinValueValidator,
  MdMaxValueValidator,
  MdNumberRequiredValidator,
  INPUT_VALIDATORS
} from "./components/form/validators";
import {MdMessage, MdMessages} from "./components/form/messages";
import {MdInput, MdInputContainer} from "./components/input/input";
import {MdList, MdListItem} from "./components/list/list";
import {MdProgressLinear} from "./components/progress_linear/progress_linear";
import {MdProgressCircular} from "./components/progress_circular/progress_circular";
import {MdPeekaboo} from "./components/peekaboo/peekaboo";
import {MdRadioButton, MdRadioGroup} from "./components/radio/radio_button";
import {MdRadioDispatcher} from "./components/radio/radio_dispatcher";
import {MdSwitch} from "./components/switcher/switch";
import {MdSubheader} from "./components/subheader/subheader";
import {MdSidenav, MdSidenavContainer} from "./components/sidenav/sidenav";
import {SidenavService} from "./components/sidenav/sidenav_service";
import {MdToolbar} from "./components/toolbar/toolbar";
import {MdTabs, MdTab} from "./components/tabs/tabs";
import {Media} from "./core/util/media";
export * from './components/button/button';

例如,如果您只想使用单选按钮:

import {
  MdRadioButton, MdRadioGroup
} from "ng2-material/components/radio/radio_button";

有关常量,请参见同一文件:https ://github.com/justindujardin/ng2-material/blob/master/ng2-material/all.ts#L85 。

于 2016-04-12T12:13:20.297 回答
0

@firasKoubaa 我认为您可以使用类似的东西 import {MdList, MdListItem, MdContent, MdButton} from 'ng2-material/all'; 或类似的东西 import {MdButton} from "/ng2-material/components/button/button";

于 2016-04-12T11:28:25.763 回答