javascript - 我在 @Component 中遇到主机问题

标签 javascript css angular visual-studio-code components

如何使我的动画在我的浏览器上运行?特别是在@Component 中使用主机之后

这是我的 menu.component.ts:

@Component({
  selector: 'app-menu',
  templateUrl: './menu.component.html',
  styleUrls: ['./menu.component.scss'],
  host: {
    '[@flyInOut]': 'true',
    'style': 'display: block;'
    },
    animations: [
      flyInOut()
    ]
})

这是我的 app.animation.ts:

export function flyInOut() {
    return trigger('flyInOut', [
        state('*', style({
            opacity: 1,
            transform: 'translateX(0)'
        })),
        transition(':enter',[
            style({transform: 'translateX(-100%)', 
            opacity: 0
        }),
            animate('500ms ease-in')
        ]),
        transition(':leave', [
            animate('500ms ease-out', style({
                transfrom: 'translateX(100%)', 
                opacity: 0
            }))
        ])
    ])
}

我希望动画能在浏览器上运行,但在我使用主机后它却无法运行。

最佳答案

我遇到了类似的问题。我通过将函数直接放在我的组件中而不是使用导出的函数来解决它。

export function slideToLeft () {
  return [
    query(':enter, :leave', [
      style({
        position: 'absolute',
        top: 0,
        left: 0,
        width: '100%'
      })
    ], { optional: true }),
    query(':enter', [
      style({ left: '-100%'})
    ]),
    group([
      query(':leave', [
        animate('600ms ease', style({ left: '100%'}))
      ], { optional: true }),
      query(':enter', [
        animate('600ms ease', style({ left: '0%'}))
      ])
    ]),
  ];
}

@Component({
  selector: 'app-root',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.scss'],
  animations: [ 
    trigger('routeAnimations', [
      transition('FirstPage => SecondPage', slideToRight())
    ])
  ]
})
export class AppComponent implements ...
const routes: Routes = [
    {
        path: 'home/firstPage',
        component: FirstPageComponent,
        canActivate: [AuthGuard],
        data: {
            animation: 'FirstPage'
        },
    },
    {
        path: 'home/secondPage',
        component: SecondPageComponent,
        canActivate: [AuthGuard],
        data: {
            animation: 'SecondPage'
        },
    }
]

@NgModule({
    imports: [RouterModule.forChild(routes)],
    exports: [RouterModule]
})
export class RoutingModule { }

关于javascript - 我在 @Component 中遇到主机问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58167598/

相关文章:

javascript - 为什么 localhost 找不到bundle.js,即使它位于 public 目录中?

javascript - 如何在 JavaScript 下拉列表中添加点击淡入效果?

html - 全 Angular 背景图片限制高度

html - 水平重复 SVG 图案

html - CSS 悬停过渡无法正常工作

javascript - 模块构建失败 : Error: AotPlugin was detected but it was an instance of the wrong class

javascript - 单击时向 Bokeh 图添加一个点

javascript - 如何在angularjs中为负数和正数求和

angular - 使用正确的环境文件将 Angular 应用程序部署到 Heroku

Angular - JWT 刷新 token