简体   繁体   中英

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.

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.

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?

EX My main 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:

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM