iis服务器助手广告广告
返回顶部
首页 > 资讯 > 前端开发 > 其他 >Angular学习之聊聊独立组件(Standalone Component)
  • 665
分享到

Angular学习之聊聊独立组件(Standalone Component)

独立组件Angular.js前端 2023-05-14 21:05:47 665人浏览 安东尼
摘要

standalone 是 angular14 推出的新特性。它可以让你的 根模块 AppModule 不至于那么臃肿所有的 component / pipe / directive 都在被使用的时候 在对应的组件引入就好了举个例子 这是之前

standalone 是 angular14 推出的新特性。

它可以让你的 根模块 AppModule 不至于那么臃肿

所有的 component / pipe / directive 都在被使用的时候 在对应的组件引入就好了

举个例子 这是之前的写法 我们声明一个 Footer 组件

然后在使用的 Module 中导入这个组件

import { Component } from '@angular/core';

@Component({
  selector: 'app-footer',
  template: ` <footer class="dark:bg-gray-800 dark:text-gray-50">Footer</footer> `,
})
export class FooterComponent {}
import { NgModule } from '@angular/core';
import { CommonModule } from '@angular/common';
import { FooterComponent } from './footer.component';

@NgModule({
  declarations: [HomeComponent, FooterComponent],
  exports: [],
  imports: [CommonModule],
})
export class AppModuleModule {}

这种写法导致我们始终无法摆脱 NgModule

但其实我们的意图就是在 AppComponent 中使用 FooterComponent

换成 React 中的写法 其实会更便于管理和理解

用上我们的新特性 standalone

Footer 组件就改造成这样

import { Component } from '@angular/core';

@Component({
  selector: 'app-footer',
  // 将该组件声明成独立组件
  standalone: true,
  template: ` <footer class="dark:bg-gray-800 dark:text-gray-50">Footer</footer> `,
})
export class FooterComponent {}

然后比如在 Home 页面 我们就可以这样使用

import { Component } from '@angular/core';

import { FooterComponent } from '@components/footer/footer.component';

@Component({
  selector: 'app-home',
  standalone: true,
  // 声明需要使用的 component / pipe / directive 但是它们也必须都是独立组件
  imports: [FooterComponent],
  template: `<app-footer></app-footer>`,
})
export class WelcomeComponent {}

独立组件可以直接用于懒加载 本来我们必须借助 NgModule 来实现

import { NgModule } from '@angular/core';
import { RouterModule, Routes } from '@angular/router';

import { CustomPreloadingStrategy } from '@views/basic-syntax/router/customPreloadingStrategy';

const routes: Routes = [
  {
    path: 'home',
    // 之前想要实现懒加载 这里必须是一个NgModule 现在使用独立组件也可以 并且更加简洁
    loadComponent: () => import('@views/home/home.component').then((mod) => mod.HomeComponent),
  },
];

@NgModule({
  imports: [RouterModule.forRoot(routes, { preloadingStrategy: CustomPreloadingStrategy })],
  exports: [RouterModule],
})
export class AppRoutingModule {}

以上就是Angular学习之聊聊独立组件(Standalone Component)的详细内容,更多请关注编程网其它相关文章!

--结束END--

本文标题: Angular学习之聊聊独立组件(Standalone Component)

本文链接: https://www.lsjlt.com/news/205961.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
  • Angular学习之聊聊独立组件(Standalone Component)
    standalone 是 Angular14 推出的新特性。它可以让你的 根模块 AppModule 不至于那么臃肿所有的 component / pipe / directive 都在被使用的时候 在对应的组件引入就好了举个例子 这是之前...
    99+
    2023-05-14
    独立组件 Angular.js 前端
  • Angular学习之聊聊路由(Routing)
    1. 摘要简单来说地址栏中,不同的地址(URL)对应不同的页面,这就是路由。同时,点击浏览器的前进和后退按钮,浏览器就会在你的浏览历史中向前或向后导航,这也是基于路由。【相关教程推荐:《angular教程》】在 Angular 里面,Rou...
    99+
    2023-05-14
    Angular Angular.js
  • Angular学习之聊聊Directive指令
    例如 开发中常用的 *ngFor 就是一个指令 用来遍历渲染 DOM 元素可以参考下面的 Link 我为这些指令都编写了用例rick-chou.github.io/angular-tut…这里我主要介绍一下如何自定义一个自己的指令举个例子 ...
    99+
    2022-11-22
    前端 Angular.js
  • Angular学习之聊聊生命周期
    ngAfterContentInit当把外部的内容投影到内部组件,第一次调用 ngDoCheck 之后调用 ngAfterContentInit,而且只调用一次。// demo.component.ts ngAfterContentIni...
    99+
    2023-05-14
    生命周期 javascript Angular Angular.js
  • 聊聊Angular中如何创建简单独立组件并使用
    本篇文章带大家了解一下Angular中的独立组件,介绍一下如何创建简单的独立组件以及如何在 Angular 应用程序中使用它们,希望对大家有所帮助!如果你正在学习 Angular,那么你可能已经听说过独立组件(Component)。顾名思义...
    99+
    2023-05-14
    独立组件 Angular.js
  • Angular学习之聊聊notification(自定义服务)
    比如,我们这篇文章要讲到的 notification 的实现。【相关教程推荐:《angular教程》】效果图如下:UI 这个可以后期调整So,我们一步步来分解。添加服务我们在 app/services 中添加 notification.se...
    99+
    2023-05-14
    前端 JavaScript Angular.js
  • angular学习之深入聊聊状态和动画
    本篇文章带大家深入了解一下angular中的状态和动画,简单介绍一下创建动画的方法,并聊聊关键帧动画、动画回调、可重用动画、交错动画等知识点,希望对大家有所帮助!状态1、什么是状态状态表示的是要进行运动的元素在运动的不同时期所呈现的样式。2...
    99+
    2023-05-14
    状态 动画 Angular
  • Angular学习之聊聊Http ( 错误处理 / 请求拦截 )
    本篇文章带大家继续angular的学习,简单了解一下Angular中的Http处理,介绍一下错误处理和请求拦截,希望对大家有所帮助!基本使用用 Angular 提供的 HttpClient 可以很轻松的实现 API 接口的访问。【相关教程推...
    99+
    2023-05-14
    Angular.js 前端
  • Vue3学习:聊聊组件中怎么使用布尔运算
    (学习视频分享:vuejs入门教程、编程基础视频)同学们大家好,作者又来输出内容了,本文的主要内容是布尔运算。可能不少小伙伴们第一反应是咱们 Javascript 中的 true 和 false,是的没错,它们都是布尔值,但是布尔运算却远不...
    99+
    2023-05-14
    布尔运算 组件 前端 JavaScript Vue.js
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作