Home > Web Front-end > Vue.js > The role of export in vue

The role of export in vue

下次还敢
Release: 2024-05-09 13:24:21
Original
966 people have browsed it

The export keyword in Vue.js is used to export components, directives, mixins, and methods for use in other modules or components. It can export: Components: used to expose components from a module for import and use in other modules. Directive: Used to expose directives from modules so that they can be registered in other modules using Vue.directive. Mixins: Used to expose mixins from a module so that they can be imported in other components using the mixins option. Method: used to expose methods from the module for import and use in other modules.

The role of export in vue

The role of export in Vue

In Vue.js, the export keyword Used to export components, directives, mixins, and methods for use in other modules or components.

Export components

export Components are a way to expose components from a module.

// MyComponent.vue
<template>
  <div>My Component</div>
</template>

export default {
  name: 'my-component'
}
Copy after login

In other modules, you can use import to import this component:

// main.js
import MyComponent from './MyComponent.vue'

new Vue({
  components: { MyComponent }
})
Copy after login

Export directive

export Directives are a way of exposing directives from modules.

// my-directive.js
export default {
  bind(el, binding) { },
  update(el, binding) { },
  unbind(el) { }
}
Copy after login

In other modules, you can use Vue.directive to register this directive:

// main.js
import myDirective from './my-directive.js'

Vue.directive('my-directive', myDirective)
Copy after login

Export mixin

export Mixins are a way to expose mixins from a module.

// my-mixin.js
export default {
  created() { },
  mounted() { },
  beforeDestroy() { }
}
Copy after login

In other components, this mixin can be imported using the mixins option:

// MyComponent.vue
<template>
  <div>My Component</div>
</template>

export default {
  mixins: [myMixin]
}
Copy after login

Export method

The export method is a way of exposing methods from a module.

// my-utils.js
export function myMethod() { }
Copy after login

In other modules, you can use import to import this method:

// main.js
import { myMethod } from './my-utils.js'

myMethod()
Copy after login

The above is the detailed content of The role of export in vue. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
vue
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template