要使用Angular 6与Angular Material、sweetalert和webpack,您需要按照以下步骤进行设置:
npm install -g @angular/cli
ng new my-app
cd my-app
ng add @angular/material
npm install sweetalert
app.module.ts
文件中导入所需的模块:import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';
import { BrowserAnimationsModule } from '@angular/platform-browser/animations';
import { MatButtonModule } from '@angular/material/button';
import { MatInputModule } from '@angular/material/input';
import { MatIconModule } from '@angular/material/icon';
import { SweetAlert2Module } from '@sweetalert2/ngx-sweetalert2';
import { AppComponent } from './app.component';
@NgModule({
declarations: [
AppComponent
],
imports: [
BrowserModule,
BrowserAnimationsModule,
MatButtonModule,
MatInputModule,
MatIconModule,
SweetAlert2Module.forRoot()
],
providers: [],
bootstrap: [AppComponent]
})
export class AppModule { }
请注意,您需要在app.module.ts
文件中导入所需的Angular Material和sweetalert模块,并将它们添加到imports
数组中。此外,还需要将SweetAlert2Module添加到imports
数组中。
app.component.ts
文件中导入所需的模块并添加自定义代码示例:import { Component } from '@angular/core';
import { SweetAlertService } from 'sweetalert2';
@Component({
selector: 'app-root',
template: `
`
})
export class AppComponent {
constructor(private sweetAlertService: SweetAlertService) {}
openSweetAlert() {
this.sweetAlertService.fire('Hello SweetAlert!', 'This is a custom alert!', 'success');
}
}
在app.component.ts
文件中,我们导入了SweetAlertService并注入了它。然后,我们在按钮的点击事件处理程序中调用openSweetAlert()
函数来打开一个自定义的SweetAlert。
app.component.scss
文件中添加一些样式:button {
margin-top: 20px;
}
ng build --prod
以上是将Angular 6与Angular Material、sweetalert和webpack集成的一般步骤和示例代码。您可以根据自己的需求进行自定义和扩展。