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

Methods in functional components #8287

Closed
wants to merge 2 commits into from

Conversation

privatenumber
Copy link
Contributor

@privatenumber privatenumber commented Jun 1, 2018

What kind of change does this PR introduce? (check at least one)

  • Bugfix
  • Feature
  • Code style update
  • Refactor
  • Build-related changes
  • Other, please describe:

Does this PR introduce a breaking change? (check one)

  • Yes
  • No

If yes, please describe the impact and migration path for existing applications:

The PR fulfills these requirements:

If adding a new feature, the PR's description includes:

  • A convincing reason for adding this feature (to avoid wasting your time, it's best to open a suggestion issue first and wait for approval before working on it)

Other information:
Currently it's difficult to use the functional template since you lose access to closures where as the render method can reference closed-over functions.

privatenumber and others added 2 commits May 7, 2018 15:59
Supports the "methods" object in functional components for better functional-template usability

re vuejs#8142
@Kingwl
Copy link
Member

Kingwl commented Jun 1, 2018

why functional component need method?

@privatenumber
Copy link
Contributor Author

@Kingwl I already explained in the PR body but, when using a render method instead of using a functional template, I can write functions outside of the component and reference them via closure. However, when using a template, I no longer have access to the closure.

I can reference them via $options.methods but seems hacky and would prefer official support in offering a similar API to stateful components.

@Kingwl
Copy link
Member

Kingwl commented Jun 1, 2018

I respect your work, but I think there will be a better solution(maybe)
/ cc: @posva

@posva
Copy link
Member

posva commented Jun 1, 2018

I think that rather than having just methods, it will be useful to be able to inject stuff in the context so it can also be accessed in templates

@privatenumber
Copy link
Contributor Author

privatenumber commented Jun 1, 2018

What are you thinking @Kingwl ?

@posva If I've understood your suggestion correctly, that is what this PR is doing; it allows methods to be injected into the context so it can be accessed in the template. I'd like to see more support of functional components so that they can be easier to use and hence more useful; this will be a step towards that.

Here's an issue in vue-loader (it was originally in vue but closed) that might explain the use-case more clearly: vuejs/vue-loader#1291

@yyx990803
Copy link
Member

yyx990803 commented Oct 23, 2018

I don't think we should be going further along the route of "making functional components work like normal components in a Vue file", for the following reasons:

  • In v3 functional components can and should just be functions
  • In v3, stateful components are much faster to initialize, and this should make it less necessary to use functional components just for performance
  • v3 stateful components can also return multiple root nodes
  • If we want to make functional components fully equivalent to stateful ones in a Vue file, we go down the rabbit hole of supporting components, directives, methods, provide/inject and all other options... which eventually defeats the simplicity of functional components.

I think we should go back and make functional components more about "functions".

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.

4 participants