html - 角度 - 仅在页面刷新时触发的路由动画
问题描述
我有一种情况,我为路线设置的动画仅在刷新浏览器页面时触发。当我导航到应用程序中的不同路线时,不会应用动画。
这是我的router-animations.ts
:
import {
trigger,
transition,
style,
animate,
state
} from '@angular/animations';
export function slideLeft() {
return slide();
}
function slide() {
return trigger('slide', [
state('void', style({ position: 'absolute', width: '100%' })),
state('*', style({ position: 'absolute', width: '100%' })),
transition('void => *', [
style({ transform: 'translateX(100%)', opacity: 0 }),
animate(
'1s ease-in-out',
style({ transform: 'translateX(0%)', opacity: 1 })
),
]),
transition('* => void', [
style({ transform: 'translateX(0%)', opacity: 1 }),
animate(
'1s ease-in-out',
style({ transform: 'translateX(-100%)', opacity: 0 })
),
]),
]);
}
我的app.component.html
:
<div [@slide]="prepareRoute(outlet)" >
<router-outlet #outlet="outlet"></router-outlet>
</div>
app.component.ts
:_
import { slideLeft } from './router-animations'
@Component({
selector: 'app-root',
animations: [slideLeft()],
templateUrl: './app.component.html',
styleUrls: ['./app.component.css'],
})
export class AppComponent {
title = 'ShopCompanionApp';
prepareRoute(outlet: RouterOutlet) {
return outlet && outlet.activatedRouteData && outlet.activatedRouteData['animation'];
}
}
最后,这是我的路线:
const appRoutes: Routes = [
{ path: "", component: LoginShopComponent },
{ path: "login/store", component: LoginShopComponent },
{ path: "login/account", component: LoginAccountComponent },
{ path: "mystore/manage", component: SlotsManagementComponent, canActivate: [AuthGuard] },
{ path: "mystore/orders", component: OrdersComponent, canActivate: [AuthGuard] },
{ path: "mystore/orders/:id", component: OrderDetailsComponent, canActivate: [AuthGuard] },
{ path: "**", redirectTo: "mystore/manage", pathMatch: "full" }
]
我在这里想念什么?
提前致谢。
解决方案
我不是 100% 确定规格,但您可以尝试监听路线变化,如果 url 符合您的条件,则应用动画。
我删除了prepareRoute
,并将其替换为shouldSlide
变量。
<div [@slide]="shouldSlide" >
<router-outlet #outlet="outlet"></router-outlet>
</div>
import { Router, NavigationEnd } from '@angular/router';
export class AppComponent {
title = 'ShopCompanionApp';
shouldSlide = false;
constructor(private router: Router) {
router.events.subscribe((evt) => { // will trigger each time there's a route change.
if(evt instanceof NavigationEnd){
this.shouldSlide = evt.url.match('animation');
}
});
}
// TODO unsubscribe when the component is destroyed
}
推荐阅读
- javascript - 如何在Vue JS中的计算属性内设置全局变量
- python - 即使文件可用,文件位置问题
- python - Django - 使 python 对象在会话期间持续存在
- enterprise-architect - 为什么我不能在 Enterprise Architect 中将一个对象(类的实例)连接到另一个对象(类的实例)的特定属性?
- ios - 模拟器上的后台获取工作(我可以看到日志)但不能在真实设备上工作
- javascript - 保险丝盒和打字稿:意外令牌{
- windows - Win 7 中的命令行:由于某些脚本不再工作,我意外更改了什么
- ios - 如何显示或隐藏 AGSArcGISMapImageLayer 子图层
- c# - 加载具有依赖引用的程序集
- react-native - 使用索引将对象添加到redux中的数组