GithubHelp home page GithubHelp logo

vue2-happyfri's Introduction

说明

非常简单的一个vue2 + vuex的项目,整个流程一目了然,麻雀虽小,五脏俱全,适合作为入门练习。

如果对您有帮助,您可以点右上角 "Star" 支持一下 谢谢! ^_^

或者您可以 "follow" 一下,我会不断开源更多的有趣的项目

如有问题请直接在 Issues 中提,或者您发现问题并有非常好的解决方案,欢迎 PR 👍

开发环境 macOS 10.12.3 Chrome 56 nodejs 6.10.0

这个项目主要用于 vue2 + vuex 的入门练习,另外推荐一个 vue2 比较复杂的大型项目,覆盖了vuejs大部分的知识点。目前项目已经完成。地址在这里

项目运行(nodejs 6.0+)

# 克隆到本地
git clone https://github.com/bailicangdu/vue2-happyfri.git

# 进入文件夹
cd vue2-happyfri

# 安装依赖
npm install 或 yarn(推荐)

# 开启本地服务器localhost:8088
npm run dev

# 发布环境
npm run build

效果演示

demo地址(请用chrome手机模式预览)

移动端扫描下方二维码

路由配置

import App from '../App'

export default [{
    path: '/',
    component: App,
    children: [{
        path: '',
        component: r => require.ensure([], () => r(require('../page/home')), 'home')
    }, {
        path: '/item',
        component: r => require.ensure([], () => r(require('../page/item')), 'item')
    }, {
        path: '/score',
        component: r => require.ensure([], () => r(require('../page/score')), 'score')
    }]
}]

配置actions

import ajax from '../config/ajax'

export default {
	addNum({ commit, state }, id) {
		//点击下一题,记录答案id,判断是否是最后一题,如果不是则跳转下一题
		commit('REMBER_ANSWER', id);
		if (state.itemNum < state.itemDetail.length) {
			commit('ADD_ITEMNUM', 1);
		}
	},
	//初始化信息
	initializeData({ commit }) {
		commit('INITIALIZE_DATA');
	}
}

mutations

const ADD_ITEMNUM = 'ADD_ITEMNUM'
const REMBER_ANSWER = 'REMBER_ANSWER'
const REMBER_TIME = 'REMBER_TIME'
const INITIALIZE_DATA = 'INITIALIZE_DATA'
export default {
	//点击进入下一题
	[ADD_ITEMNUM](state, payload) {
		state.itemNum += payload.num;
	},
	//记录答案
	[REMBER_ANSWER](state, payload) {
		state.answerid[state.itemNum] = payload.id;
	},
	/*
	记录做题时间
	 */
	[REMBER_TIME](state) {
		state.timer = setInterval(() => {
			state.allTime++;
		}, 1000)
	},
	/*
	初始化信息,
	 */
	[INITIALIZE_DATA](state) {
		state.itemNum = 1;
		state.allTime = 0;
	},
}

创建store

import Vue from 'vue'
import Vuex from 'vuex'
import mutations from './mutations'
import actions from './action'


Vue.use(Vuex)

const state = {
	level: '第一周',
	itemNum: 1,
	allTime: 0,
	timer: '',
	itemDetail: [],
	answerid: {}
}

export default new Vuex.Store({
	state,
	actions,
	mutations
})

创建vue实例

import Vue from 'vue'
import VueRouter from 'vue-router'
import routes from './router/router'
import store from './store/'

Vue.use(VueRouter)
const router = new VueRouter({
	routes
})

new Vue({
	router,
	store,
}).$mount('#app')

vue2-happyfri's People

Contributors

aswind7 avatar bailicangdu avatar dingdj avatar gzz0204 avatar lakerswgq avatar shun2014 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  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

vue2-happyfri's Issues

npm run dev 报错,新人求解。

$ npm run dev

[email protected] dev I:\workspace\WebStorm\vue2-happyfri
node build/dev-server.js

module.js:538
throw err;
^

Error: Cannot find module 'express'
at Function.Module._resolveFilename (module.js:536:15)
at Function.Module._load (module.js:466:25)
at Module.require (module.js:579:17)
at require (internal/module.js:11:18)
at Object. (I:\workspace\WebStorm\vue2-happyfri\build\dev-server.js:5:15)
at Module._compile (module.js:635:30)
at Object.Module._extensions..js (module.js:646:10)
at Module.load (module.js:554:32)
at tryModuleLoad (module.js:497:12)
at Function.Module._load (module.js:489:3)
npm ERR! code ELIFECYCLE
npm ERR! errno 1
npm ERR! [email protected] dev: node build/dev-server.js
npm ERR! Exit status 1
npm ERR!
npm ERR! Failed at the [email protected] dev script.
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.
npm WARN Local package.json exists, but node_modules missing, did you mean to install?

npm ERR! A complete log of this run can be found in:
npm ERR! C:\Users\FXBL-PC\AppData\Roaming\npm-cache_logs\2017-11-23T16_57_23_461Z-debug.log

上传头像显示401错误

POST http://localhost:8000/eus/v1/users/186655961/avatar 401 (Unauthorized)
_callee$ @ info.vue?8291:175
tryCatch @ runtime.js?1aab:65
invoke @ runtime.js?1aab:303
prototype.(anonymous function) @ runtime.js?1aab:117
step @ asyncToGenerator.js?74f6:17
(anonymous) @ asyncToGenerator.js?74f6:35
F @ _export.js?ece2:35
(anonymous) @ asyncToGenerator.js?74f6:14
uploadAvatar @ info.vue?8291:188
boundFn @ vue.common.js?e881:166
invoker @ vue.common.js?e881:1739

info.vue?8291:185 Uncaught (in promise) Error: TypeError: path.indexOf is not a function
at VueComponent._callee$ (eval at 202 (http://localhost:8000/15.info.js:90:2), :127:39)
at tryCatch (eval at (http://localhost:8000/app.js:1708:2), :65:40)
at Generator.invoke [as _invoke] (eval at (http://localhost:8000/app.js:1708:2), :303:22)
at Generator.prototype.(anonymous function) [as next] (eval at (http://localhost:8000/app.js:1708:2), :117:21)
at step (eval at (http://localhost:8000/app.js:706:2), :17:30)
at eval (eval at (http://localhost:8000/app.js:706:2), :28:13)
at
_callee$ @ info.vue?8291:185
tryCatch @ runtime.js?1aab:65
invoke @ runtime.js?1aab:303
prototype.(anonymous function) @ runtime.js?1aab:117
step @ asyncToGenerator.js?74f6:17
(anonymous) @ asyncToGenerator.js?74f6:28

对于vue-router的写法的问题

我看你项目router目录下的路由的写法是

 {
        path: '/item',
        component: r => require.ensure([], () => r(require('../page/item')), 'item')
    },

而我看到的大多数路由的写法是

 {
        path: '/item',
        component: item
      }

感觉你这种写法比较复杂,为什么要这么复杂的写,这么写有什么好处呢?

开发环境下图片不能显示

你好,我在仿写这个项目的时候遇到了一个问题。我尝试了很久都没能搞清楚问题的原因。

  • 已查询过相关文档
  • 已上网做过相应查询

是这样的,我在模仿主页,在CSS和created钩子函数插入资源的时候遇到了问题,无论怎么改变图片的路径,它都不会在页面上显示。控制台没有错误信息输出

我认为这是vue-loader中的东西,于是查看了相关文档,但是文档上说有关.png .jpgfile-loader已经集成在环境中了。有关代码已经上传至此处

另外,我还有个疑问想请教。在你给出的代码中第89行,为什么图片的路径是在static路径下,而源代码中不存在static这个文件夹。是当时的脚手架没有给出这个文件夹还是这个就是引入webpack打包好之后的静态资源文件

我的VueVue-CLI版本是2.9.1webpack版本是3.6.0

非常感谢你的帮助

少一个·babel-runtime的依赖

npm run dev 运行时,会有

Module not found: Error: Cannot resolve module 'babel-runtime/helpers/typeof' in /home/me/WebstormProjects/vue2-happyfri/src/config
 @ ./src/config/ajax.js 7:15-54

ERROR in ./src/config/ajax.js
Module not found: Error: Cannot resolve module 'babel-runtime/core-js/json/stringify' in /home/me/WebstormProjects/vue2-happyfri/src/config
 @ ./src/config/ajax.js 11:17-64

ERROR in ./src/config/ajax.js
Module not found: Error: Cannot resolve module 'babel-runtime/core-js/object/keys' in /home/me/WebstormProjects/vue2-happyfri/src/config
 @ ./src/config/ajax.js 15:12-56

ERROR in ./src/config/ajax.js
Module not found: Error: Cannot resolve module 'babel-runtime/core-js/promise' in /home/me/WebstormProjects/vue2-happyfri/src/config
 @ ./src/config/ajax.js 19:15-55

ERROR in ./src/store/mutations.js
Module not found: Error: Cannot resolve module 'babel-runtime/helpers/defineProperty' in /home/me/WebstormProjects/vue2-happyfri/src/store
 @ ./src/store/mutations.js 7:23-70

package.json依赖中缺少"babel-runtime"

The style was wrong?

样式是不是有问题啊?
感觉不是一个完整的项目,运行后和demo的完全不一样

Mutation 必须是同步函数

刚开始学Vue,请教一下,不知道对不对。我看文档上说:
现在想象,我们正在 debug 一个 app 并且观察 devtool 中的 mutation 日志。每一条 mutation 被记录,devtools 都需要捕捉到前一状态和后一状态的快照。然而,在上面的例子中 mutation 中的异步函数中的回调让这不可能完成:因为当 mutation 触发的时候,回调函数还没有被调用,devtools 不知道什么时候回调函数实际上被调用——实质上任何在回调函数中进行的状态的改变都是不可追踪的;
您代码里记录做题时间有这样写:
/* 记录做题时间 */ [REMBER_TIME](state) { state.timer = setInterval(() => { state.allTime++; }, 1000) },
不知道这样有没有问题,请指教!

npm run build no such file or directory:static/*

npm run build

[email protected] build G:\workplace\WebStorm\vue2-happyfri
node build/build.js

cp: no such file or directory: static/*
Hash: 279e8632ca3d04732614
Version: webpack 1.15.0
Time: 4519ms
Asset Size Chunks Chunk Names
static/js/home.abe9f43ebc69913271f5.min.js 29.2 kB 1, 3 [emitted] home
static/img/1-1.jpg 144 kB [emitted]
static/img/4-1.jpg 193 kB [emitted]
static/img/4-2.png 14.2 kB [emitted]
static/img/4-4.png 10.4 kB [emitted]
static/js/item.b0390b470966316eaf4e.min.js 29.2 kB 0, 3 [emitted] item
static/img/1-2.png 25.5 kB [emitted]
static/js/score.1bf2ac51530fa17b0b12.min.js 21.4 kB 2, 3 [emitted] score
static/js/manifest.js 962 bytes 3 [emitted] manifest
static/js/vendor.js 153 kB 4, 3 [emitted] vendor
static/js/app.js 6.25 kB 5, 3 [emitted] app
static/css/app.css 970 bytes 5, 3 [emitted] app
index.html 906 bytes [emitted]

感谢,感谢

在你的基础上走完了vuex,把你记分的逻辑写完了。但是有点不好,我是用state里一个变量记录的,如果将分数页面直接复制出去,记分就失效了。这个记录分数的变量可以放在localStorage里面就可以消除这个问题
再次感谢

npm run dev 后报错

直接下载后
npm install后提示deprecate autoprefixer-loader@^3.2.0 ,deprecate babel-preset-es2015@^6.0.0
npm run dev 后有抛出错误
[email protected] dev C:\Users\gaof\Desktop\VUE\vue2-happyfri-master

node build/dev-server.js

events.js:183
throw er; // Unhandled 'error' event
^

Error: listen EACCES 0.0.0.0:8088
at Object._errnoException (util.js:1024:11)
at _exceptionWithHostPort (util.js:1046:20)
at Server.setupListenHandle [as _listen2] (net.js:1334:19)
at listenInCluster (net.js:1392:12)
at Server.listen (net.js:1476:7)
at Function.listen (C:\Users\gaof\Desktop\VUE\vue2-happyfri-master\node_modules_express@4.16.2@express\lib\application.js:618:24)
at Object. (C:\Users\gaof\Desktop\VUE\vue2-happyfri-master\build\dev-server.js:70:25)
at Module._compile (module.js:635:30)
at Object.Module._extensions..js (module.js:646:10)
at Module.load (module.js:554:32)
at tryModuleLoad (module.js:497:12)
at Function.Module._load (module.js:489:3)
at Function.Module.runMain (module.js:676:10)
at startup (bootstrap_node.js:187:16)
at bootstrap_node.js:608:3
npm ERR! code ELIFECYCLE
npm ERR! errno 1
npm ERR! [email protected] dev: node build/dev-server.js
npm ERR! Exit status 1
npm ERR!
npm ERR! Failed at the [email protected] dev script.
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.

npm ERR! A complete log of this run can be found in:
npm ERR! C:\Users\gaof\AppData\Roaming\npm-cache_logs\2017-12-19T07_46_57_968Z-debug.log
请问下这里是需要更新什么吗 新手表示没看懂错误,请大神解惑

注释呢

初学者一枚,有些代码都不知道干啥的,为啥不写注释呢

ajax代理的时候POST请示貌似有问题

传不了图片,抓包的时候 用POST请求 Form Data 里出现 {"phoneNum":"13545697852"}: 只有键 没有值。
是不是ajax.js有问题 或者我哪里写错了 望指点一下

咨询一下rem.js导入

你好,请问在main.js里面导入import './config/rem'这个是怎么生效的呢?
因为我看到里面并没有export,vue实例化的时候是怎么使用得到这种导入的呢?谢谢

config/index.html

build: {assetsSubDirectory: 'static', assetsPublicPath: '/happyfri/',}
and
build: {assetsSubDirectory: 'static', assetsPublicPath: './',}
都是no such file or directory :static/*
why?

刚开始看vue2

npm run dev 之后浏览器没有反应,启动不了,怎么解决

计算结果不对

答完题后,每次的得分都是20分,发现你在score/index.vue中的created()方法中给score赋值为20,难道不应该是前一个页面传过来,或者用vuex把score也当作状态来管理吗?

main.js 中 引入common.less 出现报错信息

新建了一个项目,同样在main中引入common.js,为什么在main.js 中 引入 import './assets/style/common.less' 出现如下报错信息:
This relative module was not found:

  • ./assets/style/common in ./src/main.js,要先安装什么模块才能这样引用吗?

怎么调试呢?

您好,我刚刚开始学VUE,看了这个项目后很困惑平时开发的时候到底该怎么跑起来调试,难道每次都编译发布?

store 在子组建中找不到

刚开始学习vue,下载这个demo,自己照着写了一个,
在子组建中的定义
image
store中的定义
image
运行的时候报这个错误

应该是再子组建中找不到

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.