1、为什么有的人路由转动效离场动效不生效?

自己研究发现是加动效的位置放错了  如下:

<---! animate-state.component.html -->
<div style="background-color: antiquewhite;width: 100vw" [@trigger] id="f">
style="background-color: antiquewhite;width: 100%;height: 100px;
</div>
//有的人可能会在组件内容的最顶层元素 加一个动效。以为这样能达到路由转场的效果,其实不然,因为组件本身才是这个组件的根元素。当通过路由转场时,
//离场的根元素瞬间就隐藏了,以有其组件里面的内容不管加什么动效也无用,有动效,元素隐藏了也看不到啊!
// 如上面的的html 模块名为:selector: 'app-animate-state',那么<app-animate-state></app-animate-state>是根元素,动画应该加在这样面才会有转场效果,而不是上面的组件内容最顶层元素

2、解决放法

  因为路由你是没法写代表一个路由内容<app-animate-state></app-animate-state>这个标签元素的。

那么怎么做了?如下

用 @HostBinding()绑定根元素,加上动效。

/路由切换动效如下

//animate-state.component.html
<div style="background-color: antiquewhite;width: 100vw">
style="background-color: antiquewhite;width: 100%;height: 100px;
</div> //animate-state.component.ts import {Component, HostBinding, OnInit} from '@angular/core';
import {trigger2} from "../animation/trigger"; @Component({
selector: 'app-animate-state',
templateUrl: './animate-state.component.html',
styleUrls: ['./animate-state.component.css'],
animations: [trigger2]
})
export class AnimateStateComponent implements OnInit { constructor() { }
@HostBinding('@trigger2') trigger2 = "";
ngOnInit() {
} } //animate-trigger.component.html
<div style="background-color: red;width: 100vw">
[@heroState] = 'name'
</div> //animate-trigger.component.ts import {Component, HostBinding, OnInit} from '@angular/core';
import {trigger1, trigger2} from "../animation/trigger"; @Component({
selector: 'app-animate-trigger',
templateUrl: './animate-trigger.component.html',
styleUrls: ['./animate-trigger.component.css'],
animations: [trigger1, trigger2]
})
export class AnimateTriggerComponent implements OnInit {
constructor() { }
@HostBinding('@trigger2') trigger2 = "";
ngOnInit() {
} }
//app.module.ts
const appRoutes: Routes = [
{ path: 'trigger', component: AnimateTriggerComponent },
{ path: 'state', component: AnimateStateComponent },
{ path: '',
redirectTo: '/trigger',
pathMatch: 'full'
},
{ path: '**', component: AnimateTriggerComponent }
];
//app.component.html <a routerLink="/trigger" routerLinkActive="active">trigger</a>
<a routerLink="/state" routerLinkActive="active">state</a> <div id="app">
<router-outlet></router-outlet>
</div> //trigger.ts 动效 export const trigger2 = trigger('trigger2', [
transition('* => void', [style({opacity: ,position: 'absolute'}),animate(,style({opacity: }))]),
transition('void => *', [style({opacity: ,position: 'absolute'}),animate(,style({opacity: }))])
]);

3、可是这样做的话,每个路由都要这样加,会显的很繁琐,在重复,有办法一次性全加上吗? 当然是有  如下: 用到动效里的query特性函数和路由中的NavigationEnd属性 代码如下

//animate-state.component.html
<div style="background-color: antiquewhite;width: 100vw">
style="background-color: antiquewhite;width: 100%;height: 100px;
</div> //animate-state.component.ts import {Component, HostBinding, OnInit} from '@angular/core'; @Component({
selector: 'app-animate-state',
templateUrl: './animate-state.component.html',
styleUrls: ['./animate-state.component.css'],
})
export class AnimateStateComponent implements OnInit { constructor() { }
ngOnInit() {
} } //animate-trigger.component.html
<div style="background-color: red;width: 100vw">
[@heroState] = 'name'
</div> //animate-trigger.component.ts import {Component, HostBinding, OnInit} from '@angular/core'; @Component({
selector: 'app-animate-trigger',
templateUrl: './animate-trigger.component.html',
styleUrls: ['./animate-trigger.component.css'],
})
export class AnimateTriggerComponent implements OnInit {
constructor() { }
ngOnInit() {
} }
//trigger.ts
import {
trigger,
state,
style,
animate,
transition, query, group
} from '@angular/animations';
import {AnimationEntryMetadata} from "@angular/core";
 export const routeAnimation1: AnimationEntryMetadata = trigger('routeAnimation1', [ transition('* => *', group([ query(':leave', animate('.5s', 
style({opacity: ,position: 'absolute'})), { optional: true }), query(':enter', [style({opacity: ,position: 'absolute'}),
animate('.5s', style({ opacity: }))],{ optional: true }) ]) ) ]); //app.module.ts
const appRoutes: Routes = [ { path: 'trigger', component: AnimateTriggerComponent },
{ path: 'state', component: AnimateStateComponent }, { path: '', redirectTo: '/trigger', pathMatch: 'full' } ]; //app.component.html <a routerLink="/trigger" routerLinkActive="active">trigger</a>
<a routerLink="/state" routerLinkActive="active">state</a>
<div id="app" [@routeAnimation1]="routerStateCode">
  <router-outlet>
  </router-outlet>
</div> //app.component.ts
import { Component } from '@angular/core'; import {NavigationEnd, Router} from "@angular/router";
import { routeAnimation1} from "./animation/trigger";
@Component({ selector: 'app-root', templateUrl:
'./app.component.html', styleUrls: ['./app.component.css'],
animations: [routeAnimation1] })
export class AppComponent {
routerState:boolean = true;
routerStateCode:string = 'active';
constructor(private router:Router)
{ this.router.events.subscribe(event => {
if (event instanceof NavigationEnd) { // 每次路由跳转改变状态
console.log(NavigationEnd,event) this.routerState = !this.routerState;
this.routerStateCode = this.routerState ? 'active' : 'inactive'; } }); } }

有问题,欢迎留言。。。

angular2中的路由转场动效的更多相关文章

  1. Vue-admin工作整理(七):路由的切换动效

    思路就是通过 transition-group 这个组件来对多组件进行控制,效果是通过样式来写,transition-group要有指定的key值,样式中通过name来命名类的名字 <templ ...

  2. angular2-4 之动效-animation

    提示:   angular2 时animation代码在核心模块里面(@angular/core里面);到了angular4.0时animation从核心模块中提取出来作为一个单独的模块, 这样可以在 ...

  3. 动效解析工厂:Mask 动画

    转载自:http://www.cocoachina.com/ios/20160214/15250.html 前言:很多动效都是多种动画的组合,有时候你可能只是需要其中某个动画,但面对庞杂的代码库或是教 ...

  4. 百度MUX:APP动效之美需内外兼修

    移动互联网时代已经到来.APP已如天空的繁星.数也数不清.随着手机硬件的不断升级,实现炫酷且流畅的动效不再是遥远的梦想.假设你是APP达人,喜欢试用各种APP,你肯定会发现越来越多的APP開始动效化. ...

  5. iOS 停止不必要的UI动效设计

    http://www.cocoachina.com/design/20151124/14400.html 前言:这篇短文将会探讨UI设计中动画的过度使用,并将其与早期的视觉设计进行对比,提出一些对于有 ...

  6. 前端必须收藏的CSS3动效库!!!

    现在的网站和App的设计中越来越重视用户体验,而优秀的动效则能使你的应用更具交互性,从而吸引更多用户的使用. 如果你对CSS3中定义动效还不熟练,或希望采用更加简单直接的方式在你的应用中引入动效的话, ...

  7. 【总结】前端必须收藏的CSS3动效库!!!

    现在的网站和App的设计中越来越重视用户体验,而优秀的动效则能使你的应用更具交互性,从而吸引更多用户的使用. 如果你对CSS3中定义动效还不熟练,或希望采用更加简单直接的方式在你的应用中引入动效的话, ...

  8. 巧用 -webkit-box-reflect 倒影实现各类动效

    在很久之前的一篇文章,有讲到 -webkit-box-reflect 这个属性 -- 从倒影说起,谈谈 CSS 继承 inherit -webkit-box-reflect 是一个非常有意思的属性,它 ...

  9. Google I/O 官方应用中的动效设计

    版权声明:本文为博主原创文章,未经博主同意不得转载. https://blog.csdn.net/jILRvRTrc/article/details/82881743 作者:Nick Butcher, ...

随机推荐

  1. 005-maven坐标和依赖

    1.何为Maven坐标 groupId.artifactId.version.packaging.classifier 中央仓库:http://repol.maven.org/maven22.坐标详解 ...

  2. 17.出现fatal signal(SIGSEGV),code 1,fault addr 0x0 in tid 29931的问题

    原因是: 在onCreate方法里面调一些东西的时候,有时候系统相关的东西还没有初始化完,你的代码就已经执行了,会出现一些问题  解决方法: 增加延时1s /一.第一步初始化,此处用的是demo的ap ...

  3. Spark机器学习系列之13: 支持向量机SVM

    Spark 优缺点分析 以下翻译自Scikit. The advantages of support vector machines are: (1)Effective in high dimensi ...

  4. 长网址 短网址(http://www.zhihu.com/question/19852154?rf=21975802)

    短网址(Short URL),顾名思义就是在形式上比较短的网址.通常用的是asp或者php转向,在Web 2.0的今天,不得不说,这是一个潮流.目前已经有许多类似服务,借助短网址您可以用简短的网址替代 ...

  5. C# 使用 SqlBulkCopy 类批量复制数据到数据库

    最近公司需要优化导入的问题,由于之前使用的方式是生成 Insert 语句插入数据库,数据量小的时候还行,但是随着发展数据量渐渐大了,之前的方法性能就跟不上了,于是发现了 SqlBulkCopy 这个类 ...

  6. JavaWeb 如何在web.xml中配置多个servlet

    15:34:42 <servlet> <description></description> <display-name>ListMusicServle ...

  7. Please check registry access list (whitelist/blacklist)

    https://blog.csdn.net/sprita1/article/details/51735566

  8. 【Linux学习】3.Linux常见配置文件

    一./etc 配置文件/etc/passwd 用户数据库,其中的域给出了用户名.真实姓名.家目录.加密口令和用户的其他信息 /etc/group 类似/etc/passwd ,但说明的不是用户而是组. ...

  9. ABP官方文档翻译 1.4 启动配置

    启动配置 配置ABP 替换内置服务 配置模块 创建模块配置 ABP提供了基础设施和模型在启动的时候对它及模块进行配置. 配置ABP 在模块的PreInitialize事件中配置ABP.示例配置如下: ...

  10. WeX5基础

    最近在研究微信app开发,使用的是WeX5,在这里把一些基础知识点记录下来,忘记了可以翻阅查看. 一:开发后端服务 1.建立数据源:窗口--首选项--studio配置--数据源--增加--数据源类型选 ...