詳解Weex基于Vue2.0開發(fā)模板搭建
前言
最近有一些人反饋說在面試過程中常常被問到weex相關(guān)的知識(shí),也側(cè)面反映的weex的發(fā)展還是很可觀的,可是目前weex的開發(fā)者大多數(shù)是中小型公司或者個(gè)人,大公司屈指可數(shù),揪其原因可能是基于weex的開發(fā)正確的姿勢(shì)大家并沒有找到,而且市面上的好多輪子還是.we后綴的,眾所周知,weex和vue一直在努力的進(jìn)行生態(tài)互通,而且weex實(shí)現(xiàn)web標(biāo)準(zhǔn)化是早晚的問題,今天和大家分享一下weex基于vue2.0的開發(fā)框架模板~
工作原理
先簡(jiǎn)單熟悉一下weex的工作原理,這里引用一下weex官網(wǎng)上的一直圖片,詳細(xì)信息見官網(wǎng)

開發(fā)環(huán)境搭建
weex 開發(fā)環(huán)境搭建
關(guān)于weex開發(fā)環(huán)境搭建問題見官方文檔
android 、iOS 開發(fā)環(huán)境
關(guān)于native開發(fā)環(huán)境搭建問題見官方文檔
框架說明
基于vue2.0搭建
像前面說的那樣weex和vue一直在努力的進(jìn)行生態(tài)互通,而且weex實(shí)現(xiàn)web標(biāo)準(zhǔn)化是早晚的問題,所以也建議開發(fā)者不要在用.we做后綴來(lái)開發(fā)了
多頁(yè)模式(拋棄vue-router)
單頁(yè)形態(tài)對(duì)于原生可能體驗(yàn)不夠好,目前在 native App 里單頁(yè)模式不太合適
集成三端(android、ios、h5平臺(tái))
關(guān)于android、ios、h5平臺(tái)的集成與打包問題,在項(xiàng)目中都以解決~
集成eslint代碼檢查
代碼檢查是必要的操作,為了能夠擁有vue開發(fā)的體驗(yàn),將eslint集成進(jìn)來(lái)~
注:
由于weexpack暫不支持vue問題,打包相關(guān)后續(xù)會(huì)集成進(jìn)來(lái)~
框架介紹
package.json依賴
"dependencies": {
"vue": "^2.1.8",
"vue-router": "^2.1.1",
"vuex": "^2.1.1",
"vuex-router-sync": "^4.0.1",
"weex-vue-render": "^0.1.4"
},
"devDependencies": {
"babel-core": "^6.20.0",
"babel-eslint": "^7.1.1",
"babel-loader": "^6.2.9",
"babel-preset-es2015": "^6.18.0",
"css-loader": "^0.26.1",
"eslint": "^3.15.0",
"eslint-config-standard": "^6.2.1",
"eslint-loader": "^1.6.1",
"eslint-plugin-html": "^2.0.1",
"eslint-plugin-promise": "^3.4.2",
"eslint-plugin-standard": "^2.0.1",
"postcss-cssnext": "^2.9.0",
"serve": "^1.4.0",
"vue-loader": "^10.0.2",
"vue-template-compiler": "^2.1.8",
"webpack": "^1.14.0",
"weex-devtool": "^0.2.64",
"weex-loader": "^0.4.1",
"weex-vue-loader": "^0.2.5"
}
打包配置
1、 遍歷.vue文件后綴,生成相應(yīng)的entry.js文件
function getEntryFileContent (entryPath, vueFilePath) {
const relativePath = path.relative(path.join(entryPath, '../'), vueFilePath);
return 'var App = require(\'' + relativePath + '\')\n'
+ 'App.el = \'#root\'\n'
+ 'new Vue(App)\n'
}
function walk (dir) {
dir = dir || '.'
let directory = path.join(__dirname, './src', dir)
let entryDirectory = path.join(__dirname, './src/entry');
fs.readdirSync(directory)
.forEach(file => {
let fullpath = path.join(directory, file)
let stat = fs.statSync(fullpath)
let extname = path.extname(fullpath)
if (stat.isFile() && extname === '.vue') {
let entryFile = path.join(entryDirectory, dir, path.basename(file, extname) + '.js')
fs.outputFileSync(entryFile, getEntryFileContent(entryFile, fullpath))
let name = path.join(dir, path.basename(file, extname))
entry[name] = entryFile + '?entry=true'
} else if (stat.isDirectory()) {
let subdir = path.join(dir, file)
walk(subdir)
}
})
}
walk()
2、通過weex-loader打包生成native jsbundle
3、 通過weex-vue-loader打包生成web jsbundle
function getBaseConfig () {
return {
entry: entry,
output: {
path: 'dist'
},
resolve: {
extensions: ['', '.js', '.vue'],
fallback: [path.join(__dirname, './node_modules')],
alias: {
'assets': path.resolve(__dirname, './src/assets/'),
'components': path.resolve(__dirname, './src/components/'),
'constants': path.resolve(__dirname, './src/constants/'),
'api': path.resolve(__dirname, './src/api/'),
'router': path.resolve(__dirname, './src/router/'),
'store': path.resolve(__dirname, './src/store/'),
'views': path.resolve(__dirname, './src/views/'),
'config': path.resolve(__dirname, './config'),
'utils': path.resolve(__dirname, './src/utils/')
}
},
module: {
preLoaders: [
{
test: /\.vue$/,
loader: 'eslint',
exclude: /node_modules/
},
{
test: /\.js$/,
loader: 'eslint',
exclude: /node_modules/
}
],
loaders: [
{
test: /\.js$/,
loader: 'babel',
exclude: /node_modules/
}, {
test: /\.vue(\?[^?]+)?$/,
loaders: []
}
]
},
vue: {
postcss: [cssnext({
features: {
autoprefixer: false
}
})]
},
plugins: [bannerPlugin]
}
}
const webConfig = getBaseConfig()
webConfig.output.filename = 'web/[name].js'
webConfig.module.loaders[1].loaders.push('vue')
const weexConfig = getBaseConfig()
weexConfig.output.filename = 'weex/[name].js'
weexConfig.module.loaders[1].loaders.push('weex')
項(xiàng)目結(jié)構(gòu)
weex-frame
├── android (android項(xiàng)目)
│
├── ios (ios項(xiàng)目代碼)
│
├── src (weex模塊)
│ ├── api (api模塊)
│ ├── components(組件模塊)
│ ├── constants(常量配置)
│ ├── utils (工具模塊)
│ └── views(視圖模塊)
│
└── dist (build輸出模塊)
├── weex (native使用jsbundle)
└── web(web使用jsbundle)
項(xiàng)目啟動(dòng)
- git clone git@github.com:osmartian/weex-frame.git
- cd weex-frame
- npm install
- 執(zhí)行 ./start
android 啟動(dòng)
- 打開andorid studio
- File -> New -> Import Project -> weex-frame/android -> 啟動(dòng)
iOS 啟動(dòng)
- cd ios
- pod install (未安裝pod,請(qǐng)先安裝)
- open WeexFrame.xcworkspace
h5 啟動(dòng)方式
打開 http://localhost:12580/weex.html
項(xiàng)目示例


android 端示例



iOS 端示例



結(jié)語(yǔ)
能看的出來(lái)上方的三端示例表現(xiàn)還是很一致的,本篇博文也是想給大家提供一個(gè)輪子,也歡迎大家多多提意見,共同促進(jìn)weex生態(tài)成熟~
框架項(xiàng)目地址:weex-frame_jb51.rar
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
vue如何統(tǒng)一樣式(reset.css與border.css)
這篇文章主要介紹了vue如何統(tǒng)一樣式(reset.css與border.css),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-05-05
vue關(guān)于接口請(qǐng)求數(shù)據(jù)過大導(dǎo)致瀏覽器崩潰的問題
這篇文章主要介紹了vue關(guān)于接口請(qǐng)求數(shù)據(jù)過大導(dǎo)致瀏覽器崩潰的問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-08-08
vue 折疊展示多行文本組件的實(shí)現(xiàn)代碼
這篇文章主要介紹了vue 折疊展示多行文本組件,自動(dòng)根據(jù)傳入的expand判斷是否需要折疊,非常完美,文章通過實(shí)例代碼給大家介紹的非常詳細(xì),需要的朋友可以參考下2021-10-10
vue動(dòng)態(tài)合并單元格并添加小計(jì)合計(jì)功能示例
這篇文章主要給大家介紹了關(guān)于vue動(dòng)態(tài)合并單元格并添加小計(jì)合計(jì)功能的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-11-11
VSCode創(chuàng)建Vue項(xiàng)目的完整步驟教程
Vue是一個(gè)輕量級(jí)、漸進(jìn)式的Javascript框架,如果想要要開發(fā)全新的Vue項(xiàng)目,建議項(xiàng)目構(gòu)建工具vue-cli,下面這篇文章主要給大家介紹了關(guān)于VSCode創(chuàng)建Vue項(xiàng)目的完整步驟,需要的朋友可以參考下2022-06-06
Vue中實(shí)現(xiàn)表單地區(qū)選擇與級(jí)聯(lián)聯(lián)動(dòng)示例詳解
這篇文章主要為大家介紹了Vue中實(shí)現(xiàn)表單地區(qū)選擇與級(jí)聯(lián)聯(lián)動(dòng)示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-09-09

