网创优客建站品牌官网
为成都网站建设公司企业提供高品质网站建设
热线:028-86922220
成都专业网站建设公司

定制建站费用3500元

符合中小企业对网站设计、功能常规化式的企业展示型网站建设

成都品牌网站建设

品牌网站建设费用6000元

本套餐主要针对企业品牌型网站、中高端设计、前端互动体验...

成都商城网站建设

商城网站建设费用8000元

商城网站建设因基本功能的需求不同费用上面也有很大的差别...

成都微信网站建设

手机微信网站建站3000元

手机微信网站开发、微信官网、微信商城网站...

建站知识

当前位置:首页 > 建站知识

Vue中怎么动态创建注册component

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

鹿邑ssl适用于网站、小程序/APP、API接口等需要进行数据传输应用场景,ssl证书未来市场广阔!成为创新互联公司的ssl证书销售渠道,可以享受市场价格4-6折优惠!如果有意向欢迎电话联系或者加微信:13518219792(备注:SSL证书合作)期待与您的合作!

常规组件声明与注册

// 定义一个名为 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
标题URL:http://bjjierui.cn/article/gscgoc.html

其他资讯