Encapsulation and calling of functions in vue

1. Use export default

let fn ={
	set(){
		console.log('set')
	},
	get(){
		console.log('get')
	},
	post(){
		console.log('post')
	}
}

export default fn

Reference in vue

import fn from '@/xxx.js'  //xxx.js的路径,这里是当前文件夹

 Just use it directly in vue

created(){
	fn.get()
}

2. Export by export

Note: To export by export, you need to add { } when importing, but export default does not need

export function subtraction (a,b){
    return a + b
}

Reference in vue

import { subtraction } from '@/components/basejs/subtraction.js'

You can also use it directly in vue

created(){
	this.num = subtraction(2,11)
}

Note: Both export and export default can be used to export constants, functions, files, modules, etc. You can import them in other files or modules by import+(constant | function | file | module) name, so that you can import them For use, in a file or module, there can be multiple export and import, and only one export default


 

 

Guess you like

Origin blog.csdn.net/weixin_48951113/article/details/129795465