Angular项目中核心模块core Module只加载一次的实现
核心模块CoreModule在整个系统中只加载一次,如何实现?
创建core Modele:ng g m core
既然CoreModule是类,就有构造函数,在构造函数中进行依赖注入。
export class CoreModule {
constructor(parent: CoreModule){
if(parent){
throw new Error('模块已经存在,不能重复加载!')
}
}
}
使用SkipSelf注解避免重复注入。去系统的父级找依赖。
使用Optional注解 让SkipSelf作为可选,在第一次注入时候系统中并没有CoreModule时候成功注入。
import { NgModule,SkipSelf,Optional} from '@angular/core';
import { CommonModule } from '@angular/common'; @NgModule({
imports: [
CommonModule
],
declarations: []
})
export class CoreModule {
constructor(@Optional() @SkipSelf() parent: CoreModule){ //加上@SkipSelf()注解
if(parent){
throw new Error ('模块已经存在,不能再次加载');
}
}
}
后续加了模块,后在declartions中声明后需要在exports中导出。
Header,Footer,Sidebar放到核心模块中。
ng g c core/header --spec=false
ng g c core/footer --spec=false
ng g c core/sidebar --spec=false
然后
import { NgModule,SkipSelf,Optional} from '@angular/core';
import { CommonModule } from '@angular/common';
import { HeaderComponent } from './header/header.component';
import { FooterComponent } from './footer/footer.component';
import { SidebarComponent } from './sidebar/sidebar.component'; @NgModule({
imports: [
CommonModule
],
declarations: [HeaderComponent, FooterComponent, SidebarComponent],
exports:[
HeaderComponent, FooterComponent, SidebarComponent
]
})
export class CoreModule {
constructor(@Optional() @SkipSelf() parent: CoreModule){ //加上@SkipSelf()注解
if(parent){
throw new Error ('模块已经存在,不能再次加载');
}
}
}
这样只需要在app.module.ts中imports coreModule就可以了。
本文作者starof,因知识本身在变化,作者也在不断学习成长,文章内容也不定时更新,为避免误导读者,方便追根溯源,请诸位转载注明出处:https://www.cnblogs.com/starof/p/9069181.html 有问题欢迎与我讨论,共同进步。
Angular项目中核心模块core Module只加载一次的实现的更多相关文章
- Angular项目中共享模块的实现
创建share Modele:ng g m share import进来所有需要共享的模块都export出去, 一.共享CommonModule 暂时只有CommonModule,以后会有一些需要共享 ...
- Angular 项目中如何使用 ECharts
在有些使用 ECharts 库的 Angular 项目中,通常除了安装 npm 包之外,还会在 angular.json 中配置 “build.options.scripts”,将 “node_mod ...
- angular项目中各个文件的作用
原文地址 https://www.jianshu.com/p/176ea79a7101 大纲 1.对angular项目中的一些文件的概述 2.对其中一些文件的详细描述 2.1.package.json ...
- angular项目中遇到的问题
一.angular项目中如何实现路由缓存 需要实现的效果,对请求的数据进行缓存,比如进入文章详情页之后点击返回,不会再调用后台数据接口:而是加载缓存中的数据,如何数据变动的情况下,可使用下拉刷新刷新页 ...
- Spring Data JPA系列3:JPA项目中核心场景与进阶用法介绍
大家好,又见面了. 到这里呢,已经是本SpringData JPA系列文档的第三篇了,先来回顾下前面两篇: 在第1篇<Spring Data JPA系列1:JDBC.ORM.JPA.Spring ...
- [翻译 EF Core in Action 1.10] 应该在项目中使用EF Core吗?
Entity Framework Core in Action Entityframework Core in action是 Jon P smith 所著的关于Entityframework Cor ...
- gulp 在 angular 项目中的使用
gulp 在 angular 项目中的使用 keyword:gulp,angularjs,ng,ngAnnotate,jshint,gulpfile 最后附完整简洁的ng项目gulpfile.js 准 ...
- AngularJS中多个ng-app(手动加载模块)
1.当有多个ng-app时:(首先是要加载angularJS) <div ng-app=""> <p>姓名:<input type="tex ...
- Angular.JS + Require.JS + angular-async-loader 来实现异步加载 angular 模块
传统的 angular 应用不支持异步加载模块,必须在 module 启动的时候,所有模块必须预加载进来. 通过使用 angular-async-loader 库,我们可以使用 requirejs 等 ...
随机推荐
- [模板] 笛卡尔树 && RMQ
话说我noip之前为什么要学这种东西... 简介 笛卡尔树(Cartesian Tree) 是一种二叉树, 且同时具有以下两种性质: 父亲节点的值大于/小于子节点的值; 中序遍历的结果为原序列. 笛卡 ...
- jQUERY中的属性获取
jQuery获取Select选择的Text和Value:语法解释:1. $("#select_id").change(function(){//code...}); //为Se ...
- pyspider框架学习
一.crawl()方法学习: 1.url:爬去是的url,可以定义单个,可以定义为url列表. 2.callback:回调函数,指定该url使用哪个方法来解析. 3.age:任务的有效时间. 4.pr ...
- 51Nod--1247 可能的路径(gcd)
根据规则可知 假设 (a,b) 可以到达坐标(aa,bb) 那么 aa=a*x+b*y x y 必定有解 所以 我们只要求两个坐标的gcd看是否相等就好 #include<bits/stdc ...
- java 简单程序
public class a{ public static void main(String[] args) { System.out.println("Hello world") ...
- deepin安装mysql记录
本文转载自http://www.linuxidc.com/Linux/2016-07/133128.htm sudo apt-get install mysql-server apt-get isnt ...
- MS SQL Server 数据库连接字符串详解
MS SQL Server 数据库连接字符串详解 原地址:http://blog.csdn.net/jhhja/article/details/6096565 问题 : 超时时间已到.在从池中获取连接 ...
- 第十四节:Lambda、linq、SQL的相爱相杀(3)
一. SQL 开篇 1. where用法 #region 封装EF调用SQL语句查询 public static List<T> ExecuteQuery<T>(string ...
- n对n
创建一对一的关系:OneToOne("要绑定关系的表名") 创建一对多的关系:ForeignKey("要绑定关系的表名") 创建多对多的关系:ManyToMan ...
- Python DB operation
mysql http://www.cnblogs.com/zhangzhu/archive/2013/07/04/3172486.html 1.连接到本机上的MYSQL.首先打开DOS窗口,然后进入目 ...