2

In Vue3 - is there any way to globally disable Attribute Inheritance? I know that it's possible to set inheritAttrs to false when registering / creating a new component but what if I want to generally disallow this kind of behavior without having to change / add inheritAttrs: false to every single component I create?

A related question is Vue JS Non prop attributes and Disabling Attribute Inheritance - but it's only about if it's possible, not how you can do this globally...

The reason I want to disable it is that I want to achieve the same kind of behavior as in React / Angular - forwarding any props without receiving any error / warnings leads to inconsistency / unexpected behavior (possibly - especially with properties such as class and other native HTML Attributes).

A workaround we currently have is to import and re-export any component and "pre-processing" them:

import * as components from './components.ts'; // all components are re-exported with names
export * from './components.ts';

// Disable attribute-inheritance for every single component
Object.values(components).forEach((v) => (v.inheritAttrs = false));

1
  • 2
    I'd recomment to not try to do this as modifying default global behaviour can have long-running consequences, e.g. break third-party components. If you want to make this default behaviour for your own components, create custom wrapper for defineComponent Commented Dec 13, 2021 at 14:12

3 Answers 3

2

It's technically possible to set inheritAttrs=false for every component by using a global mixin:

// main.js
import { createApp } from 'vue'

createApp(App)
  .mixin({ inheritAttrs: false }) 👈
  .mount('#app')

However, you should be aware that this does not cause Vue to emit warnings/errors when trying to set nonexistent props. Any such usage is silently ignored.

demo

Sign up to request clarification or add additional context in comments.

Comments

0

We ended up writing a vite plugin to avoid mixins (since we're using vue3 and mixins are only there for backwards compatibility) to automatically inject { inheritAttrs: false }:

import { Plugin } from 'vite';

const DISABLED_INHERIT_SCRIPT = `
  <script lang="ts"> 
    export default { inheritAttrs: false }; 
  </script>
`;

export const disableAttributeInheritance = (disable = true): Plugin => ({
  name: 'DisableInheritAttributesPlugin',
  transform: (src, id) => ({
    code:
      id.endsWith('.vue') && disable
        ? `${src}\n\n${DISABLED_INHERIT_SCRIPT}`
        : src,
    map: null
  })
});

and use it like that:

export default defineConfig({
  plugins: [
    disableAttributeInheritance(),
    vue()
  ]
});

Comments

0

You should not use defineAsyncComponent when configuring route components with Vue Router.Vue Router supports a similar mechanism for asynchronously loading route components, known as lazy loading. Despite the similarities, this feature is distinct from Vue's support for async components. You can read more about this in the Lazy Loading Routes section of the Vue Router documentation.

https://router.vuejs.org/guide/advanced/lazy-loading.html

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.