优雅的使用VUE----「拯救繁乱的template」

很多人在写组件的时候,会依赖脚手架中的标签,其实template也存在一定的缺陷,例如:

  • template里存在一值多判断
  • 过多使用template会使代码冗余,杂乱
VUE给我们提供了一个render函数,我们可以通过这个函数巧妙的解决template造成的问题
// 父组件引入
<template>
  <div>
    <h1>I am Home</h1>
    <!-- 按钮根据value显示不同类型的button -->
    <Button type='success' text='button1' @myClick='...'></Button>
  </div>
</template>
import Button from './button.vue'

// 公共button组价冗余写法
<template>
  <div>
    <h1>I am Home</h1>
    <!-- 假设按钮有多种类型,通过value来显示不同类型 -->
    <div v-if='value === 1'>
      <button>button1</button>
    </div>
    <div v-else-if='value === 2'>
      <button>button2</button>
    </div>
    <div v-else>
      <button>button3</button>
    </div>
  </div>
</template>
<script>
export default {
  name: 'Home',
  data(){
    return{
        value:1
    }
  },
  methods:{
  }
}
</script>
// 上面这种写法,当出现多种类型的button,就会显得杂乱无章,
//当然,很多人会选择去封装一个button组件,那么这个组件的封装,
//又是一个技巧点,利用VUE的render函数,减少不必要的template,因此我们可以这样写
<script>
export default {
    props:{
        type:{
            type:String,
            default:'normal'
        },
        text:{
            type:String,
            default:'button'
        }
    },
    render(h){
        /*
            h 类似于 createElement, 接受2个参数
            1 - 元素
            2 - 选项
         */
        return h('button',{
            // 相当于 v-bind:class
            class:{
                btn:true,
                'btn-success':this.type === 'success',
                'btn-danger':this.type === 'danger',
                'btn-warning':this.type === 'warning',
                'btn-normal':this.type === 'normal',
            },
            domProps:{
                innerText: this.text || '默认'
            },
            on:{
                click:this.handleClick
            }
        })
    },
    methods:{
        handleClick(){
            this.$emit('myClick')
        }
    }
}
</script>

优化之后的代码,避免了一值多判断的缺点,减少冗余,更加灵活, 这种方式较适合业务简单,使用次数多的组件

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
`vue-template-compiler` 是一个将Vue单文件组件(.vue文件)中的模板编译为渲染函数的工具。如果你需要在项目中使用渲染函数,或者需要在构建时预编译模板,那么 `vue-template-compiler` 就是必须要使用的。 下面是使用 `vue-template-compiler` 的步骤: 1. 安装 `vue-template-compiler` ``` npm install vue-template-compiler --save-dev ``` 2. 使用 `vue-template-compiler` 编译模板 ```javascript const compiler = require('vue-template-compiler') const template = ` <div> <h1>{{ message }}</h1> </div> ` const compiled = compiler.compile(template) console.log(compiled.render) // 输出编译后的渲染函数 ``` 在上面的代码中,我们首先引入了 `vue-template-compiler`,然后定义了一个模板 `template`,最后使用 `compiler.compile` 方法将模板编译成渲染函数。 3. 在 Vue使用编译后的渲染函数 ```javascript const renderer = require('vue-server-renderer').createRenderer() const app = new Vue({ data: { message: 'Hello, World!' }, render: compiled.render }) renderer.renderToString(app, (err, html) => { if (err) throw err console.log(html) }) ``` 在上面的代码中,我们使用了 `vue-server-renderer` 的 `createRenderer` 方法创建了一个渲染器 `renderer`,然后定义了一个 Vue 实例 `app`,将编译后的渲染函数赋值给 `render` 选项。最后,我们使用 `renderer.renderToString` 方法将 Vue 实例渲染成 HTML 字符串。 以上就是使用 `vue-template-compiler` 的基本步骤,希望能对你有所帮助。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值