Angular创建独立组件的操作及用法是什么
Admin 2022-08-22 群英技术资讯 853 次浏览
Angular 14一项令人兴奋的特性就是Angular的独立组件终于来了。
在Angular 14中, 开发者可以尝试使用独立组件开发各种组件,但是值得注意的是Angular独立组件的API仍然没有稳定下,将来可能存在一些破坏性更新,所以不推荐在生产环境中使用。
对于已有的组件,我们可以在@Component()
中添加standalone: true的,然后我们可以在没有@NgModule()
的情况下直接使用imports
导入其他模块了。 如果是新建组件,可以使用ng generate component <name> --standalone
的命令,直接创建一个独立组件, 例如:
ng generate component button-list --standalone
@Component({ selector: 'app-button-list', standalone: true, imports: [ CommonModule, ], templateUrl: './button-list.component.html', styleUrls: ['./button-list.component.scss'] }) export class ButtonListComponent implements OnInit
我们可以在imports
中添加已有的模块,以MatButtonModule
为例:
imports: [ CommonModule, MatButtonModule, ],
这样子我们就可以在ButtonListComponent
中使用MatButtonModule
的mat-button
组件了:
<button mat-button>Basic</button> <button mat-button color="primary">Primary</button> <button mat-button color="accent">Accent</button> <button mat-button color="warn">Warn</button> <button mat-button disabled>Disabled</button> <a mat-button href="https://damingerdai.github.io" target="_blank">Link</a>
效果图:
第一步, 将AppComponent
设置为独立组件:
@Component({ selector: 'app-root', templateUrl: './app.component.html', styleUrls: ['./app.component.scss'], standalone: true, }) export class AppComponent {
第二步,将AppModule
的imports中的导入的模块加入到AppComponent
的imports中,但是有两个模块例外: BrowserModule
和BrowserAnimationsModule
。
如果导入的话,可能会导致** BrowserModule
have already been loaded. If you need access to common directives such as NgIf and NgFor, import the CommonModule
instead.**的问题:
第三步,删除app.module.ts
文件
最后一步, 将main.ts
中的:
import { platformBrowserDynamic } from '@angular/platform-browser-dynamic'; platformBrowserDynamic().bootstrapModule(AppModule) .catch(err => console.error(err));
改为:
bootstrapApplication(AppComponent).catch(err => console.error(err));
这样子我们就实现了使用独立组件启动Angular组件了。
我这里分别有三个独立组件: HomeComponent
, ButtonListComponent
和 ChipListComponent
,
然后在main.ts
中创建ROUTES对象
const ROUTES: Route[] = [ { path: '', pathMatch: 'full', redirectTo: 'home' }, { path: 'home', component: HomeComponent }, { path: 'button', loadComponent: () => import('./app/button-list/button-list.component').then( (mod) => mod.ButtonListComponent ), }, { path: 'chip', loadComponent: () => import('./app/chip-list/chip-list.component').then( (mod) => mod.ChipListComponent ), }, ];
其中ButtonListComponent
和ChipListComponent
使用loadComponent
去实现路由懒加载。
然后在bootstrapApplication
的第二个参数中使用providers
注册RouterModule
好了。
bootstrapApplication(AppComponent, { providers: [ importProvidersFrom(RouterModule.forRoot([...ROUTES])), ], }).catch(err => console.error(err));
效果图:
当我们想要启动Angular应用的时候,可能需要注入一些值或者服务。 在bootstrapApplication
, 我们可以通过providers
来注册值或者服务。
比如,我有一个获取图片的url,需要注入到PhotoService
中:
bootstrapApplication(AppComponent, { providers: [ { provide: 'photoUrl', useValue: 'https://picsum.photos', }, {provide: PhotosService, useClass: PhotosService }, importProvidersFrom(RouterModule.forRoot([...ROUTES])), importProvidersFrom(HttpClientModule) ], })
PhotoService
代码如下:
@Injectable()export class PhotosService { constructor( @Inject('photoUrl') private photoUrl: string, private http: HttpClient ) { } public getPhotoUrl(i: number): string { return `${this.photoUrl}/200/300?random=${i}`; } }
本文所使用的源代码:https://github.com/damingerdai/angular-standalone-components-app
线上demo:https://damingerdai.github.io/angular-standalone-components-app/
原文地址:https://juejin.cn/post/7107224235914821662
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:mmqy2019@163.com进行举报,并提供相关证据,查实之后,将立刻删除涉嫌侵权内容。
猜你喜欢
javascript改变图片链接地址的方法:【window.onload = function () {ImageSrc();} function ImageSrc() { var imgs = document.getEleme...】。
方法与对象绑定的,不能单独引用,只能通过对象调用。功能独立,可单独引用。在方法中,this表示该方法所属的对象。在函数中,this代表整体对象。
本篇文章带大家了解一下nodejs中的全局对象,介绍一下nodejs全局对象与浏览器中的全局对象的区别,希望对大家有所帮助!
这篇文章主要为大家详细介绍了Vue.js实现九宫格图片展示模块,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
说明: el-table有一个表格一级数据和二级数据显示的是一样的,像这种就可以用递归实现。数据结构是这样子的:tableData:[{name: Lucy,age: 18,mobile: 11111111111,type: 1,friends:[{name: Lucy-friend1,age: 16,mobile:
成为群英会员,开启智能安全云计算之旅
立即注册Copyright © QY Network Company Ltd. All Rights Reserved. 2003-2020 群英 版权所有
增值电信经营许可证 : B1.B2-20140078 粤ICP备09006778号 域名注册商资质 粤 D3.1-20240008