本人寫的小程序,功能還在完善中,歡迎掃一掃提出寶貴意見!
步驟:
1.使用vue-cli創建項目
2.使用vue-router實現單頁路由
3.用vuex管理我們的數據流
4.使用vue-resource請求我們的node服務端
5.使用.vue文件進行組件化的開發
一、目錄結構:
二、搭建項目
需先安裝淘寶鏡像:npm install -g cnpm --registry=https://registry.npm.taobao.org
Mac安裝 vue-cli: sudo npm install -g vue-cli
Windows 安裝 vue-cli:npm install -g vue-cli
構建初始化項目:vue init webpack project(創建webpack項目並下載依賴)
輸入命令進入項目: cd project
安裝依賴: npm install
npm i
開始運行:
npm run dev (或輸入http://localhost:8080),在熱加載中運行我們的應用
它會去找到package.json
的scripts
對象,執行node bulid/dev-server.js
在這文件里,配置了Webpack,會讓它去編譯項目文件,並且運行服務器。
這些都准備好后,我們需要為我們的路由、XHR請求、數據管理下載三個庫,我們可以從vue的官網中找到他們。另外我們使用bootstrap
作為我的UI庫:
npm i vue-resource vue-router vuex bootstrap --save
三、項目開始
初始化項目(main.js)
查看我們的應用文件,我們可以在src目錄下找到App.vue和main.js文件中,我們引入Vue和App,且創建了一個vue的實例(因為在router這行引入了App組件router.start(App,'#app'))
import Vue from 'vue' import App from './App' import router from './router' import VueResource from 'vue-resource' Vue.use(VueResource) Vue.config.productionTip = false new Vue({ el: '#app', router, template: '<App/>', components: { App } })
index.html
<body> <div id="app"></div> </body>
App.vue
<template> <div id="app"> <div class="row"> <div class="col-xs-offset-2 col-xs-8"> <div class="page-header"> <h2>Router Basic - 01</h2> </div> </div> </div> <div class="row"> <div class="col-xs-2 col-xs-offset-2"> <ul class="list-group"> <!--使用指令v-link進行導航--> <a class="list-group-item"><router-link to="/home">Home</router-link></a> <a class="list-group-item"><router-link to="/about">About</router-link></a> <a class="list-group-item"><router-link to="/contact">Contact</router-link></a> </ul> </div> <div class="col-xs-6"> <div class="panel"> <div class="panel-body"> <!--用於渲染匹配的組件--> <router-view></router-view> </div> </div> </div> </div> </div> </div> </template> <script> export default { name: 'app' } </script>
// src/components/Home.vue 作為我們的首頁
<template id="contact"> <div> <h1>Home</h1> <p>This is the tutorial about Contact.</p> </div> </template> <script> export default { '/hello': 'Hello' } </script>
// src/components/About.vue
<template id="about"> <div> <h1>About</h1> <p>This is the tutorial about vue-router.</p> </div> </template> <script> export default { '/about': 'About' } </script>
// src/components/Contact.vue
<template id="contact"> <div> <h1>Contact</h1> <p>This is the tutorial about Contact.</p> </div> </template> export default { '/contact': 'contact' } </script>
// src/index.js
import Vue from 'vue'
import Router from 'vue-router'
import Hello from '@/components/Hello'
import Home from '@/components/Home'
import About from '@/components/About'
import Contact from '@/components/Contact'
import 'bootstrap/dist/css/bootstrap.css'
Vue.use(Router)
export default new Router({
routes: [
{
path: '/',
name: 'Hello',
component: Hello
},
{
path: '/home',
name: 'Home',
component: Home
},
{
path: '/about',
name: 'About',
component: About
},
{
path: '/contact',
name: '/Contact',
component: Contact
}
]
})
詳細操作:
- 克隆項目:git clone https://github.com/cinderellastory415/vue-demo/tree/master/spa
- 安裝依賴:npm install
- 運行項目:npm run dev 或者 npm start
輸入以上命令,即可查看效果!