前往小程序,Get更优阅读体验!
立即前往
发布
社区首页 >专栏 >(二十一)动态组件渲染不同HTML标签

(二十一)动态组件渲染不同HTML标签

作者头像
老怪兽
发布2023-02-22 16:39:28
发布2023-02-22 16:39:28
86400
代码可运行
举报
运行总次数:0
代码可运行

使用动态组件渲染不容的HTML标签

一、渲染不同的标签

  1. 子组件
代码语言:javascript
代码运行次数:0
复制
<template>
    <!-- is后面可以绑定动态的值,is里面就是渲染的不同的标签名字,比如 div、h1、i标签等 -->
  <Component :is="heading"><slot></slot></Component>
</template>

<script>
export default {
  props: ["level"],
  computed: {
    // 也可以通过计算属性去拼接标签
    heading() {
      return `h${this.level}`;
    },
  },
};
</script>
</style>

2. 父组件

代码语言:javascript
代码运行次数:0
复制
<template>
  <main>
    <div>
      <!-- 动态 HTML 元素  -->
      <TextHeading level="h1">一级标题</TextHeading>
      <!-- <TextHeading level="div">我是div</TextHeading>
      <TextHeading level="i">我是i</TextHeading> -->
    </div>
  </main>
</template>

<script>
import TextHeading from "./components/TextHeading.vue";
export default {
  components: {
    TextHeading,
  },
};
</script>

二渲染不同组件

  1. 子组件一
代码语言:javascript
代码运行次数:0
复制
<template>
  <form @submit.prevent>
    <label>手机号:<input type="number" /></label>
    <label
      >验证码:<input type="number" /><button class="sendSMSCodeBtn">
        发送验证码
      </button></label
    >
  </form>
</template>
<script>
export default {};
</script>
<style scoped>
.sendSMSCodeBtn {
  margin-left: 24px;
}
</style>

2. 子组件二

代码语言:javascript
代码运行次数:0
复制
<template>
  <form @submit.prevent>
    <label>昵称:<input type="text" /></label>
    <label>生日:<input type="date" /></label>
    <label>地址:<input type="text" /></label>
  </form>
</template>
<script>
export default {};
</script>
<style scoped>
::-webkit-calendar-picker-indicator {
  filter: invert(1);
}
</style>

3. 父组件使用

代码语言:javascript
代码运行次数:0
复制
<template>
  <main>
    <div>
      <!-- 展示不同的组件 -->
      <component :is='contentData'></component>
      <!-- 动态切换上一步,下一步;并且赋予新的组件的值 -->
      <button v-if="contentData === 'RegisterForm'" @click="contentData = 'ProfileForm'">下一步</button>
      <button v-else @click="contentData = 'RegisterForm'">上一步</button>
    </div>
  </main>
</template>

<script>
import RegisterForm from './components/RegisterForm.vue'
import ProfileForm from './components/ProfileForm.vue'
export default {
  components: {
    RegisterForm,
    ProfileForm
  },

  data() {
    return {
      contentData: "RegisterForm"
    }
  }
};
</script>

总结:写在最后

总结

通过vue 给我们提供的内置组件标签 <component :is="组件名字"> 通过他的is属性来切换不同的标签,或者是不同的组件

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2022年11月7日,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 使用动态组件渲染不容的HTML标签
    • 一、渲染不同的标签
    • 二渲染不同组件
    • 总结:写在最后
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档