如何在Nuxt中加载特定页面的外部文件?
我遵循了这个指南:https://nuxtjs.org/faq/#local-settings,如果我加载带有服务器端渲染的页面(即直接访问URL ),它就会工作。
但是,如果我使用链接(从vue路由器)转到页面,则脚本将不存在。
发布于 2020-02-17 23:54:32
在创建沙箱之后,我意识到当您导航到沙箱时,它确实会加载脚本。问题是,如果您试图在实际加载脚本之前加载外部库方法/函数/变量(例如,在mounted()调用中)。我是这样解决的:
举个例子,如果你想加载一个特定页面的jQuery,这将会失败:
mounted () {
jQuery('.element').hide();
}但是,您可以设置一个间隔,以便在加载jQuery时进行检查,如下所示:
mounted () {
const awaitScriptInterval = setInterval(() => { // set an interval to check when script is loaded
if (typeof jQuery === 'undefined') { // script is not yet loaded in
return;
}
clearTimeout(awaitScriptInterval); // clear the interval, so we don't continuously check
jQuery('.element').hide(); // use your external library
}, 500); // the interval in milliseconds to check
}有关如何加载外部脚本/样式的信息,请参阅本文:https://nuxtjs.org/faq/#local-settings
https://stackoverflow.com/questions/60265660
复制相似问题