[英]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.