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
890 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.

<code class="javascript">// MyComponent.vue
<template>
  <div>My Component</div>
</template>

export default {
  name: 'my-component'
}</code>
Copy after login

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

<code class="javascript">// main.js
import MyComponent from './MyComponent.vue'

new Vue({
  components: { MyComponent }
})</code>
Copy after login

Export directive

export Directives are a way of exposing directives from modules.

<code class="javascript">// my-directive.js
export default {
  bind(el, binding) { },
  update(el, binding) { },
  unbind(el) { }
}</code>
Copy after login

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

<code class="javascript">// main.js
import myDirective from './my-directive.js'

Vue.directive('my-directive', myDirective)</code>
Copy after login

Export mixin

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

<code class="javascript">// my-mixin.js
export default {
  created() { },
  mounted() { },
  beforeDestroy() { }
}</code>
Copy after login

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

<code class="javascript">// MyComponent.vue
<template>
  <div>My Component</div>
</template>

export default {
  mixins: [myMixin]
}</code>
Copy after login

Export method

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

<code class="javascript">// my-utils.js
export function myMethod() { }</code>
Copy after login

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

<code class="javascript">// main.js
import { myMethod } from './my-utils.js'

myMethod()</code>
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
source:php.cn
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
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template