简体   繁体   English

Barba.jS 中的页面转换后脚本未加载

[英]Scripts are not loading after the page transition in Barba.jS

So I have one index file that has few js files are included like Gsap & Barba JS.所以我有一个索引文件,其中包含很少的 js 文件,例如 Gsap 和 Barba JS。 But I have one inner page that includes some extra js files such as locomotive.js & OWLcarousel.js and please note that these two Js files (locomotive.js & OWLcarousel.js) are not included on index page as they are not required.但是我有一个内页,其中包含一些额外的 js 文件,例如 locomotive.js 和 OWLcarousel.js,请注意这两个 Js 文件(locomotive.js 和 OWLcarousel.js)不包含在索引页面中,因为它们不是必需的。 The issue is the inner page opens after completing the index page transition but without loading these two JS files (locomotive.js & OWLcarousel.js).问题是内页在完成索引页面转换后打开,但没有加载这两个 JS 文件(locomotive.js 和 OWLcarousel.js)。 As a result both locomotive scrolling & OWL carousel are not working.结果,机车滚动和 OWL 旋转木马都不起作用。 However, If I'll refresh the inner page once then these two Js files (locomotive.js & OWLcarousel.js) are loading and everything works fine.但是,如果我刷新一次内页,那么这两个 Js 文件(locomotive.js 和 OWLcarousel.js)正在加载并且一切正常。 So how to load JS files in Barba.Js for inner pages?那么如何在 Barba.Js 中加载内页的 JS 文件呢?

Update: I have created a minimal demo and you can see that inner page is not loading the JS and CSS of Locomotive and also OWL carousel if click on heading text on homepage and navigate to inner.更新:我创建了一个最小的演示,如果单击主页上的标题文本并导航到内部,您可以看到内部页面没有加载机车的 JS 和 CSS 以及 OWL 轮播。 But if refresh the page then both locomotive & owl carousel are working fine in inner page.但是如果刷新页面,那么机车和猫头鹰旋转木马在内页都可以正常工作。 How to fix this?如何解决这个问题? I have added the suggested change to the JS file ( https://bootstrap-awesome.com/barba-test1f/js/main-test.js ) Demo Link: https://bootstrap-awesome.com/barba-test1f/index-test.html我已将建议的更改添加到 JS 文件( https://bootstrap-awesome.com/barba-test1f/js/main-test.js )演示链接: https://bootstrap-awesome.com/barba-test1f/指数测试。html

So I tried to load the additional JS files in the same below format but nothing happens.因此,我尝试以以下相同的格式加载其他 JS 文件,但没有任何反应。 Can anyone guide me with this please?有人可以指导我吗?

barba.init({
  views: [{
        namespace: 'home',
        beforeEnter({ next }) {

        // load your script
        let script = document.createElement('script');
        script.src = '<script src="https://cdn.jsdelivr.net/npm/locomotive-scroll@3.5.4/dist/locomotive-scroll.min.js"></script>'; 
script.src = '<script src="js/owl/owl.carousel.js"></script>';
        next.container.appendChild(script);
        }, 
    }],

在此处输入图像描述

网络控制台中的错误

I would try something like this, where you add the scripts you want in an array, then loop through each one and build a <script src="..."></script> string and append to the next container.我会尝试这样的事情,在数组中添加所需的脚本,然后遍历每个脚本并构建<script src="..."></script>字符串和 append 到next容器。

 barba.init({ views: [{ namespace: 'home', beforeEnter({ next }) { // Script URLs to load let pageScriptSrcs = [ 'https://cdn.jsdelivr.net/npm/locomotive-scroll@3.5.4/dist/locomotive-scroll.min.js', 'js/owl/owl.carousel.js' ] pageScriptSrcs.forEach(scriptSrc => { let script = '<script src="' + scriptSrc + '"><\/script>'; console.log(script); next.container.appendChild(script); }) }, }], })

The below is what I have tried but still not working.以下是我尝试过但仍然无法正常工作的方法。 Also, how to load Locomotive external CSS?另外,如何加载机车外部CSS?

JS: bootstrap-awesome.com/barba-test1f/js/main-test.js JS:bootstrap-awesome.com/barba-test1f/js/main-test.js

Demo: https://bootstrap-awesome.com/barba-test1f/index-test.html演示: https://bootstrap-awesome.com/barba-test1f/index-test.html

views: [{
    namespace: 'home',
    beforeEnter({ next }) {
      // Script URLs to load
      let pageScriptSrcs = [
        'https://cdn.jsdelivr.net/npm/locomotive-scroll@3.5.4/dist/locomotive-scroll.min.js',
        'js/custom-scroll.js',
        'js/owl/owl.carousel.js',
        'js/owl/owl-custom.js'
      ]
      
      pageScriptSrcs.forEach(scriptSrc => {
        let script = '<script src="' + scriptSrc + '"><\/script>';
        console.log(script);
        next.container.appendChild(script);
      })
    }
    },
    {
    namespace: 'projdetailpage',
    beforeEnter({ next }) {
      // Script URLs to load
      let pageScriptSrcs = [
        'https://cdn.jsdelivr.net/npm/locomotive-scroll@3.5.4/dist/locomotive-scroll.min.js',
        'js/custom-scroll.js',
        'js/owl/owl.carousel.js',
        'js/owl/owl-custom.js'
      ]
      
      pageScriptSrcs.forEach(scriptSrc => {
        let script = '<script src="' + scriptSrc + '"><\/script>';
        console.log(script);
        next.container.appendChild(script);
      })
    }

  }],

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

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