简体   繁体   English

Vue Js:在带有 vue-loader 的组件中使用指令

[英]Vue Js: Use directive in component with vue-loader

I'm using vue-loader and I'm facing a problem because I have created a directive in the main app.js to use in a component but is not possible to use that directive in the component.我正在使用 vue-loader 并且遇到了一个问题,因为我在主 app.js 中创建了一个指令以在组件中使用,但无法在组件中使用该指令。

I'm receiving this message:我收到这条消息:

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

app.js应用程序.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应用程序.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见证.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>

I hope you can help me.我希望你能帮助我。

I do not quite understand your code, but if u want to use directive in a component (*.vue file), I can show you how:我不太了解您的代码,但是如果您想在组件(* .vue 文件)中使用指令,我可以向您展示如何:

component/snippet.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>

directive/snippet.js指令/snippet.js

export default {

    update: function (el) {

        console.log('update');
    }
}

For further information, You can check https://v2.vuejs.org/v2/guide/custom-directive.html (the Intro part).有关更多信息,您可以查看https://v2.vuejs.org/v2/guide/custom-directive.html (介绍部分)。

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

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