您可以使用 vanilla js 在自定义指令中初始化完美滚动条(因为它支持它;-)),如下所示:
import Ps from 'perfect-scrollable';
@Directive({
selector: '[ps]'
})
export class PsDirective {
constructor(private elementRef:ElementRef) {
}
ngAfterViewInit() {
Ps.initialize(this.elementRef.nativeElement);
}
}
您可以像这样使用/应用它:
@Component({
selector: 'app'
template: `
<div ps class="container">
<div class="content"></div>
</div>
`,
styles: [`
.content {
background-image: url('https://noraesae.github.io/perfect-scrollbar/azusa.jpg');
width: 1280px;
height: 720px;
}
.container {
position: relative;
margin: 0px auto;
padding: 0px;
width: 600px;
height: 400px;
}
`],
directives: [ PsDirective ]
})
export class App {
}
在这种方式之前必须已经配置了库(css 和 SystemJS):
<link rel="stylesheet" type="text/css" href="https://cdnjs.cloudflare.com/ajax/libs/jquery.perfect-scrollbar/0.6.11/css/perfect-scrollbar.min.css"/>
<script>
System.config({
transpiler: 'typescript',
typescriptOptions: { emitDecoratorMetadata: true },
map: {
'perfect-scrollable': 'https://cdnjs.cloudflare.com/ajax/libs/jquery.perfect-scrollbar/0.6.11/js/min/perfect-scrollbar.min.js'
},
packages: {
'app': {
defaultExtension: 'ts'
}
}
});
System.import('app/main')
.then(null, console.error.bind(console));
</script>
请参阅此 plunkr:https ://plnkr.co/edit/S8DJpHFVNFioklTl1xg6?p=preview 。