本文粗略讲述了Vue单页面应用开发,包括组件:状态管理模块Vuex、路由管理模块VueRouter、AJAX模块VueResource等,新建一个页面并实现跳转。
VueResource 工程中默认使用的ajax模块是axios,需要额外下载VueResource
1 $ npm install vue-resource
VueRouter VueRouter用于管理单页面应用的页面跳转,可以实现局部页面的切换,路由配置文件为/src/router.js
1 2 3 4 5 6 7 8 9 10 11 12 13 14 const routers = [{ path: '/' , meta: { title: '' }, component: (resolve ) => require (['./views/index.vue' ], resolve) }, {path : '/index' , component : require ('./views/index.vue' )}, {path : '/hello' , component : require ('./views/main/hello.vue' )}, {path : '*' , component : require ('./views/index.vue' )}, ] export default routers
Main.js /src/main.js为项目总配置文件,在其中引入各模块,并实例化前端应用根节点,内容如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 import Vue from 'vue' import iView from 'iview' import VueRouter from 'vue-router' import Routers from './router' import Vuex from 'vuex' import VueResource from 'vue-resource' import Util from './libs/util' import App from './app.vue' import 'iview/dist/styles/iview.css' Vue.use(VueRouter) Vue.use(Vuex) Vue.use(VueResource) Vue.use(iView) const RouterConfig = { mode: 'history' , routes: Routers } const router = new VueRouter(RouterConfig)router.beforeEach((to, from , next ) => { iView.LoadingBar.start() Util.title(to.meta.title) next() }) router.afterEach(() => { iView.LoadingBar.finish() window .scrollTo(0 , 0 ) }) const store = new Vuex.Store({ state: {}, getters: {}, mutations: {}, actions: {} }) var vm = new Vue({ el: '#app' , router: router, store: store, render: h => h(App) })
Hello World
新建页面/src/views/main/hello.vue
1 2 3 4 5 6 7 8 9 10 <template > <div id ="hello" > Hello World! </div > </template > <script > export default { name: 'hello' , } </script >
新建样式文件/src/styles/main.less和/src/styles/variables.less,前者用于全局样式,后者用于定义全局less变量
1 2 3 4 5 6 7 8 9 10 @global-color: #ffffff ;@import '../styles/variables.less' ;html , body { height : 100% ; background-color : @global-color }
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 <style lang ="less" scoped > @import '../styles/main.less'; </style > <template > <div > <button @click ="goHello" > Go to Hello</button > </div > </template > <script > export default { methods: { goHello: function ( ) { this .$router.push({ path: '/hello' }), this .$http.get('/dist/data/ajax.json' ).then((response ) => { console .log("Success message: " + response.body.message) }, (response) => { console .log("Failure message: " + response.body.message) }); } } } </script >
VueResource
VueResource负责与后端进行Ajax请求交互,基本用法如下
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 export default { methods: { ajaxReq: function ( ) { var me = this ; this .$http.get('/dist/data/ajax.json' ).then((response ) => { console .log(this === me) console .log("Success message: " + response.body.message) }, (response) => { console .log("Failure message: " + response.body.message) }); } } }
1 2 Vue.$http.get() this .$http.get()
1 2 3 4 5 6 7 get(url, [options]) head(url, [options]) delete(url, [options]) jsonp(url, [options]) post(url, [body], [options]) put(url, [body], [options]) patch(url, [body], [options])
1 2 3 4 5 6 7 8 9 10 11 url string 请求的URL method string 请求的HTTP方法,例如:'GET', 'POST'或其他HTTP方法 body Object,FormData string request body params Object 请求的URL参数对象 headers Object request header timeout number 单位为毫秒的请求超时时间 (0 表示无超时时间) before function(request) 请求发送前的处理函数,类似于jQuery的beforeSend函数 progress function(event) ProgressEvent回调处理函数 credentials boolean 表示跨域请求时是否需要使用凭证 emulateHTTP boolean 发送PUT, PATCH, DELETE请求时以HTTP POST的方式发送,并设置请求头的X-HTTP-Method-Override emulateJSON boolean 将request body以application/x-www-form-urlencoded content type发送
1 2 3 4 5 6 7 8 9 方法 类型 描述 text() string 以string形式返回response body json() Object 以JSON对象形式返回response body blob() Blob 以二进制形式返回response body 属性 类型 描述 ok boolean 响应的HTTP状态码在200~299之间时,该属性为true status number 响应的HTTP状态码 statusText string 响应的状态文本 headers Object 响应头