Vue.use()和Vue.component()

当很多页面用到同一个组件,又不想每次都在局部注册时,可以在main.js 中全局注册

Vue.component()一次只能注册一个组件

javascript 复制代码
import CcInput from '@/components/cc-input.vue'
Vue.component(CcInput);

Vue.use()一次可以注册多个组件

对于自定义的组件,如果不想在每个页面都引入一次,可以使用Vue.use()方法进行注册。

注册插件的时候,需要在调用 new Vue() 启动应用之前完成

Vue.use会自动阻止多次注册相同插件,只会注册一次

假设有两个自定义的组件:cc-input, cc-button

myUI/components中定义组件

在main.js中:

javascript 复制代码
import MyUI from '/myUi';
Vue.use(MyUi);

myUi/index.js:

javascript 复制代码
import CcInput from './components/cc-input';
import CcButton from './components/cc-button';

const components = [
	CcInput,
	CcButton
]

const MyUI = {
	install(Vue) {
		components.forEach(component => {
			// 注册组件
			Vue.component(component.name, component);
		});
	}
}

export default MyUI;
相关推荐
历程里程碑1 分钟前
普通数组-----除了自身以外数组的乘积
大数据·javascript·python·算法·elasticsearch·搜索引擎·flask
摸鱼的春哥2 分钟前
春哥的Agent通关秘籍07:5分钟实现文件归类助手【实战】
前端·javascript·后端
念念不忘 必有回响5 分钟前
viepress:vue组件展示和源码功能
前端·javascript·vue.js
Amumu121389 分钟前
Vue3 Composition API(一)
开发语言·javascript·ecmascript
C澒10 分钟前
多场景多角色前端架构方案:基于页面协议化与模块标准化的通用能力沉淀
前端·架构·系统架构·前端框架
崔庆才丨静觅12 分钟前
稳定好用的 ADSL 拨号代理,就这家了!
前端
江湖有缘14 分钟前
Docker部署music-tag-web音乐标签编辑器
前端·docker·编辑器
hzb6666615 分钟前
unictf2026
开发语言·javascript·安全·web安全·php
恋猫de小郭1 小时前
Flutter Zero 是什么?它的出现有什么意义?为什么你需要了解下?
android·前端·flutter
崔庆才丨静觅8 小时前
hCaptcha 验证码图像识别 API 对接教程
前端