第一步:搭建環境
安裝vue-cli
cnpm install -g vue-cli
安裝vue-router
cnpm install -g vue-router
使用vue-cli初始化項目
vue init webpack cppba-web
進入到目錄
cd cppba-web
安裝依賴
cnpm install
開始運行
npm run dev
(這里提個畫外音:配置文件的時候會讓你選擇,這里推薦:
? Use ESLint to lint your code? 代碼規范,推薦 N
? Setup unit tests with Karma + Mocha? 單元測試,推薦 N
? Setup e2e tests with Nightwatch? E2E測試,N
這三個很重要,起碼對我來說是的)
第二步:在src文件下新建page目錄
第三步:在components新建tabBar.vue文件夾,放置路由tab切換組件
安裝vue-cli cnpm install -g vue-cli 安裝vue-router cnpm install -g vue-router 使用vue-cli初始化項目 vue init webpack cppba-web 進入到目錄 cd cppba-web 安裝依賴 cnpm install 開始運行 npm run dev
第四步:在page目錄下新建Home.vue和Me.vue文件,這是放置切換路由頁面的顯示
Home.vue代碼 <template> <div> 首頁 </div> </template> <script> </script> <style> </style> Me.vue代碼 <template> <div> 我 </div> </template> <script> </script> <style> </style>
第五步:渲染路由與頁面,在App.vue中
<template> <div id="app"> <router-view></router-view> <tabBar></tabBar> </div> </template> <script> import tabBar from './components/tabBar' export default { name: 'App', components:{ tabBar } } </script> <style> #app { font-family: 'Avenir', Helvetica, Arial, sans-serif; -webkit-font-smoothing: antialiased; -moz-osx-font-smoothing: grayscale; text-align: center; color: #2c3e50; margin-top: 60px; } </style>
第六步:在router目錄下的index.js中配置路由
import Vue from 'vue' import Router from 'vue-router' import Home from '../page/Home.vue' import Me from '../page/Me.vue' Vue.use(Router) export default new Router({ routes: [ { path: '/', component: Home }, { path:'/home', component:Home }, { path:'/me', component:Me } ] })
最后放上成果: