简体   繁体   English

使用一个主要的 class 具有我在组件中需要的所有类。 那是坏方法吗?

[英]Use one main class has all classes that I need in my components. Is that bad way?

I have question about building projects in Angular 7.我对在 Angular 7 中构建项目有疑问。

I have a many of duplicate classes that used in every component.我有许多在每个组件中使用的重复类。 I thought I make one class has all of classes that I need, instead of importing all these classes in every component.我以为我制作了一个 class 具有我需要的所有类,而不是在每个组件中导入所有这些类。

This will make me controlling on the project easily.这将使我轻松控制项目。 But I don't know if this bad way or good way?但我不知道这是坏方法还是好方法? is It consumes a lot memory and resources or no?是否消耗大量 memory 和资源?

EX My main class "AppImports": EX 我的主要 class “AppImports”:

import { Injectable } from '@angular/core';
import { SplashScreen } from '@ionic-native/splash-screen/ngx';
import { StatusBar } from '@ionic-native/status-bar/ngx';
import 'rxjs/add/operator/map';
import {Router ,  ActivatedRoute} from "@angular/router"
import { Platform,MenuController, ModalController , AlertController, ToastController, PopoverController, ActionSheetController ,  NavController } from '@ionic/angular';
import { Keyboard } from '@ionic-native/keyboard/ngx';
import { Language } from './language';
import { Display } from './display';
import { Api } from './api';
import { Buttons } from './buttons';
import { Animations } from './animations';

@Injectable()
export class AppImports {

    constructor(
        public platform: Platform,
        public splashScreen: SplashScreen,
        public statusBar: StatusBar,
        public keyboard: Keyboard,
        public router: Router,
        public activRoute: ActivatedRoute,
        public menu: MenuController,
        public modals: ModalController,
        public alerts: AlertController,
        public toasts:ToastController,
        public pops: PopoverController,
        public sheets: ActionSheetController ,
        public navs: NavController,
        public lang: Language,
        public display: Display,
        public api: Api,
        public button: Buttons,
        public animation: Animations
    ){}




}

In my component I imported only one class "AppImports" that has all of my classes:在我的组件中,我只导入了一个 class “AppImports”,其中包含我的所有类:

import { Component } from '@angular/core';
import { AppImports } from '../imports/app-imports'



@Component({
  selector: 'app-root',
  templateUrl: 'app.component.html',
  styleUrls: ['app.component.scss'],

})
export class AppComponent {

  constructor(
    public app :AppImports,
  ) {


    this.app.menu.open();

  }



}

so, Is it bad way or good way?那么,这是坏方法还是好方法?

In each component just import what you need in this component.在每个组件中,只需在该组件中导入您需要的内容。 You can import AppImports in your component if all classes of it you really need.如果你真的需要它的所有类,你可以在你的组件中导入AppImports

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在各种组件中调用一个组件。 角度4 - Call one component in various components. Angular 4 我使用Redux时所有组件都是类组件,这是正常的吗 - ReactJS Is it normal when I use Redux all of my components are class component 如何为所有组件创建全局预加载器。 角度2 - How to create global preloader to all components. Angular 2 在各种组件中显示一个组件的相同数据。 角度4 - show the same data of one component in various components. Angular 4 如果 = 一个班级,但有两个班级 - If = one class, but has two classes 我想使用rxjs来检测多个组件中的值变化。 但它只显示改变它的组件的变化 - I want to use rxjs subject to detect the value change in multiple components. But it only shows change in the component which changes it 角度2-我有3个标头组件。 如何最好地切换这2个标题? - Angular 2 - I have 3 headers components. Hows best to toggle these 2 headers? 我正在尝试将状态发送给父母组件。 但是不明白 - I'm trying to send the states to the parents components. But do not get it React-如何将所有组件导入一个 - React - How can I import all my components into one 我的onlick有2个功能-但是我需要一个接一个触发吗? - My onlick has 2 functions - but I need one to fire after the other?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM