资讯

精准传达 • 有效沟通

从品牌网站建设到网络营销策划,从策略到执行的一站式服务

Vue中怎么动态创建注册component-创新互联

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

创新互联于2013年成立,是专业互联网技术服务公司,拥有项目成都网站制作、网站设计网站策划,项目实施与项目整合能力。我们以让每一个梦想脱颖而出为使命,1280元红河哈尼做网站,已为上家服务,为红河哈尼各地企业和个人服务,联系电话:18982081108

常规组件声明与注册

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

new Vue({
 template: `
  
   

App Component

      
 ` }).$mount("#app");

在上面的代码中我们声明了一个叫做 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: 'You clicked me {{ count }} times.',
 data() {
  return {
   count: 0
  }
 }
});

new Vue({
 template: `
  
   

App Component

   click to insert button-counter comonent    
  
 `,  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: 'You clicked me {{ count }} times.',
 data() {
  return {
   count: 0
  }
 }
};

new Vue({
 template: `
  
   

App Component

   click to insert button-counter comonent         `,  methods: {   insert() {    const ButtonCounter = Vue.extend(ButtonCounterComponent);    const instance = new ButtonCounter();    instance.$mount("#insert-container");   }  } }).$mount("#app");

单文件应用

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



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

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


网站名称:Vue中怎么动态创建注册component-创新互联
链接地址:http://cdkjz.cn/article/heeeh.html
返回首页 了解更多建站资讯
多年建站经验

多一份参考,总有益处

联系快上网,免费获得专属《策划方案》及报价

咨询相关问题或预约面谈,可以通过以下方式与我们联系

业务热线:400-028-6601 / 大客户专线   成都:13518219792   座机:028-86922220