vue-touch-events icon indicating copy to clipboard operation
vue-touch-events copied to clipboard

Failed to resolve directive: touch

Open skjnldsv opened this issue 5 years ago • 18 comments

Using this library into another library and then importing the 2nd library doesn't work.

We have a Modal component on the https://github.com/nextcloud/nextcloud-vue library and we wanted to use your library, though when importing the modal component on another app, we get the Failed to resolve directive: touch error.

Though importign and using your library on the nextcloud-vue library is done properly:

import Vue from 'vue'
import Vue2TouchEvents from 'vue2-touch-events'

Vue.use(Vue2TouchEvents)

~~I'm guessing this is because you don't export your plugin as a ssr module or something webpack can understand and pack and bundle on other libraries. But I might be wrong! :)~~

EDIT: I was close, this is because you do not export the directive itself but only the {install:xxx, ...} object Vue.use requires. Please check https://github.com/Akryum/v-tooltip/blob/master/src/index.js for a working example. It exports the vue directive as well so that webpack can understand how things works and include everything.

skjnldsv avatar Feb 25 '19 07:02 skjnldsv

Maybe I got your point, but I didn't use ES6's export. I'm not sure whether it works or not if I change module.exports to export.

if (typeof module === 'object') {
-    module.exports = vueTouchEvents
+   exports.install = vueTouchEvents.install
+   exports.vueTouchEvents = vueTouchEvents
}

jerrybendy avatar Feb 25 '19 08:02 jerrybendy

It sill behave the same :confused: I think you need to restructure the vueTouchEvents objet and separate it from your install function. https://github.com/Akryum/v-tooltip/blob/e613e2b301740c68dd0f618b0101f75ea33c30dd/src/directives/v-tooltip.js#L215-L222

skjnldsv avatar Feb 25 '19 09:02 skjnldsv

You're right. I should restructure the code to separate the install function. But I can't understand the code you referenced.

export const directive = {
	options: defaultOptions,
	bind,
	update: bind,
	unbind (el) {
		destroyTooltip(el)
	},
}

Why does v-tooltip export a directive? What does it do? 🤔

jerrybendy avatar Feb 25 '19 09:02 jerrybendy

This is the default directive that vue can use in the directives component. Or with Vue.directive('directive, directive) instead of Vue.use which doesn't properly declare it it seems. (in plugin systems like nextcloud-vue or any big bundles like vuetify, vue-material...) https://vuejs.org/v2/guide/custom-directive.html

props: {
  ...
},
directives: {
  focus: {
    // directive definition
    inserted: function (el) {
      el.focus()
    }
  }
}

skjnldsv avatar Feb 25 '19 10:02 skjnldsv

OK, I see. I will restructure my code to support it 😬

jerrybendy avatar Feb 25 '19 11:02 jerrybendy

Maybe first pull my feature.. might be easier then afterwards.

Fragilem17 avatar Feb 25 '19 15:02 Fragilem17

Any news on this? I am having the same problem.

ChristophAnastasiades avatar May 23 '19 10:05 ChristophAnastasiades

I added a commit here #39 to support custom directives, but I can't make sure it can work well or not 😅

const touchDirective = require('vue2-touch-events').touchDirective

Vue.directive('touch', touchDirective)

jerrybendy avatar May 24 '19 04:05 jerrybendy

Not sure if this is a similar issue but i get the same warning so i don't want to make a new post.

Clean install of vue with the cli tool and just adding

Vue.use(Vue2Touch)

and adding this to a div

v-touch:swipe="swipeHandler"

breaks the code with

[Vue warn]: Failed to resolve directive: touch

What I can do, is to run the dev server without the v-touch line, write it back into my div, do a hot reload, and then everything works like it should. 👍 If i reload my tab it breaks again tho. 👎

ptrckdev avatar Jul 05 '19 08:07 ptrckdev

@ptrckdev Which version did you use?

import Vue from 'vue'
import Vue2TouchEvents from 'vue2-touch-events'

Vue.use(Vue2TouchEvents)

This will run install function and register the v-touch directive.

jerrybendy avatar Jul 05 '19 08:07 jerrybendy

Fixed, didnt work with Vue for a year now and put the Vue.use under new Vue() in my main.js 🙄

ptrckdev avatar Jul 05 '19 09:07 ptrckdev

@ptrckdev Yep. You can't put Vue.use under new Vue.

jerrybendy avatar Jul 05 '19 09:07 jerrybendy

I added a commit here #39 to support custom directives, but I can't make sure it can work well or not 😅

const touchDirective = require('vue2-touch-events').touchDirective

Vue.directive('touch', touchDirective)

What aspects are you uncomfortable with? It seems with the latest version of vuetify-loader (at least in our build), the v-touch directive won't work as it conflicts with their own v-touch directive.

thequiet avatar Feb 14 '20 23:02 thequiet

I had the same issue and it was caused by having 2 plugins in the same Vue.use. I changed Vue.use(Argon, Vue2TouchEvents); to

Vue.use(Argon);
Vue.use(Vue2TouchEvents);

aezur avatar Apr 24 '20 18:04 aezur

It seems to be a big problem. I will resolve this issue ASAP

jerrybendy avatar Apr 25 '20 05:04 jerrybendy

Not sure if this is that same issue. When testing with vue test utils and jest I get this error for each instance of v-touch in my html:

      (found in <Component>)
    console.error node_modules/vue/dist/vue.runtime.common.dev.js:621
      [Vue warn]: Failed to resolve directive: touch

I am importing it into my test like this: const touchDirective = require("vue2-touch-events").touchDirective;

describe("TEST", () => {
  const localVue = createLocalVue();
  localVue.use(Vuex);
  localVue.directive(touchDirective);

  const factory = propsData => {
    return mount(Component, {
      localVue
    });
  };

  it("isCompleted", async () => {
    const wrapper = factory();
    ...

Any idea how to fix this?

BrianDavidYork-zz avatar Aug 03 '20 19:08 BrianDavidYork-zz

@BrianDavidYork vue2-touch-events now exports a install function only. So that you can do this:

const vue2TouchEvents = require('vue2-touch-events');
// ...
localVue.use(vue2TouchEvents);
// ...

jerrybendy avatar Aug 04 '20 02:08 jerrybendy

Is there any option to define a custom directive when installing? I'am using Vuetify and the component "v-touch" seems to override this package.

import Vue2TouchEvents from 'vue2-touch-events';
Vue.use(Vue2TouchEvents);

alekbless avatar Dec 04 '20 09:12 alekbless