1、vue中内部函数的几种定义格式

  1. <script>
  2. new Vue({
  3. //指定作用于那一个div
  4. el:'#app',
  5. //属性
  6. data:{
  7. },
  8. methods: {
  9. a(e,c){
  10. alert("aaa");
  11. },
  12. a(){
  13. alert("aaa");
  14. },
  15. a:function(e,c) {
  16. alert("aaa");
  17. }
  18. }
  19. });
  20. </script>

2、js中定义外部函数

主要通过在xx.js文件中定义export函数暴露给xx.vue文件进行import后调用,如下图的xx.js:

  1. import request from '@/utils/request'
  2. export function fetchList(query) {
  3. return request({
  4. url: '/vue-element-admin/article/list',
  5. method: 'get',
  6. params: query
  7. })
  8. }
  9. export function fetchArticle(id) {
  10. return request({
  11. url: '/vue-element-admin/article/detail',
  12. method: 'get',
  13. params: { id }
  14. })
  15. }
  16. export function createArticle(data) {
  17. return request({
  18. url: '/vue-element-admin/article/create',
  19. method: 'post',
  20. data
  21. })
  22. }

xx.vue文件进行调用

  1. <template>
  2. <div class="app-container">
  3. </div>
  4. </template>
  5. <script>
  6. import { fetchList, fetchArticle, createArticle } from '@/api/article'
  7. export default {
  8. name: 'ComplexTable',
  9. components: { Pagination },
  10. directives: { waves },
  11. data() {
  12. return {
  13. listLoading: true,
  14. temp: {
  15. id: undefined,
  16. importance: 1,
  17. remark: '',
  18. timestamp: new Date(),
  19. title: '',
  20. type: '',
  21. status: 'published'
  22. },
  23. }
  24. },
  25. created() {
  26. this.getList()
  27. },
  28. methods: {
  29. getList() {
  30. this.listLoading = true
  31. fetchList(this.listQuery).then(response => {
  32. this.list = response.data.items
  33. this.total = response.data.total
  34. // Just to simulate the time of the request
  35. setTimeout(() => {
  36. this.listLoading = false
  37. }, 1.5 * 1000)
  38. })
  39. },
  40. createData() {
  41. this.$refs['dataForm'].validate((valid) => {
  42. if (valid) {
  43. this.temp.id = parseInt(Math.random() * 100) + 1024 // mock a id
  44. this.temp.author = 'vue-element-admin'
  45. createArticle(this.temp).then(() => {
  46. this.list.unshift(this.temp)
  47. this.dialogFormVisible = false
  48. this.$notify({
  49. title: 'Success',
  50. message: 'Created Successfully',
  51. type: 'success',
  52. duration: 2000
  53. })
  54. })
  55. }
  56. })
  57. },
  58. }
  59. }
  60. </script>