≡
  • 网络编程
  • 数据库
  • CMS技巧
  • 软件编程
  • PHP笔记
  • JavaScript
  • MySQL
位置:首页 > 网络编程 > vue.js

vue组件jsx语法的具体使用

人气:424 时间:2019-04-11

这篇文章主要为大家详细介绍了vue组件jsx语法的具体使用,具有一定的参考价值,可以用来参考一下。

感兴趣的小伙伴,下面一起跟随四海网的小编两巴掌来看看吧!

如果使用render函数来写比较复杂的vue组件,对于可读性和可维护性都很不友好,而使用jsx就会让我们回到更接近于模板的语法。babel转译器会将jsx转译为render函数渲染。

 

配置

 

需要用到babel插件

安装

代码如下:


npm install\
 babel-plugin-syntax-jsx\
 babel-plugin-transform-vue-jsx\
 babel-helper-vue-jsx-merge-props\
 babel-preset-env\
 --save-dev

.babelrc配置

在plugins中添加transform-vue-jsx

代码如下:


{
 "presets": ["env"],
 "plugins": ["transform-vue-jsx"]
}

 

基础示例

 

转义前

代码如下:


<div id="foo">{this.text}</div>

转译后

代码如下:


h('div', {
 attrs: {
  id: 'foo'
 }
}, [this.text])

Note:h函数为vue实例的$createElement方法,必须存在于jsx的作用域中,在渲染函数中必须以第一个参数传入,如:

代码如下:


render (h) { // <-- h 函数必须在作用域内
 return <div id="foo">bar</div>
}

 

自动注入h函数

 

从3.4.0开始,在用ES2015语法声明的方法和getter访问器中(使用function关键字或箭头函数除外),babel会自动注入h(const h = this.$createElement)函数,所以可以省略(h)参数。

代码如下:


Vue.component('jsx-example', {
 render () { // h 会自动注入
  return <div id="foo">bar</div>
 },
 myMethod: function () { // h 不会注入
  return <div id="foo">bar</div>
 },
 someOtherMethod: () => { // h 不会注入
  return <div id="foo">bar</div>
 }
})

@Component
class App extends Vue {
 get computed () { // h 会自动注入
  return <div id="foo">bar</div>
 }
}

 

 Vue JSX 和 React JSX对比

 

首先, Vue2.0 的vnode 格式与react不同,createElement函数的第二个参数是一个数据对象,接受一个嵌套的对象,每一个嵌套对象都会有对应的模块处理。

Vue2.0 render语法

代码如下:


render (h) {
 return h('div', {
  // 组件props
  props: {
   msg: 'hi'
  },
  // 原生HTML属性
  attrs: {
   id: 'foo'
  },
  // DOM props
  domProps: {
   innerHTML: 'bar'
  },
  // 事件是嵌套在`on`下面的,所以将不支持修饰符,如:`v-on:keyup.enter`,只能在代码中手动判断keyCode
  on: {
   click: this.clickHandler
  },
  // For components only. Allows you to listen to
  // native events, rather than events emitted from
  // the component using vm.$emit.
  nativeOn: {
   click: this.nativeClickHandler
  },
  // class is a special module, same API as `v-bind:class`
  class: {
   foo: true,
   bar: false
  },
  // style is also same as `v-bind:style`
  style: {
   color: 'red',
   fontSize: '14px'
  },
  // other special top-level properties
  key: 'key',
  ref: 'ref',
  // assign the `ref` is used on elements/components with v-for
  refInFor: true,
  slot: 'slot'
 })
}

对应的Vue2.0 JSX语法

代码如下:


render (h) {
 return (
  <div
   // normal attributes or component props.
   id="foo"
   // DOM properties are prefixed with `domProps`
   domPropsInnerHTML="bar"
   // event listeners are prefixed with `on` or `nativeOn`
   onClick={this.clickHandler}
   nativeOnClick={this.nativeClickHandler}
   // other special top-level properties
   class={{ foo: true, bar: false }}
   style={{ color: 'red', fontSize: '14px' }}
   key="key"
   ref="ref"
   // assign the `ref` is used on elements/components with v-for
   refInFor
   slot="slot">
  </div>
 )
}

 

JSX展开运算符

 

支持JSX展开,插件会智能的合并数据属性,如:

代码如下:


const data = {
 class: ['b', 'c']
}
const vnode = <div class="a" {...data}/>

合并后的数据为:

代码如下:


{ class: ['a', 'b', 'c'] }

 

Vue 指令

 

JSX对大多数的Vue内建指令都不支持,唯一的例外是v-show,该指令可以使用v-show={value}的语法。大多数指令都可以用编程方式实现,比如v-if就是一个三元表达式,v-for就是一个array.map()等。

如果是自定义指令,可以使用v-name={value}语法,但是改语法不支持指令的参数arguments和修饰器modifier。有以下两个解决方法:

将所有内容以一个对象传入,如:v-name={{ value, modifier: true }}

使用原生的vnode指令数据格式,如:

代码如下:


const directives = [
 { name: 'my-dir', value: 123, modifiers: { abc: true } }
]

return <div {...{ directives }}/>

原文地址(https://github.com/vuejs/babel-plugin-transform-vue-jsx#usage)

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持四海网。

本文来自:http://www.q1010.com/184/5508-0.html

注:关于vue组件jsx语法的具体使用的内容就先介绍到这里,更多相关文章的可以留意四海网的其他信息。

关键词:vue.js

您可能感兴趣的文章

  • vue input输入框模糊查询的示例代码
  • Vue中父子组件通讯之todolist组件功能开发
  • vue2.0 实现导航守卫(路由守卫)
  • 基于Vue的延迟加载插件vue-view-lazy
  • vue单页开发父子组件传值思路分析
  • 分析使用vue-admin-template的优化历程
  • vue.js使用3DES加密的方法示例
  • 分析vue的diff算法原理
  • Vue页面骨架屏的实现方法
  • 安装vue-cli的简易过程
上一篇:基于Vue自定义指令实现按钮级权限控制思路分析
下一篇:基于Vue的延迟加载插件vue-view-lazy
热门文章
  • Vue 报错TypeError: this.$set is not a function 的解决方法
  • vue实现动态添加数据滚动条自动滚动到底部的示例代码
  • vue项目设置scrollTop不起作用(总结)
  • vue项目中使用vue-i18n报错的解决方法
  • iview实现select tree树形下拉框的示例代码
  • 分析关于element级联选择器数据回显问题
  • vue项目打包后打开页面空白解决办法
  • 解决element ui select下拉框不回显数据问题的解决
  • element-ui table span-method(行合并)的实现代码
  • element-ui 设置菜单栏展开的方法
  • 最新文章
    • 理解vue ssr原理并自己搭建简单的ssr框架
    • vue favicon设置以及动态修改favicon的方法
    • vue-router启用history模式下的开发及非根目录部署方法
    • 从零开始在NPM上发布一个Vue组件的方法步骤
    • Element input树型下拉框的实现代码
    • Vue 报错TypeError: this.$set is not a function 的解决方法
    • Vue.js组件高级特性实例分析
    • 浅谈VueJS SSR 后端绘制内存泄漏的相关解决经验
    • 分析Vue.js自定义tipOnce指令用法实例
    • 浅谈vuex actions和mutation的异曲同工

四海网收集整理一些常用的php代码,JS代码,数据库mysql等技术文章。