在Svelte/Sapper中,脚本标记的作用域是如何确定的?

在Svelte/Sapper中,脚本标记的作用域是如何确定的?,svelte,Svelte,Sapper中一个页面的script标记中导入库()的实例,该实例在导航到其他页面时保持存在 我尝试在ondestroy中销毁它,但无法引用引用实例的变量(在oncreate中创建) 如何将脚本标记的范围限定到各个页面,或者在离开页面时销毁实例?您有两种方法可以访问在onstroy中创建的onstroy中的内容。第一个是将它们添加为组件的属性: 从“ScrollMagic”导入ScrollMagic; 导出默认值{ oncreate(){ this.controller=new ScrollM

Sapper中一个页面的script标记中导入库()的实例,该实例在导航到其他页面时保持存在

我尝试在
ondestroy
中销毁它,但无法引用引用实例的变量(在
oncreate
中创建)


如何将脚本标记的范围限定到各个页面,或者在离开页面时销毁实例?

您有两种方法可以访问在
onstroy
中创建的
onstroy
中的内容。第一个是将它们添加为组件的属性:


从“ScrollMagic”导入ScrollMagic;
导出默认值{
oncreate(){
this.controller=new ScrollMagic.controller(…);
this.scene=new ScrollMagic.scene(…);
},
ondestroy(){
this.controller.destroy();
这个.scene.destroy();
}
};
第二个是使用
this.on(“destroy”,…)
表单:


从“ScrollMagic”导入ScrollMagic;
导出默认值{
oncreate(){
const controller=new ScrollMagic.controller(…);
const scene=new ScrollMagic.scene(…);
this.on('destroy',()=>{
controller.destroy();
场景。销毁();
});
}
};

感谢您回答我所有的新手问题。非常感谢!