vue3注册全局组件

注册单个全局组件

一.在main.ts中引入注册为全局组件

  1. 在main.ts 引入我们的组件跟随在createApp(App) 后面 切记不能放到mount 后面这是一个链式调用用
  2. 其次调用 component 第一个参数组件名称 第二个参数组件实例
import { createApp } from 'vue'
import App from './App.vue'
import leftVue from './components/left.vue'
createApp(App).component('leftVue',leftVue).mount('#app') 
//.component('leftVue',leftVue)就是注册全局组件 (‘自定义键名’,组件名)

二.页面使用

不需要引入直接使用

  <leftVue></leftVue>

批量注册组件

在日常项目开发中,我们会用到很多组件,一个一个去全局组注册很麻烦,这里就不得不提起批量注册了,具体实现如下

一.在components下创建index.ts/js

import leftVue from './left.vue'
import rightVue from './right.vue' //引入组件

import type { App, Component } from 'vue'
const components:any= {leftVue,rightVue} 

export default {
  install(app: App) {
    Object.keys(components).forEach((key: string) => { 
      app.component(key, components[key])//遍历挂载组件
    })
  },
}

二.在main.ts/js引入挂载

import globalComponent from '@/components/index'
app.use(globalComponent)

三.在页面可以直接使用,不需要引入

  <leftVue></leftVue>
  <rightVue></rightVue>

局部注册

一.引入

在需要用到的页面直接引入

import leftVue from "@/components/left.vue";

二.使用

  <leftVue></leftVue>

相关推荐

  1. vue3注册全局组件

    2024-03-10 20:32:01       20 阅读
  2. Vue 批量注册全局组件

    2024-03-10 20:32:01       34 阅读
  3. vue3组件注册

    2024-03-10 20:32:01       41 阅读
  4. vue3组件注册

    2024-03-10 20:32:01       17 阅读
  5. 学习Vue 02-25 注册全局组件

    2024-03-10 20:32:01       33 阅读
  6. Vue2组件注册全局组件和局部组件

    2024-03-10 20:32:01       29 阅读
  7. Vue.component v2v3注册(局部与全局)组件使用详解

    2024-03-10 20:32:01       12 阅读
  8. vue3基础: 组件注册

    2024-03-10 20:32:01       37 阅读

最近更新

  1. TCP协议是安全的吗?

    2024-03-10 20:32:01       18 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2024-03-10 20:32:01       19 阅读
  3. 【Python教程】压缩PDF文件大小

    2024-03-10 20:32:01       18 阅读
  4. 通过文章id递归查询所有评论(xml)

    2024-03-10 20:32:01       20 阅读

热门阅读

  1. Docker Register 搭建私有镜像仓库

    2024-03-10 20:32:01       22 阅读
  2. Linux 系统上卸载 Docker

    2024-03-10 20:32:01       22 阅读
  3. 在 Docker 环境下安装 OpenWrt

    2024-03-10 20:32:01       25 阅读
  4. Docker修改网段

    2024-03-10 20:32:01       22 阅读
  5. Kotlin 中的数据类

    2024-03-10 20:32:01       22 阅读
  6. lvs集群

    lvs集群

    2024-03-10 20:32:01      22 阅读
  7. sklearn随机森林实现(备忘版)

    2024-03-10 20:32:01       20 阅读
  8. Docker

    2024-03-10 20:32:01       22 阅读
  9. Flink命令行提交时参数的传递

    2024-03-10 20:32:01       18 阅读
  10. Redis的HyperLogLog原理介绍

    2024-03-10 20:32:01       20 阅读
  11. 使用Rust开发小型搜索引擎

    2024-03-10 20:32:01       21 阅读
  12. 【深度学习】COCO API源码解读

    2024-03-10 20:32:01       20 阅读
  13. SpringSecurity与Shiro的区别

    2024-03-10 20:32:01       19 阅读
  14. 决策树基本原理&sklearn实现

    2024-03-10 20:32:01       25 阅读