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

rule: no v-if on template root #1467

Closed
privatenumber opened this issue Mar 26, 2021 · 6 comments · Fixed by #2138
Closed

rule: no v-if on template root #1467

privatenumber opened this issue Mar 26, 2021 · 6 comments · Fixed by #2138

Comments

@privatenumber
Copy link
Contributor

privatenumber commented Mar 26, 2021

Please describe what the rule should do:

Warn on using v-if on the template root node, because:

  1. Whether a component should be rendered or not is a usage concern. Specifically, it's confusing when a declared component is not rendered:

    /* Usage */
    <template>
        <div>
            ...
            <custom-component /> /* Readers expect this to be rendered */
        </div>
    </template>
    
    
    /* CustomComponent.vue */
    <template>
        <div v-if="false">
            ...
        </div>
    </template>
  2. There are performance benefits:

    1. Instantiating a new stateful component only for it to not be rendered via root v-if="false" is wasteful. If possible, it should happen in the parent usage life-cycle.
    2. When moving the v-if to the parent, the component can be asynchronously loaded when needed, so not only will it save on component registration, it will save on module declaration and asset size.
      <template>
          <div>
              ...
              <custom-component v-if="shouldShow" /> /* Won't be loaded till true */
          </div>
      </template>
      
      <script>
      export default {
      	components: {
              CustomComponent: () => import('./CustomComponent.vue')
          }
      }
      </script>

What category should the rule belong to?

[ ] Enforces code style (layout)
[ ] Warns about a potential error (problem)
[x] Suggests an alternate way of doing something (suggestion)
[ ] Other (please specify:)

Provide 2-3 code examples that this rule should warn about:

Bad

/* Usage */
<template>
    <div>
        <custom-component />
    </div>
</template>


/* CustomComponent.vue */
<template>
    <div v-if="shouldShow">
        ...
    </div>
</template>

Good

/* Usage */
<template>
    <div>
        <custom-component v-if="shouldShow" />
    </div>
</template>


/* CustomComponent.vue */
<template>
    <div>
        ...
    </div>
</template>

Additional context

  • There are likely cases where it's not possible to do this. I think this would be a warning in "recommended", or not even a pre-enabled rule.
@ota-meshi
Copy link
Member

Thank you for rule proposal!
I think it's good to make it rule available to users.

I think the following template needs to be valid.

<template>
    <div v-if="mode === 'a'">
        ...
    </div>
    <div v-if="mode === 'b'">
        ...
    </div>
</template>
<template>
    <div v-if="loading">
        ...
    </div>
    <div v-else>
        ...
    </div>
</template>

@privatenumber
Copy link
Contributor Author

Yeah that's a great point. Good catch!

@chelorope
Copy link

I think that this is already covered by this rule vue/valid-template-root

@privatenumber
Copy link
Contributor Author

How so?

@songpengyuan
Copy link
Contributor

I would like to work on this @ota-meshi

@ota-meshi
Copy link
Member

Yeah, thank you!

@ota-meshi ota-meshi linked a pull request Apr 30, 2023 that will close this issue
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants