一个完整的vue应用 ( vuex+vue-router ) 起手

项目链接

github连接javascript

介绍

  • 本项目主要介绍如何使用vue+vuex+vue-router开启一个SPA应用,注重的是将应用搭建起来,因此项目不大css

  • 第一次发文,不知道如何开口,那我就直接上代码了,一切尽在注释中( ̄▽ ̄)",各位看官原谅html

  • 看这篇文章以前,但愿你已经对vue有所认识,知道vuex,vue-router,要是懂一点flux原理就更好了vue

  • 若是以前是react的用户,我相信转vue必定很是easy,由于二者有不少的共同点java

  • 用到的技术:vue vuex vue-router fetch sass babel webpackreact

目录结构

├── src
│   ├── components    #组件
│   │   └── Counter.vue
│   ├── store
│   │   ├── actions
│   │   │   ├── counter.js    #counter actions
│   │   │   ├── fetchApi.js    #fetch action
│   │   │   └── index.js    ##合并导出 actions
│   │   ├── getters    #经过一些函数对store上的元数据作一些操做后再返回给组件使用
│   │   │   └── index.js    
│   │   ├── mutations    #处理上面对应的actions
│   │   │   ├── counter.js    #counter mutations
│   │   │   ├── fetchApi.js    #fetch mutation
│   │   │   └── index.js    #合并导出 mutations
│   │   └── index.js    #合并上面的东西,export store
│   ├── style #样式
│   │   ├── app.scss
│   │   ├── counterpage.scss
│   │   └── homepage.scss
│   ├── views    #页面,由组件拼凑而成
│   │   ├── App.vue    #能够理解为页面的容器,页面在这个容器中切换
│   │   ├── CounterPage.vue    #计算页
│   │   └── HomePage.vue    #首页
│   ├── index.html    #html模板
│   ├── main.js    #入口文件
│   └── route-config.js    #路由配置
├── package.json
├── .babelrc
└── webpack.config.js

主要文件

src/main.js

作为入口文件,咱们固然会把全部要用到的都给引入进来。webpack

引入router很简单,建立一个VueRouter的实例,最重要的两个参数一个就是路由模式,一个就是路由配置(见下),建立好之后,扔到Vue实例的配置中就行,最终路由的全部相关信息都会挂在this.$router上,组件能够经过this.$router直接访问。git

require('es6-promise').polyfill();  //es6 promise
require('isomorphic-fetch');  //fetch库

import Vue from 'vue';
import VueRouter from 'vue-router';
import routes from './route-config.js';    //路由配置
import store from './store/index.js';    //store
import App from './views/App.vue';    //页面容器

Vue.use(VueRouter);    //vue使用veux,vue-router 都是经过Vue这个对象上的use这个方法。

//建立路由
const router = new VueRouter({
  mode: 'hash',    //路由的模式
  routes
});

//将store, router加入并生成应用
new Vue({
  el: '#application',
  store,
  router,
  render: h => h(App)
});
src/route-config.js

路由配置也很简单,文档有详细的例子。若是应用过大,打包到一个js文件里有点不合适,咱们能够在这里引入页面的时候作页面的懒加载,就是code spliting。懒加载例子es6

import HomePage from './views/HomePage.vue';    //引入页面
import CounterPage from './views/CounterPage.vue';

//固然真正应用的路由不会这么简单,vue-router也提供动态路由,嵌套路由等等,详见vue-router文档
export default [
  { path: '/', component: HomePage },
  { path: '/counter', component: CounterPage}
];
src/store/index.js

同使用vue-router同样,先调一下use方法,而后新建一个Store实例,把state,actions,getters,mutations全扔进去。github

最终将store抛出,会被用在新建vue实例的时候。一样store的全部相关会挂在this.$store上,组件能够经过this.$store直接访问。

import Vue from 'vue';
import Vuex from 'vuex';
import actions from './actions/index.js';
import mutations from './mutations/index.js';
import * as getters from './getters/index.js';

Vue.use(Vuex);

//state
const state = {
  count: 0,    //counter actions 操做的值
  pageData: {}    //fetch action 操做的值
};

//把上面的融到一块儿
export default new Vuex.Store({
  state,
  actions,
  getters,
  mutations
});
src/views/App.vue
<style lang="sass" src="../style/app.scss"></style>

<template lang="html">
  <div id="app">
    <!--你也能够在其余地方使用<router-view></router-view>来建立嵌套路由,详见vue-router文档-->
    <router-view></router-view>
  </div>
</template>

总结

看到这里,各位聪明的看官,必定已经知道如何把vue,vuex,vue-router串联起来了。

vue的官方文档很全,也出了中文文档,并且vue的设计思路清晰,应用的结构也比较简单明了,因此上手vue不是一件很难的事情。

分享一波文档地址:

vue vuex vue-router

最后各位别忘了github右上角点波关注, 噢,说错了,点颗star ( ̄3 ̄),谢谢你们

相关文章
相关标签/搜索