温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

Vue中怎么动态创建注册component

发布时间:2021-06-17 16:24:16 来源:亿速云 阅读:108 作者:Leah 栏目:web开发

这期内容当中小编将会给大家带来有关Vue中怎么动态创建注册component,文章内容丰富且以专业的角度为大家分析和叙述,阅读完这篇文章希望大家可以有所收获。

常规组件声明与注册

// 定义一个名为 button-counter 全局注册的组件
Vue.component("button-counter", {
 template: '<button v-on:click="count++">You clicked me {{ count }} times.</button>',
 data() {
  return {
   count: 0
  }
 }
});

new Vue({
 template: `
  <div id="app">
   <h2>App Component</h2>
   <button-counter></button-counter>
  </div>
 `
}).$mount("#app");

在上面的代码中我们声明了一个叫做 button-counter 的组件。如果在常规情况下使用的话,只需要在页面上写对应的 <button-counter></button-counter> 标签就够了。

全局创建注册组件可以实现动态创建,但是我们必须在 template 声明使用该组件,而且如果把所有组件都全局注册这并不是一个好办法。

在官方文档中我们可以看到,我们可以通过 Vue.component('component-name') 的方式来注册一个组件。

而组件实例又有 $mount 这样一个方法,官方对于 $mount 的解释如下:

vm.$mount( [elementOrSelector] )
Arguments:
{Element | string} [elementOrSelector]
{boolean} [hydrating]
Returns: vm - the instance itself
Usage:
If a Vue instance didn't receive the el option at instantiation, it will be in “unmounted” state, without an associated DOM element. vm.$mount() can be used to manually start the mounting of an unmounted Vue instance.
If elementOrSelector argument is not provided, the template will be rendered as an off-document element, and you will have to use native DOM API to insert it into the document yourself.
The method returns the instance itself so you can chain other instance methods after it.

那我们是否可以通过这种方式来达到我们的需求呢?

还不够!

为什么???

因为 Vue.component 返回的结果是一个 function!它返回的并不是 组件实例,而是一个构造函数。

那到这里其实我们就清楚了。 对于 Vue.component 声明的组件,我们先通过 Vue.component 获取它的构造函数,再 new 出一个组件实例,最后 通过 $mount 挂载到 html 上。

// 定义一个名为 button-counter 全局注册的组件
Vue.component("button-counter", {
 template: '<button v-on:click="count++">You clicked me {{ count }} times.</button>',
 data() {
  return {
   count: 0
  }
 }
});

new Vue({
 template: `
  <div id="app">
   <h2>App Component</h2>
   <button @click="insert">click to insert button-counter comonent</button>
   <div id="insert-container"></div>
  </div>
 `,
 methods: {
  insert() {
   const ButtonCounter = Vue.component("button-counter"); // 只能查找到全局注册到组件
   const instance = new ButtonCounter();
   instance.$mount("#insert-container");
  }
 }
}).$mount("#app");

上述代码中,Vue.component 先获取到组件的构造函数,然后构造实例,通过实例的一些方法来处理数据和挂载节点。

但是我们发现 Vue.component 只负责全局注册或查找。

如果想要针对局部注册的组件使用动态创建并添加我们需要使用 Vue.extend 基础Vue构造器创建"子类"达到目的。

其实 Vue.component 方法传入的选项是一个对象时,Vue自动调用 Vue.extend。

完整代码示例:

const ButtonCounterComponent = {
 template: '<button v-on:click="count++">You clicked me {{ count }} times.</button>',
 data() {
  return {
   count: 0
  }
 }
};

new Vue({
 template: `
  <div id="app">
   <h2>App Component</h2>
   <button @click="insert">click to insert button-counter comonent</button>
   <div id="insert-container"></div>
  </div>
 `,
 methods: {
  insert() {
   const ButtonCounter = Vue.extend(ButtonCounterComponent);
   const instance = new ButtonCounter();
   instance.$mount("#insert-container");
  }
 }
}).$mount("#app");

单文件应用

在实际使用场景里,大部分都是用脚手架构建到项目,使用 *.vue 这种单文件方式注册组件。

<template></template>
<script>
export default {
 data() {
  return {
   msg: "hello"
  }
 },
 created() {},
 mounted() {},
 destroy() {}
};
</script>
<style scoped></style>

import *.vue 返回的就是模版中 script 中 export 部分。

上述就是小编为大家分享的Vue中怎么动态创建注册component了,如果刚好有类似的疑惑,不妨参照上述分析进行理解。如果想知道更多相关知识,欢迎关注亿速云行业资讯频道。

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI