1. 程式人生 > >一步步帶你做vue後臺管理框架(二)——上手使用

一步步帶你做vue後臺管理框架(二)——上手使用

系列教程《一步步帶你做vue後臺管理框架》第二課

線上體驗地址:立即體驗

  閒扯再多不會用也沒白搭,這節課我來帶大家直接上手框架,體驗到簡單方便之後你就會愛上這個框架欲罷不能的。

  首先跟所有的vue專案一樣,兩種方式,一種去專案上下載程式碼包到本地,或者使用

git clone https://github.com/herozhou/vue-framework-wz.git

   無論哪種方式,只要我們能夠拿到整個程式碼包就行。在我們程式碼包的根目錄下開啟命令列,執行如下命令:

npm install --registry=https://registry.npm.taobao.org

  建議大家最好使用這樣的方式,cnpm總是會出現各種奇怪的bug。

  等到依賴安裝完成之後我們執行

npm run dev

  然後在瀏覽器中輸入http://localhost:9001,就可以直接看到我們的主介面了。  

  這是一個很粗糙的首頁,我們接下來要學習如何做一個自己的介面。

  開啟編輯器,進入到vue-framework-wz/src/views資料夾下,新建一個vue檔案,名字就叫做wz.vue,然後定義好一個vue元件的dom模板,如下圖。

      

  我們寫點什麼呢?先寫一個簡單標題能看到效果吧。

<template>
  <div class
="animated fadeIn"> <h3>框架在手,天下我有</h3> <p>好用的框架決定了一個程式設計師的效率</p> </div> </template>

  如果我們想要有自適應的效果怎麼辦?加上Row行元件和Col列元件,並設定好Col的屬性

<template>
  <div class="animated fadeIn">

    <Row>
      <Col :sm="24" :md="24" :lg="12">

        <
h3>框架在手,天下我有</h3> <p>好用的框架決定了一個程式設計師的效率</p> </Col> </Row> </div> </template> <script> export default { data () { return {         } } } </script>

這樣就很簡單的做好了一個頁面,接下來就要配置路由了,進入到vue-framework-wz/src/router/index.js檔案,把我們的元件引入進來,然後在下面配置好我們的路由,如下所示。

 

 完整index.js程式碼

import Vue from 'vue';
import Router from 'vue-router';
const _import = require('./_import_' + process.env.NODE_ENV);
import Full from '@/containers/Full'
import Buttons from '@/views/components/Buttons'

// Views - Pages
import Page404 from '@/views/errorPages/Page404'
import Page500 from '@/views/errorPages/Page500'


/* login */
const Login = _import('login/index');
Vue.use(Router);

export const constantRouterMap = [
    { path: '/login', component: Login, hidden: true },
    {path: '/pages',redirect: '/pages/p404', name: 'Pages',
          component: {
            render (c) { return c('router-view') }
              // Full,
          },
          children: [{path: '404',  name: 'Page404', component: _import('errorPages/Page404') },
                     {path: '500',name: 'Page500',component: _import('errorPages/Page404')},
                    ]
    }


]

export default new Router({
  mode: 'hash', 
  linkActiveClass: 'open active',
  scrollBehavior: () => ({ y: 0 }),
  routes: constantRouterMap
});

export const asyncRouterMap = [

 {
    path: '/',
    redirect: '/dashboard',
    name: '首頁',
    component: Full,
    hidden:false,
    children: [
     {path: '/dashboard',name: 'Dashboard',icon:'speedometer',component: _import('Dashboard')},
     {path: '/introduction',name: '介紹',icon:'thumbsup',component: _import('Introduction')},
     {path: '/components',name: 'component元件',redirect: '/components/buttons',icon:'bookmark',
        component: {render (c) { return c('router-view') }},
        children: [ {path: 'buttons',name: 'Buttons按鈕',icon:'social-youtube',component: _import('components/Buttons'), hidden:false, },
                    {path: 'hoverbuttons',name: '懸停特效按鈕',icon:'wand',component: _import('components/HoverButtons')},
                    {path: 'alert',name: 'Alert警告提示',icon:'alert',component: _import('components/Alert')},
                    {path: 'card',name: 'Card卡片',icon:'ios-browsers-outline',component: _import('components/Card')},
                    {path: 'datepicker',name: 'DatePicker',icon:'ios-calendar-outline',component: _import('components/DatePicker')},
                    {path: 'form',name: 'Form表單',icon:'ios-list-outline',component: _import('components/Form')},
                    {path: 'modal',name: 'Modal對話方塊',icon:'ios-chatbubble-outline',component: _import('components/Modal')},
                    {path: 'select',name: 'Select選擇器',icon:'ios-arrow-down',component: _import('components/Select')},
                    {path: 'spin',name: 'Spin載入中',icon:'load-d ',component: _import('components/Spin')},
                    {path: 'steps',name: 'Steps步驟條',icon:'ios-checkmark-outline',component: _import('components/Steps')},
                    {path: 'timeline',name: 'Timeline時間軸',icon:'android-more-vertical',component: _import('components/Timeline')},
                    {path: 'transfer',name: 'Transfer穿梭框',icon:'ios-pause-outline',component: _import('components/Transfer')},
                    {path: 'timepicker',name: 'Timepicker',icon:'ios-clock-outline',component: _import('components/Timepicker')},
                    {path: 'upload',name: 'Upload上傳',icon:'ios-cloud-upload-outline',component: _import('components/Upload')},
                  ]
      },
       {path: '/charts',name: 'echart圖表',redirect: '/charts/shopchart',icon:'pie-graph',
        component: {render (c) { return c('router-view') }},
        children: [ {path: 'shopchart',name: '商場統計圖表',icon:'stats-bars',component: _import('charts/ShopChart'), hidden:false, },
                    {path: 'radarchart',name: '雷達圖',icon:'arrow-graph-up-right',component: _import('charts/RadarChart')},
                    {path: 'cakechart',name: '蛋糕銷量圖表',icon:'ios-analytics',component: _import('charts/CakeChart')}
                  ]
      },
      {path: '/table', name: '表格綜合例項',icon:'ios-paper',component: _import('Table'),meta: { role: ['admin'] }},
      {path: '/jsontree', name: 'JSON檢視',icon:'merge',component: _import('JsonTree')},
      {path: '/tabledetail/:id',name: 'TableDetail', hidden:true, component: _import('TableDetail')},
      {path: '/tinymce',name: 'Tinymce編輯器',icon:"android-document",component: _import('Tinymce')},
      {path: '/markdown',name: 'Markdown',icon:"android-list",component: _import('Markdown')},
       {path: '/wz',name: 'WZ',icon:"social-html5",component: _import('wz')},
    ]
  },
  { path: '*', redirect: '/pages/404', hidden: true }
  
];

然後開啟我們的瀏覽器進入到http://localhost:9001/#/wz

頁面就顯示出來了,而且側邊欄已經自動遍歷可訪問的路由生成列表項了。

很方便吧?

接下來我們進階一下,看看如何做一個表格。

首先加入Table標籤,

<Table :columns="columns1" :data="data1"></Table>

再配置列和data屬性:

 export default {
        data () {
            return {
               columns1: [
                    {
                        title: '姓名',
                        key: 'name'
                    },
                    {
                        title: '年齡',
                        key: 'age'
                    },
                    {
                        title: '地址',
                        key: 'address'
                    }
                ],
                data1: [
                    {
                        name: '王小明',
                        age: 18,
                        address: '北京市朝陽區芍藥居'
                    },
                    {
                        name: '張小剛',
                        age: 25,
                        address: '北京市海淀區西二旗'
                    },
                    {
                        name: '李小紅',
                        age: 30,
                        address: '上海市浦東新區世紀大道'
                    },
                    {
                        name: '周小偉',
                        age: 26,
                        address: '深圳市南山區深南大道'
                    }
                ]
            }
        },
      }

這樣簡單的表格就做好了

怎麼在表格中加入按鈕呢,比如檢視,刪除?

這就用到vue的render函數了。

在columns1中加入一個新的屬性,是一個render函式。

{
                        title: '操作',
                        key: 'action',
                        width: 150,
                        align: 'center',
                        render: (h, params) => {
                            return h('div', [
                                h('Button', {
                                    props: {
                                        type: 'primary',
                                        size: 'small'
                                    },
                                    style: {
                                        marginRight: '5px'
                                    },
                                    on: {
                                        click: () => {
                                            this.show(params.index)
                                        }
                                    }
                                }, '檢視'),
                                h('Button', {
                                    props: {
                                        type: 'error',
                                        size: 'small'
                                    },
                                    on: {
                                        click: () => {
                                            this.remove(params.index)
                                        }
                                    }
                                }, '刪除')
                            ]);
                        }
                    }

如果對render函式很陌生的話,移步vue文件學習一下render函式。

然後我們加入一些功能函式,實現檢視和刪除功能。

  methods: {
            show (index) {
                this.$Modal.info({
                    title: '使用者資訊',
                    content: `姓名:${this.data1[index].name}<br>年齡:${this.data1[index].age}<br>地址:${this.data1[index].address}`
                })
            },
            remove (index) {
                this.data1.splice(index, 1);
            }
        },

當我們點選檢視就會出現一個對話方塊,點選刪除就會移除這一行

 大家有什麼問題最好去我github提issues,文章評論評論較長時間才檢視一次。

接下來的教程講一下封裝UI元件、router、webpack、node命令列構建工具等內容。

希望大家看了這系列教程都能製作出自己的前端框架,從而在工作中得心應手。

如果喜歡就點個start鼓勵下作者吧。

線上體驗地址:立即體驗