Code Monkey home page Code Monkey logo

vue-spa-template's Introduction

项目简介

基于 vue.js 的前端开发环境,用于前后端分离后的单页应用开发,可以在开发时使用 ES Next、scss 等最新语言特性。项目包含:

  • 基础库: vue.jsvue-routervuexwhatwg-fetch
  • 编译/打包工具:webpackbabelnode-sass
  • 单元测试工具:karmamochasinon-chai
  • 本地服务器:express

目录结构

├── README.md                       项目介绍
├── 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.js

环境安装

本项目依赖 node.js, 使用前先安装 node.js 和 cnpm(显著提升依赖包的下载速度)。

  1. 自行下载并安装 node.js: https://nodejs.org/en/download/

  2. 然后安装 cnpm 命令:

     npm install -g cnpm --registry=https://registry.npm.taobao.org
    

快速开始

git clone https://github.com/hanan198501/vue-spa-template.git 
cd vue-spa-template
cnpm install
npm run dev

命令列表:

#开启本地开发服务器,监控项目文件的变化,实时构建并自动刷新浏览器,浏览器访问 http://localhost:8081
npm run dev

#使用生产环境配置构建项目,构建好的文件会输出到 "dist" 目录,
npm run build

#运行构建服务器,可以查看构建的页面
npm run build-server

#运行单元测试
npm run unit

前后端分离

项目基于 spa 方式实现前后端分离,服务器通过 nginx 区分前端页面和后端接口请求,分发到不同服务。前端物理上只有一个入口页面, 路由由前端控制(基于vue-router),根据不同的 url 加载相应数据和组件进行渲染。

接口 mock

前后端分离后,开发前需要和后端同学定义好接口信息(请求地址,参数,返回信息等),前端通过 mock 的方式,即可开始编码,无需等待后端接口 ready。 项目的本地开发服务器是基于 express 搭建的,通过 express 的中间件机制,我们已经在 dev-server 中添加了接口 mock 功能。 开发时,接口的 mock 数据统一放在 mock 目录下,每个文件内如下:

module.exports = {

  // 接口地址
  api: '/api/hello',

  // 返回数据 参考http://expressjs.com/zh-cn/4x/api.html
  response: function (req, res) {
    res.send(`
      <p>hello vue!</p>
    `);
  }
}

模块化

开发时可以使用 ES2015 module 语法,构建时每个文件会编译成 amd 模块。

组件化

整个应用通过 vue 组件的方式搭建起来,通过 vue-router 控制相应组件的展现,组件树结构如下:

app.vue                         根组件(整个应用只有一个)
    ├──view1.vue                    页面级组件,放在 views 目录里面,有子组件时,可以建立子目录
    │   ├──component1.vue               功能组件,公用的放在 components 目录,否则放在 views 子目录
    │   ├──component2.vue
    │   └──component3.vue
    ├──view2.vue
    │   ├──component1.vue
    │   └──component4.vue
    └──view3.vue
        ├──component5.vue
        ……

单元测试

可以为每个组件编写单元测试,放在 test/unit/specs 目录下面, 单元测试用例的目录结构建议和测试的文件保持一致(相对于src),每个测试用例文件名以 .spec.js结尾。 执行 npm run unit 时会遍历所有的 spec.js 文件,产出测试报告在 test/unit/coverage 目录。

联调方式

前后端分离后,由于服务端和前端的开发环境处于2台不同的机器上,前端的异步请求需要代理到后端机器中。 联调的时候,只需通过 proxy 参数运行 dev 脚本即可,所有 mock 目录下定义的接口将会转发到 proxy 参数指定的机器:

# 172.16.36.90:8083 为后端机器的环境地址
npm run dev -- --proxy=172.16.36.90:8083

这样,如果 mock 目录下有定义了接口 /api/hello ,将会转发到 http://172.16.36.90/:8083/api/hello

相关资源

vue-spa-template's People

Contributors

annnhan avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

vue-spa-template's Issues

按照安装方法正常安装后npm run dev 执行时报错error

16 verbose cwd E:\project\vue-spa-template
17 error Windows_NT 10.0.14393
18 error argv "D:\nodejs\node.exe" "D:\nodejs\node_modules\npm\bin\npm-cli.js" "run" "dev"
19 error node v6.9.2
20 error npm v3.10.9
21 error code ELIFECYCLE
22 error [email protected] dev: node build/dev-server.js
22 error Exit status 1
23 error Failed at the [email protected] dev script 'node build/dev-server.js'.
23 error Make sure you have the latest version of node.js and npm installed.
23 error If you do, this is most likely a problem with the my-first-vue-app package,
23 error not with npm itself.
23 error Tell the author that this fails on your system:
23 error node build/dev-server.js
23 error You can get information on how to open an issue for this project with:
23 error npm bugs my-first-vue-app
23 error Or if that isn't available, you can get their info via:
23 error npm owner ls my-first-vue-app
23 error There is likely additional logging output above.
24 verbose exit [ 1, true ]

master分支上,package.json文件里,开发本地环境里运行 node run dev,缺少包文件"babel-runtime"

master分支上,package.json文件里,开发本地环境里运行 node run dev,缺少包文件"babel-runtime"

ERROR in .//.npminstall/babel-loader/6.2.8/babel-loader/lib!.//.npminstall/vue-loader/9.9.5/vue-loader/lib/selector.js?type=script&index=0!./src/views/hello.vue
Module not found: Error: Cannot resolve module 'babel-runtime/regenerator' in /Users/jerry/demo/vue-spa-template/src/views
@ .//.npminstall/babel-loader/6.2.8/babel-loader/lib!.//.npminstall/vue-loader/9.9.5/vue-loader/lib/selector.js?type=script&index=0!./src/views/hello.vue 7:19-55

ERROR in .//.npminstall/babel-loader/6.2.8/babel-loader/lib!.//.npminstall/vue-loader/9.9.5/vue-loader/lib/selector.js?type=script&index=0!./src/views/hello.vue
Module not found: Error: Cannot resolve module 'babel-runtime/helpers/asyncToGenerator' in /Users/jerry/demo/vue-spa-template/src/views
@ .//.npminstall/babel-loader/6.2.8/babel-loader/lib!.//.npminstall/vue-loader/9.9.5/vue-loader/lib/selector.js?type=script&index=0!./src/views/hello.vue 11:25-74

安装完运行出错

浏览器提示
Uncaught SyntaxError: Unexpected token import
是因为cnpm 吗,但npm install 试了一下也没报错,请问是什么原因?

$store.commit的值为false时失败

在vuex/store.js中的这行代码

// 生成 mutations 方法
function generate(name) {
  return function (state, value) {
    state[name] = value || state[name];  // 这行代码会导致我commit('xxxx', false)时失败
  }
}

可以提供一个使用 mock 的 demo 吗?

您好:
谢谢分享一个这么详细的 vue template。因为刚刚学习使用 vue 做 spa 页面开发,受益匪浅。
但是有点愚钝,通过 express 做 mock 这里有点不是很理解,可以在您空闲的时候做个 demo 或者详细说一下吗?
拜谢。

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.