nuxt / bridge

🌉 Experience Nuxt 3 features on existing Nuxt 2 projects
MIT License
273 stars 29 forks source link

Script setup variable inaccessible from script #1147

Closed wJoenn closed 7 months ago

wJoenn commented 7 months ago

Environment


Reproduction

https://stackblitz.com/edit/github-fxasxi?file=pages%2Findex.vue

Describe the bug

Before updating our application from 2.15.7 + @nuxtjs/composition-api to 2.17.2 + bridge we used to create components in a script setup tag and use Vuelidate's validations on variables declared in the setup script from an adjacent script tag. (It's necessary to keep vuelidate's validations inside a classic script becaus eit's not compatible with the composition API)

<script setup>
const task = ref('');
</script>

<script>
import { required } from 'vuelidate/lib/validators';

export default defineComponent({
  validations() {
    return {
      task: { required },
    };
  },
});
</script>

Since we updated, this is no longer possible. The setup variables are now inaccessible in the script so Vuelidate keeps trying to validate the presence of an undefined variable instead of the actual ref

I thought it would be solved with https://github.com/nuxt/bridge/pull/1104 but even after updating bridge to 3.1 is does not solve the problem

#

You can test this from the reproduction, simply input anything in the text field then press submit and notice how undefined is being logged in the devtool and the validation errors is set to true (meaning the validation didn't succeed)

Additional context

No response

Logs

No response

wattanx commented 7 months ago

Looks like defineExpose will solve this.

<script setup>
const task = ref('');

defineExpose({
  task
})
</script>

Also, vuelidate 2 supports the Composition API.

wJoenn commented 7 months ago

Looks like defineExpose will solve this

Can confirm it does ! If there an official API to solve this then I suppose it's not a bug but the intended behaviour then. I wonder why it used to work with @nuxtjs/composition-api 🤷

Also, vuelidate 2 supports the Composition API.

Yeah but we don't really like the way the changes vuelidate in the newest version. We decided to move to vee-validate instead but we have a lot of forms to refactor which will be done over a few weeks so we had to make Bridge work with vuelidate 0

#

Thanks a lot for the help Wattanx 🙌