vue中的jsx

一、配置文件package.json

{
  "name": "vuetest",
  "version": "1.0.0",
  "description": "",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1",
    "server": "webpack-dev-server --open",
    "build": "webpack-dev-server"
  },
  "author": "camille",
  "license": "ISC",
  "devDependencies": {
    "babel-core": "^6.26.0",
    "babel-helper-define-map": "^6.26.0",
    "babel-helper-optimise-call-expression": "^6.24.1",
    "babel-helper-vue-jsx-merge-props": "^2.0.2",
    "babel-loader": "^7.1.2",
    "babel-plugin-syntax-jsx": "^6.18.0",
    "babel-plugin-transform-vue-jsx": "^3.5.0",
    "babel-preset-es2015": "^6.24.1"
  }
}

devDependencies这一项最初没有,是在后续步骤中,安装插件时,通过命令自动添加的。

二、新建文件
1、index.html

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>vuejsx</title>
</head>
<body>
  <div id="J_vapp_jsx"></div>
  <script type="text/javascript" src="https://unpkg.com/vue"></script>
  <script type="text/javascript" src="bundle.js"></script>
</body>
</html>

2、webpack.config.js

module.exports = {
  entry: './myvu.js',
  output: {
    path: __dirname,
    filename: 'bundle.js'
  },
  devtool: "inline-source-map",
  module: {
    rules: [
      {
        test: /\.js$/,
        exclude: /node_modules/,
        use: [ 'babel-loader']
      }
    ]
  }
}

3、.babelrc

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

4、myvu.js

var vm = new Vue({
  el: '#J_vapp_jsx',
  data: {
    msg: 'Hello JSX'
  },
  methods: {
    hello () {
      alert('Hello Vue 2.0')
    }
  },
  render (h) {
    const data = {
      attrs: {
        id: 'J_div'
      },
      on: {
        click: () => {
          console.log('点击状态:点击啦!')
        }
      },
      props: {
        innerHTML: 'hihihi'
      },
      hook: {
        insert: () => {
          console.log('dom插入状态:已经插入。')
        }
      }
    }
    // return <div title="一个div" {...data}/>
    return (
      <div id="J_jsx_box">
        <button
          class={{ cls1: true, cls2: true }}
          style={{fontSize: '15px'}}
          on-click={this.hello}
          {...data}>
          {this.msg}
        </button>
      </div>
    )
  }
})

三、安装相关插件

cnpm i -D xxx
xxx包含babel-plugin-syntax-jsx,babel-plugin-transform-vue-jsx,babel-helper-vue-jsx-merge-props,babel-core,babel-loader,babel-preset-es2015。
为什么会在package.json中看到babel-helper-define-map和babel-helper-optimise-call-expression,这是我在调试的时候,自己加上去的,也要安装,然后就不报错了。github上的vue作者写的文档有些问题,直接运行会报错。

四、运行项目
npm run server

转载于:https://www.cnblogs.com/camille666/p/vue_jsx_webpack.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值