I am working on a Vue.js app. I am determined to understand the Composition API. However, the syntax throws me off a bit. I feel like I'm creating closures inside of closures, which leads me to believe I'm doing it wrong. I'm trying to set whether a button is enabled/disabled when my component loads. To demonstrate, I've created this fiddle, which includes the following:
example
myApp.component("banner",
{
template: '<button :disabled="isDisabled">generate</button>',
setup(props) {
let isDisabled = true;
function initializeButton() {
fetch('<some-url>')
.then(res => { isDisabled = !res.ok; } )
;
}
initializeButton();
return {
isDisabled
};
}
}
);
However, this does not work. I've narrowed it down to the fact that isDisabled is not "accessible" in the then part of the promise. However, I'm unsure how to actually set isDisabled in then. Plus, I would love to put the initializeButton outside of the setup method, even that, I couldn't do though. I appreciate anyone's help who can help me out.
Thank you.