vue组件暴露和.js文件暴露接口操作

2022-07-30,,,,

1、将同一类型的组件放在一个文件夹下

2、在此文件夹下创建一个index.js

3、在index.js中导入组件,并把他们暴露出去

1、写法一

 import studentcourse1 from './studentcourse.vue'
 import studentinfo1 from './studentinfo.vue'
 
 export var studentcourse=studentcourse1
 export var studentinfo=studentinfo1

2、写法二

 export var studentcourse=()=>import('./studentcourse.vue')
 export var studentinfo=()=>import('./studentinfo.vue')
 //export var studentinfo=()=>{
 return import('./studentinfo.vue')
 }

4、最后在.vue文件中使用组件

此处你只需引入index.js所在文件夹就行啦

 import {studentcourse,studentinfo} from './components/stucom'
 export default{
 components:{
  'studentcourse':studentcourse,
  'studentinfo':studentinfo
  }
 }

1、将.js放在同一个文件夹下

2、同样一定要有一个index.js文件

3、 index.js文件内容如下

  import auth from './auth.js'
  import error from './error-log.js'
 export default{
 auth,
 error
 }

4、在main.js中

import utils from './utils'

vue.use(utils.auth,{params})

补充知识:vue项目中将方法名暴露给app端调用

只需要将methods中的方法赋值到window对象即可

created() {
 window.getparams = this.getparams
},
 
methods: {
 getparams(params) {
  this.id = params.id
  // ...
 },
}

也可以赋值给window对象中的某个对象

以上这篇vue组件暴露和.js文件暴露接口操作就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持。

《vue组件暴露和.js文件暴露接口操作.doc》

下载本文的Word格式文档,以方便收藏与打印。