我目前正在运行一个已编译的scripts.js文件,该文件使用onRendered方法激活一系列各种UI功能(砌体,导航栏等)。
Template.mainLayout.onRendered( function() {
$.getScript('js/scripts.js');
});
我的问题是,如果我导航到像/ team这样的其他页面,则脚本不会重新运行,这意味着在其他页面中,砌体不再起作用。此外,当我回到主页时,脚本也会断开,导致导航栏坏了。
如果我使用此功能错误,请告诉我?
答案 0 :(得分:1)
如果您自己托管脚本,请将它们放在client/compatibility
文件夹中。 Meteor会自动将它们包含在您的应用中。
如果您想从CDN加载它们,请修改client/index.html
文件夹以添加script
标记。
然后在onRendered
回调中,输入初始化自定义脚本的代码,例如: this.$('table').datatable()
或其他什么。
答案 1 :(得分:0)
Eddie,.getscript()是一个异步函数,所以你需要指定一个calback。我需要在meteor中加载一个外部JS作为传单插件。使用异步,定义回调函数做了hack:
$.getScript('js/l.control.geosearch.js', function( data, textStatus, jqxhr ) {
$.getScript('js/l.geosearch.provider.google.js', function( data, textStatus, jqxhr ) {
new L.Control.GeoSearch({
provider: new L.GeoSearch.Provider.Google(),
position: 'topcenter',
showMarker: false,
retainZoomLevel: true,
}).addTo(map);
})
})