vue3+ts:render极简demo -- 引入element ui el-input组件

一、示例一:

父组件:

<template>
  <div class="home">
    <render-input
      :msg="title"
      @updateMsg="updateMsg"
    >
    </render-input>
  </div>
</template>

<script lang="ts">
import { Options, Vue } from 'vue-class-component';
import renderInput from './components/render-components2.vue';

@Options({
  components: {
    renderInput,
  },
})
export default class Home extends Vue {
  public title = 'title'
  public updateMsg(val:any){
    console.log('23um', val)
    this.title = val
  }
}
</script>

子组件:

<script lang="ts">
import { ElInput } from "element-plus";
import { defineComponent, h, watch } from "vue";

export default defineComponent({
  props: {
    msg: {
      type: String,
      default: '',
    },
  },
  emits: ['updateMsg'],
  setup(props, { emit, attrs }: any) {
    return (props:any) => {
      watch(
        props.msg,
        (val) => {
          console.log('18', val)
        },
        { immediate: true }
      )
      return h(
        "div", // 标签,只写标签名,不写<>
        { // 属性
          ref: 'test-render-ref',
          class: 'test-render-class',
          // innerHTML: 'test-render-html' // 这里打开后子元素里边innerHTML内容不能展示
        },
        [ // 子元素
          h(ElInput,{
            ...ElInput.$el,
            ...ElInput.$attrs,
            class: 'test-render-class-child',
            placeholder: '请输入内容',
            style: {
              color: 'FF0000'
            },
            modelValue: props.msg,
            "onUpdate:modelValue": ($event:any) => {
              console.log('25', $event)
              console.log('27', ElInput)
              emit('updateMsg', $event)
            },
          })
        ]
      );
    };
  }
})
</script>

这里通过emit调用父组件的方法,改变值,然后再次单向数据流传递到子组件,子组件input数据更新。

这种方法在父组件定义数据,在子组件实现逻辑,再返回数据到父组件,个人认为是适合封装复杂组件的。

二、示例二:

<script lang="ts">
import { ElInput } from "element-plus";
import { defineComponent, h, ref } from "vue";

export default defineComponent({
  props: {
    msg: {
      type: String,
      default: '',
    },
  },
  emits: ["updateMsg"],
  setup(props, { emit, attrs }: any) {
    return () => {
      let inputValue = ref("123456");
      return h(
        "div", // 标签,只写标签名,不写<>
        {
          // 属性
          ref: "test-render-ref",
          class: "test-render-class",
          // innerHTML: 'test-render-html' // 这里打开后子元素里边innerHTML内容不能展示
        },
        [
          // 子元素
          h(
            ElInput,
            {
              ...ElInput.$el,
              ...ElInput.$attrs,
              class: "test-render-class-child",
              placeholder: "请输入内容",
              style: {
                color: "FF0000",
              },
              modelValue: inputValue.value,
              "onUpdate:modelValue": ($event: any) => {
                // 实现了数据更新
                return (inputValue.value = $event.target.value);
              }
            }
          )
        ]
      )
    }
  }
})
</script>

在"onUpdate:modelValue" 里实现修改后的值

三、示例三:

父组件:

<template>
  <div class="home">
    <render-input
      :msg="title"
      @updateMsg="updateMsg"
    >
    </render-input>
  </div>
</template>

<script lang="ts">
import { Options, Vue } from 'vue-class-component';
import renderInput from './components/renderDiv.vue';

@Options({
  components: {
    renderInput,
  },
})
export default class Home extends Vue {
  public title = 'title'
  public updateMsg(val:any){
    console.log('23um', val)
    this.title = val
  }
}
</script>

子组件:

<script lang="ts">
import { ElInput } from "element-plus";
import { defineComponent, h, ref } from "vue";

export default defineComponent({
  props: {
    msg: {
      type: String,
      default: '',
    },
  },
  emits: ["updateMsg"],
  setup(props, { emit, attrs }: any) {
    return () => {
      let inputValue = ref("123456");
      console.log('16inputValue', inputValue.value)
      if(props.msg){
        inputValue.value = props.msg
      }
      return h(
        "div", // 标签,只写标签名,不写<>
        {
          // 属性
          ref: "test-render-ref",
          class: "test-render-class",
          // innerHTML: 'test-render-html' // 这里打开后子元素里边innerHTML内容不能展示
        },
        [
          // 子元素
          h(
            ElInput,
            {
              ...ElInput.$el,
              ...ElInput.$attrs,
              class: "test-render-class-child",
              placeholder: "请输入内容",
              style: {
                color: "FF0000",
              },
              modelValue: inputValue.value,
              "onUpdate:modelValue": ($event: any) => {
                // 实现了数据更新
                return (inputValue.value = $event.target.value);
              },
            },
          ),
          h("span", {
            class: "test-render-class-button",
            style: {
              color: "#FF0000",
              border: "1px solid #ccc",
              width: "60px",
              cursor: "pointer",
            },
            innerHTML: "提交",
            onClick: () => {
              emit('updateMsg', inputValue.value += 's-i')
            },
          }),
        ]
      );
    };
  },
});
</script>

四、欢迎交流指正,关注我一起学习。

  • 3
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
对于搭建 Vue 3 + TypeScript + Vite + Element Plus 的环境,并进行 Vite 的优化,可以按照以下步骤进行操作: 1. 确保已安装 Node.js 首先确保你的电脑已经安装了 Node.js,可以在命令行中输入 `node -v` 来检查是否安装成功。 2. 创建新项目 打开命令行,进入到你想要创建项目的目录中,执行以下命令创建新的 Vue 3 项目: ``` npm init @vitejs/app my-vue-app --template vue-ts ``` 3. 安装 Element Plus 进入到项目目录,执行以下命令安装 Element Plus: ``` npm install element-plus --save ``` 4. 配置 Element Plus 在 `src/main.ts` 中引入 Element Plus 的样式和组件: ```typescript import { createApp } from 'vue'; import App from './App.vue'; import elementPlus from 'element-plus'; import 'element-plus/lib/theme-chalk/index.css'; const app = createApp(App); app.use(elementPlus); app.mount('#app'); ``` 5. Vite 的优化 Vite 是一个基于 ES Modules 的构建工具,它的特点是快速的冷启动和开发时的实时模块热更新。但在生产环境中,我们需要对 Vite 进行一些优化。 - 使用生产模式构建 在 `package.json` 文件中的 `scripts` 中添加以下命令: ```json "scripts": { "build": "vite build" } ``` 然后执行 `npm run build` 命令进行构建。 - 优化打包体积 在 `vite.config.ts` 中可以配置一些优化选项,例如: ```typescript import { defineConfig } from 'vite' import vue from '@vitejs/plugin-vue' export default defineConfig({ plugins: [vue()], build: { target: 'es2015', terserOptions: { compress: { drop_console: true, drop_debugger: true } } } }) ``` 上述配置中,`target` 指定了构建的目标浏览器环境为 ES2015,`terserOptions` 中的配置会在压缩代码时移除控制台输出和调试器。 - 配置静态资源路径 默认情况下,Vite 会将静态资源放在根目录下,可以通过配置 `base` 选项来指定静态资源的路径。在 `vite.config.ts` 中添加以下配置: ```typescript export default defineConfig({ base: '/your-base-path/' }) ``` - 优化生产环境加载速度 在生产环境中,可以通过配置 `build.assetsInlineLimit` 来控制是否将小于指定大小的文件转换为 base64 格式内联到 HTML 中。在 `vite.config.ts` 中添加以下配置: ```typescript export default defineConfig({ build: { assetsInlineLimit: 4096 } }) ``` 这些只是一些常用的优化选项,你可以根据具体需求进行配置。 希望以上步骤对你有所帮助!如果你有更多问题,可以继续提问。
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值