简体   繁体   English

Shopify - 在内联部分/片段脚本中使用您的 JS 库等

[英]Shopify - using your JS libraries etc in inline section / snippet scripts

I want to use a script within the file for a specific section (in my collection-template.liquid in this circumstance, but it could be in any) - I want to place the script at the bottom of the file.我想在文件中为特定部分使用脚本(在这种情况下在我的 collection-template.liquid 中,但它可能在任何情况下) - 我想将脚本放在文件的底部。 However when you use inline scripts in this way, they don't seem to reference other libraries that you have saved on your theme.但是,当您以这种方式使用内联脚本时,它们似乎不会引用您保存在主题中的其他库。

In my example I want to be able to use jQuery in this script, but jQuery doesn't appear to be loaded - ie it doesn't recognise any jQuery or any other libraries.在我的示例中,我希望能够在此脚本中使用 jQuery,但 jQuery 似乎没有加载 - 即它无法识别任何 jQuery 或任何其他库。 It does work for the theme.js, but I need this script to be only in this section/snippet.它确实适用于theme.js,但我需要这个脚本只在这个部分/片段中。 I need the script to be inline because it uses liquid - moving it to the theme.js is not an option in this case.我需要将脚本内联,因为它使用液体 - 在这种情况下,将其移动到 theme.js 不是一个选项。

Can anyone recommend a solution to include these libraries (perhaps its as simple as including a reference to the vendor.js file or otherwise) - and also if possible explain why these aren't referenced as standard?任何人都可以推荐一个包含这些库的解决方案(可能就像包含对 vendor.js 文件或其他文件的引用一样简单) - 如果可能的话,还可以解释为什么这些库没有被引用为标准? As it seems strange to me that this issue even exists.对我来说,这个问题甚至存在似乎很奇怪。 I am using the Debut theme.我正在使用 Debut 主题。

I appear to have fixed this simply by declaring the jQuery script (by inserting the below) immediately before the closing </head> tag.我似乎通过在结束</head>标记之前立即声明 jQuery 脚本(通过插入以下内容)来解决此问题。

<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>

jQuery is now being loaded first and is working in snippets etc.现在首先加载 jQuery 并在片段等中工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM