基於mpvue搭建小程序項目框架

簡介:css

  mpvue框架對於從沒有接觸太小程序又要嘗試小程序開發的人員來講,無疑是目前最好的選擇。mpvue從底層支持 Vue.js 語法和構建工具體系,同時再結合相關UI組件庫,即可以高效的實現小程序開發vue

前言:node

  本文講述如何搭建完整的小程序項目框架,由於是第一次使用,有不完善的地方請大佬指正。webpack

  搭建內容包括:git

  一、使用scss語法:依賴插件sass-loader 、node-sassgithub

  二、像vue同樣使用路由:依賴插件 mpvue-entry 和 mpvue-router-patchweb

  三、使用ZanUI:有贊團隊的小程序版UI組件庫(GitHubvue-router

  四、使用vuex進行狀態管理vuex

  五、使用flyio進行數據交互:GitHub地址npm

項目結構:

講解:

  1、使用scss語法

  一、安裝依賴

  cnpm install node-sass sass-loader --save-dev

  由於一些不知名的緣由致使node-sass常常安裝失敗,因此採用cnpm方式安裝最好

  二、在.vue文件中的style節點加上lang=」scss」,這樣就能夠愉快地使用sass進行開發了,無需再webpack.base.config.js中配置loader,webpack會自動識別.scss文件以及.vue中的scss代碼。

  2、像vue同樣使用路由

  在使用mpvue提供的命令 vue init mpvue/mpvue-quickstart my-project 建立項目後,會發現每一個頁面都要配置main.js 文件,不只繁瑣並且顯得多餘,因此咱們是否能夠改形成像vue同樣使用路由的方式呢,答案是能夠的,須要用到mpvue-entry 和 mpvue-router-patch插件(集中式頁面配置,自動生成各頁面的入口文件,優化目錄結構,支持新增頁面熱更新)和

  mpvue-entry: 集中式頁面配置,自動生成各頁面的入口文件,優化目錄結構,支持新增頁面熱更新

  mpvue-router-patch: 在 mpvue 中使用 vue-router 兼容的路由寫法

  一、安裝依賴

  cnpm install mpvue-entry --save-dev

  cnpm install mpvue-router-patch --save

  二、項目src文件夾下建立router文件夾和router.js文件

  

  

  三、項目引入src下的main.js文件 

import Vue from 'vue'
import MpvueRouterPatch from 'mpvue-router-patch'

Vue.use(MpvueRouterPatch)

  注:main.js的 export default {} 不能爲空,否則編譯時不生成app.json文件

 

  四、修改webpack.base.conf.js配置文件

const MpvueEntry = require('mpvue-entry')

module.exports = {
    entry:MpvueEntry.getEntry('./src/router/router.js'),
    .......
}

  

  五、配置router.js 文件 

// 首個路由爲首頁
module.exports = [{
    path: 'pages/index',
    name: 'Index',
    config: {
        navigationBarTitleText: '文章詳情',
    //引入UI組件,後面會講到 usingComponents:{
"zan-button": "../dist/btn/index" } } }, { path: 'pages/list/list', name: 'List', config: { navigationBarTitleText: 'list詳情' } }]

 

  3、使用小程序UI組件

  一、將UI組件庫克隆到本地

  

  二、將上圖中的dist目錄拷貝到mpvue項目的輸出目錄中

  

  三、在router.js文件中引入UI組件 

module.exports = [{
    path: 'pages/index',
    name: 'Index',
    config: {
        navigationBarTitleText: '文章詳情',
        //引入UI組件
        usingComponents:{
            //組件名和引用路徑
            "zan-button": "../dist/btn/index"
        }
    }
}]

  

  四、頁面中使用UI組件 

<template>
    <div class="index">
        <zan-button type="primary" size="small">確認付款</zan-button>
    </div>
</template>

 

  五、

  小程序使用自定義組件:https://developers.weixin.qq.com/miniprogram/dev/framework/custom-component/

  ZanUI組件庫使用講解:https://github.com/youzan/zanui-weapp/blob/dev/README.md

 

  4、使用vuex進行狀態管理

  一、安裝

    cnpm install vuex --save

  二、引入(main.js文件)

    impotr store from './vuex/index'

    Vue.prototrype.$store = store//掛在到vue原型上 

 

  5、使用flyio進行數據交互

  一、安裝

    cnpm install flyio --save

  二、引入(main.js文件)

    const Fly = require("flyio/dist/npm/wx")//引入

    const fly = new Fly

    Vue.prototrype.$fly = fly//掛在到vue原型上

  三、使用

    

        add(){
                //在add方法中執行接口請求
                this.$fly.get('http://******/user?id=133')
                    .then(function (res) {
                            //請求成功
                           console.log('res',res);
                    })
                    .catch(function (err) {
                        //請求失敗
                        console.log('err',err);
                    });
            }    
相關文章
相關標籤/搜索