全栈的自我修养 ———— vue中export的几种导入与引用方式

vue中的引用和导入方式一般分为三种,直接导入,默认导入,按需导入

直接导入

// directionExport
alert('这是直接导入')
<template>
  <div></div>
</template>

<script>
import './js/directionExport'
export default {
}
</script>

默认导入

// test2
const a = 10;
const b = 20;
function sum (a, b) {
    return a + b
    
}
export default {
    a,
    b,
    sum
}
<template>
  <div>
  </div>
</template>

<script>
import result from './js/test2'
export default {
  data() {
    return {
      a: result.a,
      b: result.b,
      sum: 0
    }
  },
  methods: {
    testMethod() {
      this.sum = result.sum(this.a,this.b)
    }
  },
  mounted() {
    this.testMethod()
    alert(this.sum)
  }
}
</script>

按需导入

// test3
export const i = 1000;
export const b = 2000;
export function sum (i, b) {
    return i+b
}
export default {
    i,b,sum
}
<template>
  <div>
    {{ i }}
    {{ b }}
    {{ c }}
  </div>
</template>

<script>
import { i,b,sum } from '../../js/test3'
export default {
  data() {
    return {
      i: i,
      b: b,
      c: 0
    };
  },
  methods: {
    print() {
      this.c = sum(200,400)
    }
  },
  mounted() {
    this.print()
  }
}
</script>

<style>

</style>

相关推荐

  1. 自我修养 ———— uniapp封装api请求

    2024-03-17 10:22:02       12 阅读
  2. ES6 export暴露和引用方式

    2024-03-17 10:22:02       23 阅读
  3. vue export default export 写法区别

    2024-03-17 10:22:02       14 阅读
  4. Vue下载不同文件方式

    2024-03-17 10:22:02       42 阅读

最近更新

  1. TCP协议是安全的吗?

    2024-03-17 10:22:02       16 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2024-03-17 10:22:02       16 阅读
  3. 【Python教程】压缩PDF文件大小

    2024-03-17 10:22:02       15 阅读
  4. 通过文章id递归查询所有评论(xml)

    2024-03-17 10:22:02       18 阅读

热门阅读

  1. python 直方图

    2024-03-17 10:22:02       20 阅读
  2. 动态规划 完全背包问题 携带研究材料

    2024-03-17 10:22:02       21 阅读
  3. 数据仓库的设计开发应用(三)

    2024-03-17 10:22:02       20 阅读
  4. 52. 携带研究材料(第七期模拟笔试)

    2024-03-17 10:22:02       20 阅读
  5. kotlin flow sample的用法

    2024-03-17 10:22:02       16 阅读
  6. ChatGPT:突破写作限制,开启论文写作新境界

    2024-03-17 10:22:02       18 阅读
  7. axios入门

    2024-03-17 10:22:02       17 阅读
  8. LeetCode题练习与总结:解数独

    2024-03-17 10:22:02       19 阅读
  9. Axios 中的文件上传(Upload File)方法

    2024-03-17 10:22:02       18 阅读
  10. linux系统kubernetes的yaml文件

    2024-03-17 10:22:02       21 阅读