Vue中引用公共方法和公共组件

Vue中引用公共方法

1.在src新建commonFunction文件夹,再新建common.js
Vue中引用公共方法和公共组件_第1张图片
其中common.js中方法的声明和导出如下
Vue中引用公共方法和公共组件_第2张图片
2.在main.js中引用common.js,再使用Vue.prototype添加实例属性
Vue中引用公共方法和公共组件_第3张图片

3.应用

@click=“common.方法名”
Vue中引用公共方法和公共组件_第4张图片

Vue中引用公共组件

1.在src目录下的components文件夹下新建公共组件

Vue中引用公共方法和公共组件_第5张图片

2.在main.js中引用,这里需要引用并初始化组件三个步骤

import AreaSelect from '@/components/AreaSelect'
Vue.use(AreaSelect) // 引用自定义组件
Vue.component('area-select', AreaSelect) // 初始化组件
new Vue({
  el: '#app',
  components: {
    AreaSelect
  },
})

Vue中引用公共方法和公共组件_第6张图片

3.直接用 area-select 即可使用

你可能感兴趣的:(vue项目实战经验)