vue demo1
1.開發工具
試過sublime,現在轉戰vscode,覺得很順手,總之啥工具習慣就好。
vscode用著不錯的插件,推薦安裝。
2.項目目錄介紹
vue-cli生成的項目目錄有點多,初看有點懵,梳理一下會好很多。
├── index.html 入口頁面├── build 構建腳本目錄│ ├── build-server.js 運行本地構建服務器,可以訪問構建后的頁面│ ├── build.js 生產環境構建腳本│ ├── dev-client.js 開發服務器熱重載腳本,主要用來實現開發階段的頁面自動刷新│ ├── dev-server.js 運行本地開發服務器│ ├── utils.js 構建相關工具方法│ ├── webpack.base.conf.js wabpack基礎配置│ ├── webpack.dev.conf.js wabpack開發環境配置│ └── webpack.prod.conf.js wabpack生產環境配置├── config 項目配置│ ├── dev.env.js 開發環境變量│ ├── index.js 項目配置文件│ ├── prod.env.js 生產環境變量│ └── test.env.js 測試環境變量├── mock mock數據目錄│ └── hello.js├── package.json npm包配置文件,里面定義了項目的npm腳本,依賴包等信息├── src 項目源碼目錄 │ ├── main.js 入口js文件│ ├── app.vue 根組件│ ├── components 公共組件目錄│ │ └── title.vue│ ├── assets 資源目錄,這里的資源會被wabpack構建│ │ └── images│ │ └── logo.png│ ├── routes 前端路由│ │ └── index.js│ ├── store 應用級數據(state)│ │ └── index.js│ └── views 頁面目錄│ ├── hello.vue│ └── notfound.vue├── static 純靜態資源,不會被wabpack構建。└── test 測試文件目錄(unit&e2e)└── unit 單元測試├── index.js 入口腳本├── karma.conf.js karma配置文件└── specs 單測case目錄└── Hello.spec.js3.前端框架依賴
為了不讓sample太蒼白,我們引入現在比較流行的MintUI(如果你覺得比較眼熟,那么恭喜你猜對了,就是餓了么前端),我不懂審美啦,但是流行的審美應該不會太丑吧。
首先安裝mint-ui
# Vue 1.x cnpm install mint-ui@1 -S # Vue 2.0 cnpm install mint-ui -S然后在main.js中引入全部組件(全局導入后不用再導入)
import Vue from 'vue'; import Mint from 'mint-ui'; Vue.use(Mint); // 按需引入部分組件 import { Cell, Checklist } from 'minu-ui'; Vue.component(Cell.name, Cell); Vue.component(Checklist.name, Checklist);4.搭建項目
首先配置vue router,新建init.vue做為首頁。因為是入口,所以注意path配成’/’。同理,新添的組件需要跳轉,也在這配置。
這里init.vue主要由三部分組成,mt-header、mt-tab-container、mt-tabbar。
- mt-header
這里標題綁定了selected變量,是否有點體會到vue的可愛之處。
<mt-header fixed :title="selected">- mt-tab-container
其中小知識點總結:
router-link:仔細看上圖在router目錄下的index.js中,有配置/main的跳轉,這里有點像html中的href。
section:<section> 標簽是 HTML5 中的新標簽,主要定義文檔區域,比如章節、頭部、底部或者文檔的其他區域。
<main_content></main_content>:這里引入了自定義組件,需要script中import進來后在 components中添加。
- mt-tabbar
我們循環data中的tabs數組,取得其中的id與name,并判斷tab點擊后,tab圖片高亮。
<mt-tabbar v-model="selected" ><mt-tab-item v-for="(item, index) in tabs" :key="index" :id="item.id"><img slot="icon" :src="selected == item.id ? item.tab_img_active : item.tab_img"> {{item.name}}</mt-tab-item> </mt-tabbar>對應的處理代碼:
<script> import Vue from 'vue' import MainContent from './MainContent' import { Header, Tabbar, TabItem, TabContainer, TabContainerItem } from 'mint-ui' Vue.component(TabContainer.name, TabContainer) Vue.component(TabContainerItem.name, TabContainerItem) Vue.component(Tabbar.name, Tabbar) Vue.component(TabItem.name, TabItem) Vue.component(Header.name, Header) export default {name: 'init',data () {return {selected: '首頁',msg: 'Welcome!!',tabs: [{id: '首頁',name: '首頁',active: 'false',tab_img: require('../assets/nav_home.png'),tab_img_active: require('../assets/nav_home_active.png')}, {id: '消息',name: '消息',active: 'false',tab_img: require('../assets/nav_cxl.png'),tab_img_active: require('../assets/nav_cxl_now.png')}, {id: '管車',name: '管車',active: 'false',tab_img: require('../assets/nav_car.png'),tab_img_active: require('../assets/nav_car_now.png')}, {id: '我的',name: '我的',active: 'false',tab_img: require('../assets/nav_my.png'),tab_img_active: require('../assets/nav_my_now.png')}]}},components: {'main_content': MainContent},computed: {},methods: {} } </script>5.運行項目
總結
- 上一篇: php 自带缓存,封装ThinkPhP自
- 下一篇: 关于Vue.use()详解