美文网首页
Angular 2自定义弹出组件toast(使用路由)

Angular 2自定义弹出组件toast(使用路由)

作者: 高阳刘 | 来源:发表于2021-06-08 10:22 被阅读0次

    原理:
    使用Angular2的命名路由插座,一个用来显示app正常的使用,一个用来显示弹出框

    // app.component.html
    <router-outlet></router-outlet>
    <router-outlet name='popup'></router-outlet>
    

    浏览器的导航栏中则这样显示

    http://127.0.0.1:4200/(popup:toast//apps:login)

    路由配置

    import { ToastComponent } from './components/toast/toast.component';
    const routes: Routes = [
      {
        path: 'toast',
        component: ToastComponent,
        outlet: 'popup',
      },
      ...
    ];
    

    toast内容

    // toast.component.html
    <div class="wrap">
        <div class="wrap-box">
            <div class="wrap-box-icon">
                <div *ngIf="toastParams.img">
                    <img src="{{toastParams.img}}" class="wrap-box-icon-img">
                    <div> {{toastParams.title}} </div>
                </div>
                <div *ngIf="!toastParams.img"> {{toastParams.title}} </div>
            </div>
        </div>
    </div>
    
    // toast.component.less
    .wrap{
        position: fixed;
        top:0;
        left: 0;
        z-index: 3;
        width: 100vw;
        height: 100vh;
        color:#fff;
        display: flex;
        align-items: center;
        justify-content: center;
        text-align: center;
    
        &-box{
            background: rgba(0,0,0,.65);
            padding: 10px;
            border-radius: 16px;
            max-width: 8rem;
    
            &-icon{
                max-width: 8.055rem;
                box-sizing: border-box;
                padding: 0 0.444rem;
    
                &-img{
                    height: 0.944rem;
                    width: 0.944rem;
                    display: block;
                    margin: 0 auto 0.333rem;
                }
            }
        }
      }
    
    import { Component, OnInit } from '@angular/core';
    import { PopupService } from '../../services/popup.service';
    
    @Component({
      selector: 'app-toast',
      templateUrl: './toast.component.html',
      styleUrls: ['./toast.component.less']
    })
    export class ToastComponent implements OnInit {
      public toastParams:any;
    
      constructor(
        private popupSVC: PopupService,
      ) { }
    
      ngOnInit(): void {
        this.toastParams = this.popupSVC.getToastParams();
      }
    }
    

    创建用来跳转至popup路由的服务,例如popup.service

    // popup.service.ts
    import { Injectable } from '@angular/core';
    import { Router } from '@angular/router';
    
    @Injectable({
      providedIn: 'root'
    })
    export class PopupService {
      private toastParams:any;
      private loadingParams:any;
    
      constructor(
        private router: Router
      ) { }
    
      toast(_params:any) {
        this.toastParams = _params;
        let _duration;
        if (_params.duration) {
          _duration = _params.duration;
        } else {
          _duration = 500;
        }
        const _outlets = { 'popup': 'toast' };
        this.router.navigate([{ outlets: _outlets }],{ skipLocationChange: true });
        setTimeout(() => {
          const _outlets2 = { 'popup': null };
          this.router.navigate([{ outlets: _outlets2 }],{ skipLocationChange: true });
        }, _duration);
      }
      getToastParams() {
        return this.toastParams;
      }
    }
    

    使用:
    一、在app.module.ts中将服务导进来,注册

    import { ToastComponent } from './components/toast/toast.component';
    import { PopupService } from './services/popup.service';
    @NgModule({
      declarations: [
        ToastComponent,
        ...
      ],
      imports: [
        ...
      ],
      providers: [
        PopupService,
        ...
      ],
      bootstrap: [AppComponent]
    })
    

    二、在使用的test.ts中导入

    import { PopupService } from '../../../services/popup.service';
    constructor(
      private popupSVC: PopupService,
    ) {
    }
    ngOnInit(): void {
      // 调用
      this.popupSVC.toast({
        img: false, // icon 地址或者false
        title: 'toast文案',
        duration: 2000, //toast多久后消失,默认为500ms
      });
    }
    

    原文链接:https://blog.csdn.net/zhy13087344578/article/details/80930564

    相关文章

      网友评论

          本文标题:Angular 2自定义弹出组件toast(使用路由)

          本文链接:https://www.haomeiwen.com/subject/bmmosltx.html