Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed error when vue module exports a function #137

Closed
wants to merge 1 commit into from

Conversation

karevn
Copy link

@karevn karevn commented Jan 18, 2016

This code would rise an error:

<script>
module.exports = function (context) {
}
</script>

The deal is that module.options is not defined in this case, and there's module.options.template usage in vue-loader.

@yyx990803
Copy link
Member

What's the use case of this? It's not a supported usage pattern.

@karevn
Copy link
Author

karevn commented Jan 18, 2016

Dependency injection, especially when the dependency object is created by external code and cant be require'd.

@karevn
Copy link
Author

karevn commented Jan 18, 2016

Just to make things clear - I mean the JS code inside .vue file. And the code that handles functions exported is already there, but does not work if module does not also export .options property, which is illogical.

@yyx990803
Copy link
Member

By design the *.vue file expects the export to be either an options object or a component constructor function... does usage like this work with hot module replacement?

@karevn
Copy link
Author

karevn commented Jan 18, 2016

I suppose you mean something like:

Vue = require('vue')
module.exports = Vue.extend(...)

but regardless of what the intention was - it still does not allow dependency injection.

So what I am trying to do is to inject my dependencies when creating the component:

parent.vue

<script>
module.exports = {
  components: require('child.vue')(dependencyReceivedFromTheEnvironment)
}
</script>

child.vue:

<script>
module.exports = function (dependency) {
  return ....//component created using the dependency - like local jQuery, service object, etc.
}
</script>

Even though it was not designed to work like this, it's was dead easy to fix the problem with my approach.

@yyx990803
Copy link
Member

What I am pointing out is - the toolchain around vue-loader, e.g. hot reload, is based upon the assumption that the main export from a *.vue file is always either an options object or a constructor. An arbitrary factory function may work in some cases with your fix, but not in all cases. If we want to officially support this pattern then it should work in all cases, but your proposed fix doesn't do that.

@karevn
Copy link
Author

karevn commented Jan 18, 2016

Well. How do I implemement dependency injection then?

@yyx990803
Copy link
Member

I'd like to know what makes dependency injection necessary when there is a module system. If it's for testing you can use vue-loader's inject mode.

@karevn
Copy link
Author

karevn commented Jan 22, 2016

Most staightforward one - when you need to build a redistributable
"container" component that will wrap the dynamic content without ugly
workarounds. Or when you receive a backend communication service object
inherited from the legacy system. You name it....

On Fri, Jan 22, 2016 at 8:45 PM, Evan You [email protected] wrote:

I'd like to know what makes dependency injection necessary when there is a
module system. If it's for testing you can use vue-loader's inject mode.


Reply to this email directly or view it on GitHub
#137 (comment).

@yyx990803 yyx990803 closed this in eee6bdd Jan 24, 2016
@karevn
Copy link
Author

karevn commented Jan 28, 2016

eee6bdd does not fix this issue, as it creates a new empty object and effectively ignores the template exported by .vue file.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants