Vue 内置 component 组件

发布于 2022-11-06 18:36:04 字数 4974 浏览 175 评论 0

官方的说明

渲染一个 元组件 为动态组件。依 is 的值,来决定哪个组件被渲染。

<!-- 动态组件由 vm 实例的属性值 `componentId` 控制 -->
<component :is="componentId"></component>

具体可以官网文档中的

场景

这里通过一个业务场景来阐述 vue 内置 component 组件的应用。如图所示,这里展示经典注册页面,注册分为邮箱注册和手机注册,弹窗顶部有标签可以切换注册类型,中间是注册表单信息,邮箱注册和手机注册有着不一样的表单内容,底部是注册按钮以及其他操作。

经过分析手机注册界面与邮箱注册除了中间的表单内容不一致之外,其他的界面内容是一样的。

实际项目代码设计中,为了保证复用性和可维护性,是会有一些可行的方案。这里我们采用 vue 内置的 component 组件来实现这一点。

核心代码实现

顶部 tab 切换的时候,type 值发生改变,对应的表单的组件也发生了变化

<template>
  <div>
	<a href="javascript:;" @click.prevent="handleCloseBtnClick"></a>
	<div>
	  <h3>新用户注册</h3>
	  <div>
		<span :class="{active: type === 'mobileForm'}" @click="type = mobileForm">手机注册</span>
		<span :class="{active: type === 'emailForm'}" @click="type = emailForm">邮箱注册</span>
	  </div>
	</div>
	<component :is="type" ref="form">
	  <button @click="handleRegisterBtnClick">注册</button>
	  <div ><span ><span>注册视为同意</span><a> 《法律条款和隐私说明》</a></span></div>
	  <div><span>已有账号<a href="javascript:;" @click.prevent="handleLoginBtnClick">直接登入>></a></span></div>
	</component>
  </div>
</template>
<script>
 export default {
 	methods: {
		handleRegisterBtnClick () {
		 	this.$refs.form.validateData().then(() => {
				this.$refs.form.getFormData()
			})
		 }
	}
 }
</script>

mixins 混合

用 Vue 内置 component 组件情况下,一般实际被渲染的组件具有一定的共性,比如相同的属性,相同的方法或者相同的初始化销毁过程。比如目前这个场景中邮箱表单和手机表单都具有校验方法(validateData)和获取表单数据方法(getFormData)。

这种情况下可以使用 vue 提供的混合的功能。进一步抽离

mixins.js

export default {
  methods: {
    validateData() {
      return Promise.resolve()
    },
    getFormData() {
      return {}
    }
  }
}

email-form.vue

<script>
import minx from './mixins'
export default {
  mixins: [mixins],
  methods: {
    getFormData() {
      return { email: 'example@example.com' }
    }
  }
}
</script>

如果有自定义的需求,可以重写 mixins 中的方法。

表格的应用

在管理后台项目中,表格经常会被用到。我们希望表格的 td 是文本、进度条、checkbox 等等,且希望通过传一个 json 配置就可以渲染出。使用 vue 内置的 component 组件可以起到很赞的作用。

比如这样的一个 table 使用方式

<template>
  <vue-table ref="table" :columns="columns" :datum="datum"></vue-table>
</template>
<script>
export default {
    data () {
      return {
        columns: [
          { title: 'ID', width: '30', dataKey: 'id' },
          { title: '进度组件', dataKey: 'progress', render: { type: 'progress2', max: 100, precision: 2 } }
        ],
        datum: [{ id: '1', name: '进度0', progress: 10 }]
      }
    }
  }
</script>

table 中使用 component 的实现

<td v-for="column of columns">
	<component :is="`${TYPE_PRE}${columns.render.type}`"  :row-data="rowData" :params="columns.render"></component>
</td>

表单的应用

在管理后台项目中,表单也经常需要用到,我们也同样希望表单的某一项是文本框,下拉框,时间选择框,富文本等等等等,且希望通过传一个 json 配置就可以渲染出。 vue 内置的 component 组件可以依然可以实现这样一个美好的愿景。

比如这样的一个 form 使用方式

<template>
   <c-form :cells="cells" ref="form">
      <button class="button is-primary" :class="{ 'is-disabled': isSubmitBtnDisabled }" @click.prevent="submit">提交</button>
   </c-form>
</template>
<script>
   export default {
    computed: {
      cells () {
        return [
          {
            field: 'name',
            label: '名称',
            type: 'textfield',
            attrs: { placeholder: '名称' },
            validate: {  required: { message: '请输入名称'} }
          },
          {
            field: 'enable',
            label: '启用标志',
            type: 'dropdown',
            extra: {options: [{ label: '启用', value: 1 }, { label: '禁用', value: 2 }] }
          }
        ]
      }
    }
  }
</script>

form 中使用 component 的实现

<form>
  <c-form-cell v-for="cell of cellList" :key="cell.field" :field="cell.field">
    <component
	  :is="`${TYPE_PRE}${cell.type}`"
	  :field="cell.field"
	  :attrs="cell.attrs"
	  :extra="cell.extra"
	  :validate="cell.validate"
	  :cells="cell.cells">
    </component>
  </c-form-cell>
</form>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据

关于作者

黒涩兲箜

暂无简介

文章
评论
27 人气
更多

推荐作者

櫻之舞

文章 0 评论 0

弥枳

文章 0 评论 0

m2429

文章 0 评论 0

野却迷人

文章 0 评论 0

我怀念的。

文章 0 评论 0

    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文