1、vue中内部函数的几种定义格式
<script>
new Vue({
//指定作用于那一个div
el:'#app',
//属性
data:{
},
methods: {
a(e,c){
alert("aaa");
},
a(){
alert("aaa");
},
a:function(e,c) {
alert("aaa");
}
}
});
</script>
2、js中定义外部函数
主要通过在xx.js文件中定义export函数暴露给xx.vue文件进行import后调用,如下图的xx.js:
import request from '@/utils/request'
export function fetchList(query) {
return request({
url: '/vue-element-admin/article/list',
method: 'get',
params: query
})
}
export function fetchArticle(id) {
return request({
url: '/vue-element-admin/article/detail',
method: 'get',
params: { id }
})
}
export function createArticle(data) {
return request({
url: '/vue-element-admin/article/create',
method: 'post',
data
})
}
xx.vue文件进行调用
<template>
<div class="app-container">
</div>
</template>
<script>
import { fetchList, fetchArticle, createArticle } from '@/api/article'
export default {
name: 'ComplexTable',
components: { Pagination },
directives: { waves },
data() {
return {
listLoading: true,
temp: {
id: undefined,
importance: 1,
remark: '',
timestamp: new Date(),
title: '',
type: '',
status: 'published'
},
}
},
created() {
this.getList()
},
methods: {
getList() {
this.listLoading = true
fetchList(this.listQuery).then(response => {
this.list = response.data.items
this.total = response.data.total
// Just to simulate the time of the request
setTimeout(() => {
this.listLoading = false
}, 1.5 * 1000)
})
},
createData() {
this.$refs['dataForm'].validate((valid) => {
if (valid) {
this.temp.id = parseInt(Math.random() * 100) + 1024 // mock a id
this.temp.author = 'vue-element-admin'
createArticle(this.temp).then(() => {
this.list.unshift(this.temp)
this.dialogFormVisible = false
this.$notify({
title: 'Success',
message: 'Created Successfully',
type: 'success',
duration: 2000
})
})
}
})
},
}
}
</script>