【问题标题】:Reinit javascript function when change view in vue js在vue js中更改视图时重新初始化javascript函数
【发布时间】:2018-09-19 21:22:58
【问题描述】:

我是 vue.js 的新手

我有一个带有 vue.js 的 laravel 应用程序。当 hp 正在加载脚本时,所有元素都被初始化(owl carousel、rev 滑块等),但是当我单击其他路线联系或关于并返回 hp 时,滑块或其他与 js 相关的内容不会加载。

routes.js
import VueRouter from 'vue-router';
import Home from './components/views/Home.vue';
import About from './components/views/About.vue';
import Contact from './components/views/Contact.vue';


let routes = [
    { path: '/', component: Home },
    { path: '/about', component: About },
    { path: '/contact', component: Contact },
    { path: '/notes', component: Notes }
];

export default new VueRouter({
    routes,
    linkActiveClass: 'active'
});

和app.js

import router from './routes';

import './components';

const app = new Vue({
    el: '#app',
    router
});

有没有办法在我每次更改视图时运行加载轮播等功能?

【问题讨论】:

  • 它们现在在哪里以及如何加载?
  • 其加载为 进入 master.blade
  • 好的 - 通常你会将该代码放在 vue 组件中,而不是单独加载它。

标签: javascript laravel-5 vue.js


【解决方案1】:

在挂载的触发器上,您可以为每个组件添加自定义 js

<script>
    export default {
        mounted () {

        }
    }
</script>

【讨论】:

    【解决方案2】:

    如果有人偶然发现这一点并且仍在寻找一种方法来做到这一点,这就是我设法做到这一点的方法。将 &lt;route-view/&gt; 包装在 &lt;transition&gt; 中,您可以使用 css 对其进行控制,并在 enter 上调用一个方法来调用您想要的函数。 这将在每次路由更改时在 DOM 中加载组件时立即调用该函数

    <transition name="slide" v-on:enter="reInitJS">
       <router-view></router-view>
    </transition>
    
    <script>
    //import the wanted function
    import {init} from './main';
    export default {
      name: 'App', 
      methods: {
        reInitJS(){
        //call the function
          init(); 
        }
      }
    }
    </script>
    

    【讨论】:

      猜你喜欢
      • 2014-06-01
      • 1970-01-01
      • 2020-08-08
      • 1970-01-01
      • 2022-01-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多