初始化
请确保你已经安装了 Node.js,然后全局安装 weex-toolkit
。
npm install weex-toolkit -g
|
这条命令会向你命令行环境中注册一个 weex
命令。你可以用 weex create
命令来创建一个空的模板项目:
weex create awesome-app
|
命令执行完以后,在当前目录的 awesome-app
文件夹里就有了一个空的 Weex + Vue.js 项目。
开发
下一步就是进入刚刚创建的文件夹,并且安装依赖,然后执行 npm start
:
cd awesome-app
|
自己创建
我们可以根据 webpack 和 npm 自己来创建简单的 weex 的项目。
-
新建一个 package.json 文件,并且编写内容如下:
{ "name": "weex", "version": "1.0.0", "description": "", "main": "index.js", "scripts": { "build": "webpack", "dev": "webpack --watch", "serve": "serve -p 8080", "test": "echo "Error: no test specified" && exit 1" }, "keywords": [], "author": "", "license": "ISC", "devDependencies": { "babel-core": "^6.18.0", "babel-loader": "^6.2.5", "babel-plugin-transform-runtime": "^6.15.0", "babel-preset-es2015": "^6.14.0", "babel-runtime": "^6.11.6", "serve": "^1.4.0", "webpack": "^1.13.1", "weex-html5": "^0.4.1", "weex-loader": "^0.3.4", "weex-components": "^0.2.0" } }
-
新建一个 webpack.config.js 文件,并且编写内容如下:
require('webpack') require('weex-loader') var path = require('path') module.exports = { entry: { main: path.join(__dirname, 'src', 'hello.we?entry=true') }, output: { path: 'dist', filename: '[name].js' }, module: { loaders: [ { test: /.we(?[^?]+)?$/, loaders: ['weex-loader'] } ] } }
-
创建 src 目录,并且在 src 目录中创建 hello.we 文件,内容如下所示:
<template> <div> <div class="title"> <text class="h1">{{ title }}</text> <text class="button" onclick="click">点击</text> </div> </div> </template> <style> .h1{ top:200px ; color: black; text-align: center; } .button{ top:300px; text-align: center; background-color: #4e7d95; font-size: 30px; padding: 25; margin: 15; color: white; border-radius: 30; } </style> <script> module.exports = { data: { title: "hello world" }, methods: { click: function(){ this.title = "hello weew" } } } </script>
-
创建 index.html 文件,作为 web 端的入口,内容如下:
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>Weex HTML5</title> <meta name="apple-mobile-web-app-capable" content="yes" /> <meta name="apple-mobile-web-app-status-bar-style" content="black" /> <meta name="apple-touch-fullscreen" content="yes" /> <meta name="format-detection" content="telephone=no, email=no" /> <style> html, body, #weex { 100%; height: 100%; } </style> <script src="../node_modules/weex-html5/dist/weex.js"></script> </head> <body> <div id="weex"></div> <script src="dist/main.js"></script> <script> (function () { function getUrlParam (key) { var reg = new RegExp('[?|&]' + key + '=([^&]+)') var match = location.search.match(reg) return match && match[1] } var loader = getUrlParam('loader') || 'xhr' var page = getUrlParam('page') || 'dist/main.js' window.weex.init({ appId: location.href, loader: loader, source: page, rootId: 'weex' }) })(); </script> </body> </html>