在Vue.js中,可以通过使用export关键字来导出文件。导出的内容可以是组件、函数、对象等。以下是几种常见的导出方式:

1. 导出组件:

// MyComponent.vue
<template>
  <div>
    <!-- 组件内容 -->
  </div>
</template>

<script> export default { name: 'MyComponent', // 组件逻辑 } </script>

2. 导出函数或对象:

// utils.js
export function add(a, b) {
  return a + b;
}

export const obj = { // 对象属性和方法 };

3. 导出多个内容:

// utils.js
function add(a, b) {
  return a + b;
}

const obj = { // 对象属性和方法 };

export { add, obj };

在其他文件中,可以使用import语句来导入这些导出的内容。

// App.vue
<template>
  <div>
    <!-- 组件内容 -->
  </div>
</template>

<script> import { add, obj } from './utils';

export default { name: 'App', // 组件逻辑 created() { const sum = add(1, 2); console.log(sum); console.log(obj); } } </script>

这样就可以在别的文件中使用导出的内容了。

Vue导出文件内容:组件、函数、对象导出方法详解 - Vue.js教程

原文地址: https://www.cveoy.top/t/topic/qpWh 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录