vue 全局组件注册_如何注册vue3全局组件

news/2024/7/10 0:35:29 标签: vue, js, javascript, react, npm

vue 全局组件注册

With the new versions of Vue3 out now, it’s useful to start learning how the new updates will change the way we write code. One example is the changes in the way we write our index.js file (the file that handles creating our Vue app).

随着新版本Vue3的发布,开始学习新更新将如何改变我们编写代码的方式非常有用。 一个例子是我们编写index.js文件(用于处理创建Vue应用程序的文件)的方式的变化。

Today, we’ll take a look at how to register Vue3 Global Components that can be used across our entire Vue app. It’s a little different from how we declared them in Vue2, but it’s just as simple.

今天,我们将研究如何注册可在整个Vue应用程序中使用的Vue3全局组件。 与我们在Vue2中声明它们的方式稍有不同,但它很简单。

For this tutorial, I am working with the beta release of Vue3 that can be found via the vue-next Github repository.

对于本教程,我正在使用Vue3的beta版本 ,可以通过vue-next Github存储库找到它。

Alright. Let’s just get straight to it.

好的。 让我们直接说吧。

什么是Vue全球组件? (What are Vue Global Components?)

First off, we have to understand what a Vue3 global component is and why we might want to use one.

首先,我们必须了解Vue3全局组件是什么以及为什么要使用它。

Normally, when we want to include a component inside our Vue instance, we register it locally. That normally looks something like this.

通常,当我们想在Vue实例中包含一个组件时,我们会在本地注册它。 通常看起来像这样。

<script>
import PopupWindow from '../components/PopupWindow.vue';
export default {
components: {
PopupWindow
}
}
</script>

However, let’s say that there is a component that we know we’re going to be using many times across our Vue project. It can get messy to register this component locally inside every file — especially if our project gets refactored or something.

但是,假设有一个组件,我们知道我们将在整个Vue项目中多次使用它。 在每个文件中本地注册此组件可能会很麻烦-尤其是在我们重构了项目或其他内容的情况下。

In this case, it could be useful to globally register the component — making it accessible in all subcomponents of our main root Vue instance. In other words, globally registering a component means that we don’t have to import it in each file.

在这种情况下,全局注册该组件可能会很有用-使它可以在我们的主根Vue实例的所有子组件中访问。 换句话说,全局注册一个组件意味着我们不必将其导入每个文件中。

Let’s take a look at how this is done in Vue2 and how we can do it now in Vue3.

让我们看一下如何在Vue2中完成此操作以及现在如何在Vue3中进行操作 。

全局组件如何在Vue2中工作 (How global components work in Vue2)

In Vue2, wherever we create our Vue instance, we just have to call a Vue.component method to register a global component.

在Vue2中,无论我们在哪里创建Vue实例,我们都只需要调用Vue.component方法来注册全局组件。

This method takes two arguments:

此方法有两个参数:

1 — the name of our global component

1-我们的全球组成部分的名称

2 — our component itself

2-我们的组件本身

Here’s a quick example of what that might look like.

这是一个大概的例子。

import Vue from 'vue'
import PopupWindow from './components/PopupWindow'
import App from './App.vue'
Vue.component('PopupWindow', PopupWindow) // global registration - can be used anywhere
new Vue({
render: h => h(App)
}).$mount('#app')

Now this ‘PopupWindow’ component can be used in all children of this Vue instance! Easy as that.

现在,此“ PopupWindow”组件可以在此Vue实例的所有子级中使用! 那样简单。

现在,在Vue3中呢? (Now, what about in Vue3?)

In Vue3, the code varies slightly just because of creating our Vue instance works a little differently (using createApp), but it is just as simple to understand.

在Vue3中,由于创建我们的Vue实例(使用createApp)的工作方式略有不同,因此代码略有不同,但是它很容易理解。

Instead of declaring global components from our Vue object, we first have to create our app. Then, we can run the same .component method as we would before.

首先必须创建应用程序,而不是从Vue对象声明全局组件。 然后,我们可以运行与以前相同的.component方法。

import { createApp } from 'vue'
import PopupWindow from './components/PopupWindow'
import App from "./App.vue"
const app = createApp(App)
app.component('PopupWindow', PopupWindow) // global registration - can be used anywhere
app.mount('#app')

As you can see, it’s very similar, but the slight differences in the way our Vue instance is initialized make us change up our syntax a little bit.

如您所见,它非常相似,但是Vue实例的初始化方式略有不同,使我们对语法进行了一些更改。

就是这样! (And that’s it!)

There you have it! We can now use our COMPONENT component in any Vue component that comes from this root instance. It’s a great way to keep our code DRY.

你有它! 现在,我们可以在此根实例提供的任何Vue组件中使用COMPONENT组件。 这是使我们的代码保持干燥的好方法。

It’s important to carefully consider when we want to use a global component vs. a local component. If we just make everything a global component by default, it means that even when we’re not using a component, it would still be included in our build — increasing page load times.

重要的是要仔细考虑何时要使用全局组件还是本地组件。 如果我们默认将所有内容都设为全局组件,则意味着即使不使用组件,该组件仍将包含在我们的构建中-增加页面加载时间。

Global components can be a very powerful tool when used properly, and with the new changes in Vue3, it is still very easy to use these types of components in your Vue project.

正确使用全局组件可以成为非常强大的工具,并且随着Vue3的新变化,在Vue项目中使用这些类型的组件仍然非常容易。

If you have any questions, leave them in the comments down below.

如有任何疑问,请在下面的注释中保留。

Happy coding :)

快乐的编码:)

翻译自: https://medium.com/swlh/how-to-register-a-vue3-global-component-5a6e2fe4dd31

vue 全局组件注册


http://www.niftyadmin.cn/n/1468276.html

相关文章

pid算法控制电机转速_永磁同步电机弱磁控制表贴电机弱磁算法

前言表贴式永磁同步电机凭借结构简单、控制相对容易、转矩精度高和动态性好的优点&#xff0c;在中低速工业领域获得了极其广泛的应用&#xff0c;比如主轴伺服、工业机器人等行业。本期我们就来聊一聊表贴式永磁同步电机如何进行弱磁控制。基于电机参数的弱磁控制算法由于永磁…

chrome 前端插件_如何将前端项目转换为chrome扩展

chrome 前端插件The Chrome extension can greatly extend the functions of Chrome and has a huge market and commercial value. As a front-end programmer, can you write a Chrome extension?Chrome扩展程序可以极大地扩展Chrome的功能&#xff0c;并具有巨大的市场和商…

树莓派配置php环境变量,[操作系统]树莓派配置apache php

[操作系统]树莓派配置apache php0 2016-07-09 13:00:07Apache MySql Php&#xff0e;1、安装ApacheApache可以用下面的命令来安装 sudo apt-get install apache2Apache默认路径是/var/www/其配置文件路径为&#xff1a; /etc/apache2/可以通过&#xff1a;sudo nano /etc/apa…

python shell打不开_Python Shell没有运行

我在跑步Python.org网站版本2.7 64位Windows Vista 64位使用Scrapy。当我通过命令Shell运行它时&#xff0c;我有一些代码正在工作(除了命令Shell无法识别非Unicode字符的一些问题之外)&#xff0c;但是当我尝试通过Python空闲运行脚本时&#xff0c;我得到以下错误消息&#x…

如何在托梁中处理自己

Not long ago I introduced a new framework called Joist. As pointed out in that introduction, one of the more unique features of Joist is the concept of “handlers” and because of this I thought it would be worth digging into what handlers are and what you…

matlab ncut谱聚类,NCUT 归一化分割、谱聚类之代码调试问题

1 相比于c&#xff0c;matlab的效率较低&#xff0c;为了解决这个问题&#xff0c;大家在matlab中调用c&#xff0c;也就是说matlab调用的一些函数&#xff0c;本身是由c编写完成的&#xff0c;执行的时候也是在c编译器中执行。实现这个功能的媒介是后缀为mex的文件&#xff0c…

使用babel转换js为_如何仅使用babel即可快速转换javascript babel js简介

使用babel转换js为JavaScript&#xff1a;工具 (JavaScript: Tooling) You might have heard of Webpack, Rollup, or perhaps Parcel. These are some of the widely popular (web) bundling tools. What is bundling you ask? I think you are a little late to the party. …

join left semi_Hive 中的 LEFT SEMI JOIN 与 JOIN ON

hive 的 join 类型有好几种&#xff0c;其实都是把 MR 中的几种方式都封装实现了&#xff0c;其中 join on、left semi join 算是里边具有代表性&#xff0c;且使用频率较高的 join 方式。1、联系他们都是 hive join 方式的一种&#xff0c;join on 属于 common join(shuffle j…