angular2开发知识点

news2024/9/21 14:39:30

目录

文章目录

    • 一、API 网关地址 配置
    • 二、服务注册使用
    • 三、模块组件注册使用
    • 四、html中style类动态绑定
      • 1. 单个类的绑定:[class.special]="isSpecial"
      • 2. 多个类的绑定:[ngClass]="{'selected':status === '','saveable': this.canSave,}"
      • 3. 单个内联样式绑定:[style.color]="isSpecial ? 'red': 'green'"
      • 4. 多个内联样式绑定:[ngStyle]="currentStyles"
    • **angular2 第三方插件的使用**
      • 1. 安装插件:
      • 2. 模块中引入prime
      • 3. 在组件中使用插件
    • angular中阻止点击事件冒泡

一、API 网关地址 配置

cloudlink-front-framework/config/webpack.dev.js

# line 13 ~ 19
/**
 * Webpack Constants 
 */
const ENV = process.env.ENV = process.env.NODE_ENV = 'development';
const HOST = process.env.HOST || 'localhost';
const PORT = process.env.PORT || 3000;
const HMR = helpers.hasProcessFlag('hot');

# line 150 ~ 171
devServer: {
            port: METADATA.port,
            host: METADATA.host,
            historyApiFallback: {
                index: '/index.html'
            },
            watchOptions: {
                aggregateTimeout: 300,
                poll: 1000
            },
            proxy: {
                '/cloudlink/v1/**': {
                    target: 'http://192.168.100.90:8050',
                    // target: 'http://192.168.120.110:8050',

                    // target: 'http://192.168.20.221:8901', //赵杨 ip
                    // target: 'http://192.168.100.212:8050',
                    secure: false,
                    pathRewrite: { '^/cloudlink/v1': '' }
                }
            }
        },

二、服务注册使用

外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传

如上图所示,先有一个模型跟服务,需要在enterprise-auth/enterprise-authed-approve里面使用:
模型使用:

# enterprise-admin/enterprise-auth/enterprise-authed-approve/enterprise-authed-approve.component.ts 
# 只需要在这个文件中写如下代码即可:
import {EnterpriseAdminModel} from "../shared/enterprise-admin.model";

服务的使用:

注意: 如果服务里面又引入服务,那么在使用这个服务时,也要导入引入的服务。

# 服务的依赖注入: https://angular.cn/docs/ts/latest/guide/dependency-injection.html
# 方法一: 直接在组件中引入使用
# enterprise-admin/enterprise-auth/enterprise-authed-approve/enterprise-authed-approve.component.ts 
# 在文件中写入如下代码:
import {EnterpriseAdminService} from "../shared/enterprise-admin.service"; # 导入服务文件

@Component({
    selector: "jas-enterprise-authed-approve",
    templateUrl: "./enterprise-authed-approve.component.html",
    styleUrls: ["./enterprise-authed-approve.component.css"],
    providers:[EnterpriseAdminService]                               # 在这里写上服务名字
})
------------------------------------------------------------------------------------------

# 方法二: 在组件的所在的模块中注册服务后,在组件中直接使用
# enterprise-auth/enterprise-auth.module.ts 
# 在文件中写入如下代码:
import { EnterpriseAdminService } from './shared/enterprise-admin.service';
@NgModule({
    imports: [
    ],
    declarations: [
    ],
    providers:[EnterpriseAdminService    ]  # 引入声明
})

# enterprise-auth/enterprise-authed-approve/enterprise-authed-approve.component.ts 
# 在文件中写入如下代码:
import {EnterpriseAdminService} from "../shared/enterprise-admin.service";  # 引入使用

------------------------------------------------------------------------------------------
# 方法三:在组件的所在的模块中为服务申明一个名字,在子模块中直接用这个名字调用
# enterprise-auth/enterprise-auth.module.ts 
# 在文件中写入如下代码:
mport { EnterpriseAdminService } from './shared/enterprise-admin.service';
@NgModule({
    providers:[
       {provide:'view',useClass:EnterpriseAdminService} # 引入声明
    ]  
})

# enterprise-auth/enterprise-authed-approve/enterprise-authed-approve.component.ts 
# 在构造函数中直接引用:
constructor(@Inject('view') private viewService,


三、模块组件注册使用

外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传

如上图所示,模块charts需要在enterprise-admin下注册使用:

# 模块的注册使用 

# src/app/jasframework/enterprise-admin/charts/charts.module.ts 
import {Charts} from './charts.component';
import {ChartsRoutes} from './charts.routing'
import {NgModule}      from '@angular/core';
import {CommonModule} from '@angular/common';
@NgModule({
  imports: [CommonModule, ChartsRoutes],
  declarations: [Charts],
  bootstrap: [Charts]
})
export default class ChartsModule {
}

# src/app/jasframework/enterprise-admin/charts/charts.routing.ts
import {Routes, RouterModule} from '@angular/router';
import {Charts} from './charts.component';
const routes:Routes = [
  {
    path: '',
    component: Charts,
    children: [ ]
  },
];
export const ChartsRoutes = RouterModule.forChild(routes);

# src/app/jasframework/enterprise-admin/charts/charts.component.ts
import {Component, OnInit} from '@angular/core';
@Component({
  selector: 'charts',
  templateUrl: 'charts.component.html',
  providers: [ ]
})
export class Charts implements OnInit {
  constructor() { }
  ngOnInit() { }
} 

# src/app/jasframework/enterprise-admin/charts/charts.component.html
<div>hello charts</div>

# 注册模块使之生效   
# 只需要在enterprise-admin的路由文件中注册这个路径就可以了
# src/app/jasframework/enterprise-admin/enterprise-admin.routing.ts
const routes: Routes = [
    {
        path: '', 
        component: EnterpriseAdminComponent,
        children:[{
              path: 'charts',   # 这里是路径
              loadChildren: ()=>System.import('./charts/charts.module.ts'), # 指导去哪里找这个模块
          }]
    },
];

模块比组件多了xx.module.ts与xx.routing.ts两个文件。如果删除这2个文件,那么就是组件。
组件的加载使用:

# 还是以charts为例,代码在上面,少了xx.module.ts与xx.routing.ts两个文件。

# 注册组件使之生效   
# 需要在enterprise-admin的路由文件中注册这个路径,在模块中也需要声明
# src/app/jasframework/enterprise-admin/enterprise-admin.routing.ts
import {Charts} from './charts/charts.component';  # 引入这个组件
const routes: Routes = [
    {
        path: '', 
        component: EnterpriseAdminComponent,
        children:[{
              path: 'charts',   # 这里是路径
              component: Charts, # 指明组件
          }]
    },
];

# src/app/jasframework/enterprise-admin/enterprise-admin.module.ts
import {Charts} from './charts/charts.component'; # 引入这个组件
@NgModule({
    imports:      [ CommonModule,EnterpriseAdminRoutes ],
    declarations: [ EnterpriseAdminComponent, Charts ],   # 在这里写入Charts,这里是声明
    bootstrap:    [ EnterpriseAdminComponent ]
})

四、html中style类动态绑定

1. 单个类的绑定:[class.special]=“isSpecial”

单个style类绑定介绍:https://angular.cn/guide/template-syntax#css-类绑定
由class前缀,一个点 (.)和 CSS 类的名字组成, 其中后两部分是可选的。形如:[class.class-name]。

// 不使用style类绑定的代码:
<!-- standard class attribute setting  -->
<div class="bad curly special">Bad curly special</div>    

// 当badCurly 有值的时候,会清除所有样式类
<!-- reset/override all class names with a binding  -->
<div class="bad curly special" [class]="badCurly">Bad curly</div>

// 使用style绑定
<!-- toggle the "special" class on/off with a property -->
<div [class.special]="isSpecial">The class binding is special</div>

当模板表达式的求值结果是真值时,Angular 会添加这个类,反之则移除它。

2. 多个类的绑定:[ngClass]=“{‘selected’:status === ‘’,‘saveable’: this.canSave,}”

参考链接:https://angular.cn/guide/template-syntax#ngclass-指令
ngClass绑定到一个key:value 形式的控制对象。这个对象中的每个 key 都是一个 CSS 类名,
如果它的 value 是true,这个类就会被加上,否则就会被移除。

// component.ts
currentClasses: {};
setCurrentClasses() {
  // CSS classes: added/removed per current state of component properties
  this.currentClasses =  {
    'saveable': this.canSave,
    'modified': !this.isUnchanged,
    'special':  this.isSpecial
  };
}

// component.thml
<div [ngClass]="currentClasses">This div is initially saveable, unchanged, and special</div>

3. 单个内联样式绑定:[style.color]=“isSpecial ? ‘red’: ‘green’”

https://angular.cn/guide/template-syntax#样式绑定
单个内联样式绑定由style前缀,一个点 (.)和 CSS 样式的属性名组成。 形如:[style.style-property]。

<button [style.color]="isSpecial ? 'red': 'green'">Red</button>
<button [style.background-color]="canSave ? 'cyan': 'grey'" >Save</button>

有些样式绑定中的样式带有单位。在这里,以根据条件用 “em” 和 “%” 来设置字体大小的单位。

<button [style.font-size.em]="isSpecial ? 3 : 1" >Big</button>
<button [style.font-size.%]="!isSpecial ? 150 : 50" >Small</button>

4. 多个内联样式绑定:[ngStyle]=“currentStyles”

https://angular.cn/guide/template-syntax#ngstyle-指令
NgStyle需要绑定到一个 key:value 控制对象。 对象的每个 key 是样式名,它的 value 是能用于这个样式的任何值。
下面的列子会根据另外三个属性的状态把组件的currentStyles属性设置为一个定义了三个样式的对象:

// src/app/app.component.ts
currentStyles: {};
setCurrentStyles() {
  // CSS styles: set per current state of component properties
  this.currentStyles = {
    'font-style':  this.canSave      ? 'italic' : 'normal',
    'font-weight': !this.isUnchanged ? 'bold'   : 'normal',
    'font-size':   this.isSpecial    ? '24px'   : '12px'
  };
}

// src/app/app.component.html
<div [ngStyle]="currentStyles">
  This div is initially italic, normal weight, and extra large (24px).
</div>

你既可以在初始化时调用setCurrentStyles(),也可以在所依赖的属性变化时调用。


angular2 第三方插件的使用

以 使用primeNG插件为例:https://www.primefaces.org/primeng/#/setup

1. 安装插件:

npm install primeng --save

2. 模块中引入prime

# src/app/advanced-research/advanced-research.module.ts
import { DropdownModule } from 'primeng/primeng';
@NgModule({
  imports: [
    DropdownModule,
  ],
  providers: [],
  declarations: []
})
export default class advancedResearchModule { }

3. 在组件中使用插件

angular中阻止点击事件冒泡

在点击事件中调用下面方法,或者在点击事件的父元素中调用方法

// component.ts 文件中
  // 阻止事件冒泡
  public stopBubble(e) {
    // 如果提供了事件对象,则这是一个非IE浏览器
    if (e && e.stopPropagation) {
      // 因此它支持W3C的stopPropagation()方法
      e.stopPropagation();
    } else {
      // 否则,我们需要使用IE的方式来取消事件冒泡
      window.event.cancelBubble = true;
    }
  }

// component.html文件中
<!--阻止事件冒泡-->
        <ul (click)="commonService.stopBubble($event)">
          <div *ngFor="let subItem of item.child">
            <li nz-menu-item (click)="menuClick(subItem.url)" class="menu-li">
              <i class="anticon anticon-appstore menu-icon"></i> {{subItem.name}}
            </li>
          </div>
        </ul>

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/1808434.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

理解我的积木编程思想

1 学习教程&#xff0c;至少7139手册2 编程实践&#xff0c;遇到实际问题后&#xff0c;在技术资料中查找关键词3 选择适合的条目找到代 码。修正&#xff0c;组合。

C语言详解(文件操作)1

Hi~&#xff01;这里是奋斗的小羊&#xff0c;很荣幸您能阅读我的文章&#xff0c;诚请评论指点&#xff0c;欢迎欢迎 ~~ &#x1f4a5;&#x1f4a5;个人主页&#xff1a;奋斗的小羊 &#x1f4a5;&#x1f4a5;所属专栏&#xff1a;C语言 &#x1f680;本系列文章为个人学习…

手写mybatis-预编译前的sql语句

sql表 mybatis数据库中的gxa_user表 /*Navicat Premium Data TransferSource Server : rootSource Server Type : MySQLSource Server Version : 80028Source Host : localhost:3306Source Schema : mybatisTarget Server Type : MySQLTarget…

论文略读:Onthe Expressivity Role of LayerNorm in Transformers’ Attention

ACL 2023 研究了LayerNorm在 Transformers 中对注意力的作用LayerNorm为Transformer的Attention提供了两个重要的功能&#xff1a; 投影&#xff0c;projection LayerNorm 帮助 Attention 设计一个注意力查询&#xff0c;这样所有的Key都可以平等地访问通过将Key向量投影到同一…

域内攻击 ----> DCSync

其实严格意义上来说DCSync这个技术&#xff0c;并不是一种横向得技术&#xff0c;而是更偏向于权限维持吧&#xff01; 但是其实也是可以用来横向&#xff08;配合NTLM Realy&#xff09;&#xff0c;如果不牵强说得话&#xff01; 那么下面&#xff0c;我们就来看看这个DCSyn…

Vue学习|Vue快速入门、常用指令、生命周期、Ajax、Axios

什么是Vue? Vue 是一套前端框架&#xff0c;免除原生JavaScript中的DOM操作&#xff0c;简化书写 基于MVVM(Model-View-ViewModel)思想&#xff0c;实现数据的双向绑定&#xff0c;将编程的关注点放在数据上。官网:https://v2.cn.vuejs.org/ Vue快速入门 打开页面&#xff0…

rce漏洞试试看 buuctf的pingpingping 试试ctf的rce怎么样

打开靶机开始操作 然后我们先知道一些知识点&#xff1a;下面这些是常用的 |这个管道符也就是上一条的命令的输出结果作为下一条命令的输入&#xff1b;这个是跟sql的堆叠注入是一样的|| || 当前面的执行出错时&#xff08;为假&#xff09;执行后面的 & 将任务置于后台执…

R语言绘图 --- 桑基图(Biorplot 开发日志 --- 5)

「写在前面」 在科研数据分析中我们会重复地绘制一些图形&#xff0c;如果代码管理不当经常就会忘记之前绘图的代码。于是我计划开发一个 R 包&#xff08;Biorplot&#xff09;&#xff0c;用来管理自己 R 语言绘图的代码。本系列文章用于记录 Biorplot 包开发日志。 相关链接…

从Log4j和Fastjson RCE漏洞认识jndi注入

文章目录 前言JNDI注入基础介绍靶场搭建漏洞验证注入工具 log4j RCE漏洞分析漏洞靶场检测工具补丁绕过 Fastjson RCE漏洞分析漏洞靶场检测工具补丁绕过 总结 前言 接着前文的学习《Java反序列化漏洞与URLDNS利用链分析》&#xff0c;想了解为什么 Fastjson 反序列化漏洞的利用…

如何制定工程战略

本文介绍了领导者如何有效制定工程战略&#xff0c;包括理解战略核心、如何收集信息并制定可行的策略&#xff0c;以及如何利用行业最佳实践和技术债务管理来提升团队效能和产品质量。原文: How to Build Engineering Strategy 如果你了解过目标框架&#xff08;如 OKR&#xf…

引人入胜的教育视频

对于一家专注于数字自动化和能源管理的跨国公司&#xff0c;我们制作了引人入胜的教育视频&#xff0c;帮助房主选择适合他们需求的电气产品。我们的团队审查并定稿文本&#xff0c;录制并编辑配音&#xff0c;选择背景音乐&#xff0c;设计图形&#xff0c;并制作了演示如何安…

MPB | 葛体达组-原位酶谱法高分辨率实时检测土壤微界面酶活分布

原位酶谱法高分辨率实时检测土壤微界面酶活分布 High resolution real-time detection of soil enzyme activity distribution by in situ zymography 魏晓梦1, 2、魏亮1, 2、郝存抗1, 2、祝贞科1, 2、吴金水1, 2、葛体达1, 2, * 1中国科学院亚热带农业生态研究所&#xff0c;中…

04-认识微服务-SpringCloud

04-认识微服务-SpringCloud 1.SpringCloud&#xff1a; 1.SpringCloud是目前国内使用最广泛的微服务框架。官网地址&#xff1a;https://spring.io/projects/spring-cloud 2.SpringCloud集成了各种微服务功能组件&#xff0c;并基于SpringBoot实现了这些组件的自动装配&…

Vue3学习记录第三天

Vue3学习记录第三天 背景说明学习记录Vue3中shallowReactive()和shallowRef()Vue3中toRaw()和markRaw()前端...语法Vue3中readonly()和shallowReadonly()函数 背景 之前把Vue2的基础学了, 这个课程的后面有简单介绍Vue3的部分. 学习知识容易忘, 这里仅简答做一个记录. 内容都很…

10-Feign-最佳实践分析

10-Feign-最佳实践分析 1.Feign的最佳实践: 方式一(继承):给消费者的FeignClient和提供者的controller定义统一的父接口作为标准。 ​ 服务紧耦合 ​ 父接口参数列表中的映射不会被继承下来 Spring官方不推荐这种方式: ​ 我们一般不推荐去共享接口在服务端和客户端…

【web性能】什么是图层?图层创建的条件?

CSS图层 浏览器在渲染一个页面时&#xff0c;会将页面分为很多个图层&#xff0c;图层有大有小&#xff0c;每个图层上有一个或多个节点。在渲染DOM的时候&#xff0c;浏览器所做的工作实际上是&#xff1a; 获取DOM后分割为多个图层&#xff1b;对每个图层的节点计算样式结果…

数据结构和算法之数组和链表

一、数组 数组是一种线性数据结构&#xff0c;它是由一组连续的内存单元组成的&#xff0c;用于存储相同类型的数据。在JavaScript中&#xff0c;数组可以包含任意类型的数据&#xff0c;不只限于基本数据类型。 1.存储方式 在内存中&#xff0c;数组的元素是连续存储的&…

Java 期末复习 习题集

&#x1f496; 单选题 &#x1f496; 填空题 &#x1f496; 判断题 &#x1f496; 程序阅读题 1. 读代码写结果 class A {int m 5;void zengA(int x){m m x;}int jianA(int y){return m - y;} }class B extends A {int m 3;int jianA(int z){return super.jianA(z) m;} …

【内存管理】页表映射

页表的一些术语 现在Linux内核中支持四级页表的映射&#xff0c;我们先看下内核中关于页表的一些术语&#xff1a; 全局目录项&#xff0c;PGD&#xff08;Page Global Directory&#xff09; 上级目录项&#xff0c;PUD&#xff08;Page Upper Directory&#xff09; 中间目…

Python openpyxl 库使用详解

大家好&#xff0c;当谈论处理 Excel 文件时&#xff0c;Python 的 openpyxl 库无疑是一个强大而灵活的工具。无论是在数据分析、报告生成还是自动化任务中&#xff0c;openpyxl 都展现出了其独特的价值。本文将详细介绍 openpyxl 库的各种功能和用法&#xff0c;帮助读者掌握如…