Vue 全局注册组件,供全局使用,单 vue 文件形式
有时候有的组件需要全局使用,比较你定义的一个 弹窗
组件。
如果全局注册全局都可使用呢?
一、新建一个盛放注册组件的 js 文件
在 src
目录下新建个 js
文件,如 components.js
(文件名不重要)。把所有需要全局使用的组件都从这个 js
文件中列举出来:
import popMsg from "./components/popMsg";
import popMsg from "./components/alertMsg";
export default (Vue) => {
Vue.component('popMsg',popMsg);
Vue.component('alertMsg',alertMsg);
}
二、使用这个 js 文件
然后在 src/main.js
中导入这个文件,并用 Vue.use()
使用这个文件的内容即可。这样全局都能使用这里面注册的组件了。
// import components
import components from "./components.js";
Vue.use(components);
使用中这样
<template>
<div class="body-regist" :style="'min-height:' + heightBg + 'px'">
<pop-msg title="test" type="default" callback=""/>
<div class="logo">
<img src="img/logo.svg" alt="Diary Logo">
</div>
</div>
</template>