node.js - VueJS : Use directive in component with vue-loader

标签 node.js vue.js vue-loader

我正在使用 vue-loader,我遇到了一个问题,因为我在主 app.js 中创建了一个指令以在组件中使用,但无法在组件中使用该指令。

我收到这条消息:

vue.common.js?e881:1014 [Vue warn]: Failed to resolve directive: swiper (found in component: <testimonial-comp>)

app.js

import Vue from 'vue';
import App from './App.vue';

new Vue({
    el: '#app',
    components: { App },
    directives: {
        swiper:  {
            bind: function () {
                console.log('my directive');
          }
        }
    }
});

App.vue

<template lang="jade">
    testimonial-comp
</template>

<script>
    import TestimonialComp from './components/Testimonial.vue'

    export default {
        components: {
            TestimonialComp
        }
    }
</script>

<style lang="stylus" scoped>

</style>

Testimonial.vue

<template lang="jade">
  article#testimonial
    .swiper-container(v-swiper)
      .swiper-wrapper
        .swiper-slide Slide 1
        .swiper-slide Slide 2
        .swiper-slide Slide 3
      .swiper-pagination
      .swiper-button-prev
      .swiper-button-next
      .swiper-scrollbar

</template>

<script>
  import Swiper from 'Swiper/dist/js/swiper.min.js'

</script>
<style lang="stylus">

</style>

我希望你能帮助我。

最佳答案

我不太明白你的代码,但如果你想在组件(*.vue 文件)中使用指令,我可以告诉你如何:

组件/snippet.vue

<template>

    <div id="snippet">
        <code v-snippet>
            {{snippet.code}}
        </code>
    </div>

</template>


<script>

import snippet from '../directive/snippet';


export default {
    directives: {
        snippet: snippet
    }
}

</script>

指令/snippet.js

export default {

    update: function (el) {

        console.log('update');
    }
}

更多信息,您可以查看https://v2.vuejs.org/v2/guide/custom-directive.html (介绍部分)。

关于node.js - VueJS : Use directive in component with vue-loader,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37221341/

相关文章:

node.js - 从 salesforce.com 访问本地 nodejs 服务器

node.js - 在 Node.js 中使用 Winston 在 JSON 对象中输出日志

javascript - Vue.JS Vue-Tables Laravel 关系

javascript - 为什么 vue <template> 对 v-bind 无效 :src?

node.js - Socket.io 检测客户端何时断开连接

Vue.js:我们应该将脚本或模板放在单个文件组件中以提高生产力吗?

Vue.js 从子组件关闭模态

vue.js - 如何从方法内部访问数据

webpack - 如何在我的组件 VUE 2 (webpack) 中加载外部 js 文件?

javascript - 使用 Webpack 4 减少和优化具有两个入口点的 bundle