-
Notifications
You must be signed in to change notification settings - Fork 343
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
Method context dependent on parentheses use #144
Comments
the methods returned in setup should indeed be bound to the component instance |
I can fix this, will create a PR in a bit :) |
@posva According to the origin RFC: And what about the consistency of inline render function? const comp = createComponent({
setup() {
const onButton = function() {
// what would you expect `this` to be?
console.log(this);
};
return () =>
h('button', {
on: {
click: onButton,
},
}, 'click me');
},
}); I wouldn't think it should be bound to the component instance. |
You are right. Should we bind to It's inconvenient though, I need to revisit the RFC (I may be forgetting about something we already talked about) because when starting using the Composition API and mixing it with the object api, people will likely need to reference other properties in the component instance. At the same time, some of these properties are not available right away so having access to a partial |
Consistent behaviour would be better, since the composition-api behaviour is not aligned with 2.x. I think this is still an issue, because is such a nuance behaviour, that will be difficult for someone to spot when doing a mix on createComponent({
template: `<div>
<p>{{a}}</p>
<button @click="increment"> increment</button>
<button @click="increment()"> increment()</button>
</div>`,
data() {
return {
a: 1
};
},
setup() {
const increment = function() {
console.log(this)
this.a++;
};
return {
increment
};
}
}); I think there's still an issue here, since it should behave the same in this case IMO. |
The intention of the behaviour as described in the RFC is to draw a distinct line:
So it's essentially a one-way street. And it's done this way so code that is extracted into setup is strictly decoupled from the component interface. The code you provided as an example is tightly coupled to components that have a Of course this forces people to move stuff that's meant to be fed into a composition function into the setup function, but that's ok in my eyes. |
Currently that's not the behaviour of the
I agree with "one-way street", it simplifies everything. One concern I have is access to |
it isn't? Since right now you can't access
Some plugins use global mixins to add properties to the The plugins will have to either switch to a const myCoolFeature { /* whatever */ }
export const key = Symbol('myCoolFeature')
function install () {
Vue.prototpe.$myCoolFeature = myCoolFeature
provide(key, myCoolFeature )
} // usage in otions API
methods {
someMethod() {
this.$myCoolFeature
}
}
// usage in composition API
import { key } from 'my-cool-plugin'
setup() {
const myCoolFeature = inject(key)
function someMethod() {
myCoolFeature
}
return {
someMethod
}
} |
If the
I think we were talking about different things, I was referring to the behaviour when call a function in the template with parentheses and without parentheses. What you are referring is the behaviour in the <button @click="onButton">no parens (this === window)</button>
<button @click="onButton()">with parens (this === vm)</button> I think this an unexpected behaviour in my opinion, as far as I know is not described anywhere. on the RFC
My point is having different behaviour with/without parentheses, based on the docs, the function should always be bound to |
I was playing with composition API when I stumble upon this issue. setup () {
function logVM () {
console.log(this); // expecting current vm here
};
return {
logVM,
};
}, That said, it will not conflict at all with this statement (which I definetly agree with):
|
I agree. And according to the current implementation in the |
Well, after further investigation, there's a subtle difference between the runtime-compiled version of the template and the pre-compiled version. So now I'm not sure which is the intended behavior. We might need to discuss this issue in the vue-next repo first. |
When exporting methods from a compositional function, the
this
context changes depending on if the template uses parentheses to call it. This is not the case with 2.x options methods. I've created a codepen that demonstrates this bug.The text was updated successfully, but these errors were encountered: