Convert Figma logo to code with AI

bailicangdu logovue2-happyfri

vue2 + vue-router + vuex 入门项目

10,469
2,900
10,469
22

Top Related Projects

207,677

This is the repo for Vue 2. For Vue 3, go to https://github.com/vuejs/core

227,213

The library for web and native user interfaces.

95,657

Deliver web apps with confidence 🚀

78,194

Cybernetically enhanced web apps

36,546

⚛️ Fast 3kB React alternative with the same modern API. Components & Virtual DOM.

16,083

:fire: An extremely fast, React-like JavaScript library for building modern user interfaces

Quick Overview

Vue2-happyfri is a quiz game application built with Vue.js 2. It demonstrates how to create a simple yet engaging quiz game using Vue.js, incorporating state management with Vuex and routing with Vue Router.

Pros

  • Provides a practical example of building a complete application with Vue.js 2
  • Demonstrates the use of Vuex for state management in a real-world scenario
  • Includes responsive design for mobile and desktop compatibility
  • Offers a good starting point for developers learning Vue.js and related technologies

Cons

  • Uses an older version of Vue.js (Vue 2) instead of the latest Vue 3
  • Limited documentation and comments in the code, which may make it challenging for beginners to understand
  • Lacks comprehensive test coverage
  • The project hasn't been actively maintained or updated recently

Code Examples

  1. Vuex store setup:
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
})
  1. Vue component example (Home.vue):
<template>
	<div class="home_container">
		<div>
			<span class="home_logo"></span>
			<span class="home_name">Vue2-happyfri</span>
		</div>
		<div class="home_nav">
			<router-link to="/item" class="btn btn-primary">开始答题</router-link>
		</div>
	</div>
</template>

<script>
export default {
	name: 'home'
}
</script>
  1. Vuex action example:
export default {
	addNum({ commit, state }, id) {
		commit('REMBER_ANSWER', id);
		if (state.itemNum < state.itemDetail.length) {
			commit('ADD_ITEMNUM', 1);
		}
	},
	// ... other actions
}

Getting Started

To run the project locally:

  1. Clone the repository:

    git clone https://github.com/bailicangdu/vue2-happyfri.git
    
  2. Install dependencies:

    cd vue2-happyfri
    npm install
    
  3. Start the development server:

    npm run dev
    
  4. Open your browser and navigate to http://localhost:8080 to view the application.

Competitor Comparisons

207,677

This is the repo for Vue 2. For Vue 3, go to https://github.com/vuejs/core

Pros of Vue

  • Comprehensive framework with extensive documentation and ecosystem
  • Larger community support and more frequent updates
  • Suitable for both small and large-scale applications

Cons of Vue

  • Steeper learning curve for beginners
  • More complex setup and configuration for advanced features
  • Potentially overwhelming for simple projects

Code Comparison

Vue (core library):

export function createApp(rootComponent, rootProps = null) {
  const app = ensureRenderer().createApp(rootComponent, rootProps)
  const { mount } = app
  app.mount = (containerOrSelector) => {
    // ...
  }
  return app
}

vue2-happyfri (component example):

export default {
  name: 'itemcontainer',
  data() {
    return {
      itemNum: 1,
      itemDetail: [],
      timer: '',
    }
  },
  // ...
}

Summary

Vue is a full-fledged framework offering extensive features and community support, making it suitable for various project sizes. However, it may be more complex for beginners and simple applications. vue2-happyfri, on the other hand, is a specific project built with Vue 2, demonstrating a simpler structure and focused implementation for a particular use case. While Vue provides a comprehensive toolkit, vue2-happyfri showcases a practical application of Vue in a real-world scenario.

227,213

The library for web and native user interfaces.

Pros of React

  • Larger ecosystem and community support
  • More flexible and can be used for both web and mobile development
  • Better performance for large-scale applications

Cons of React

  • Steeper learning curve, especially for beginners
  • Requires additional libraries for full functionality (e.g., routing, state management)
  • More complex setup and configuration

Code Comparison

React:

import React from 'react';

function App() {
  return <h1>Hello, World!</h1>;
}

export default App;

vue2-happyfri:

<template>
  <div>
    <h1>{{ message }}</h1>
  </div>
</template>

<script>
export default {
  data() {
    return {
      message: 'Hello, World!'
    }
  }
}
</script>

Additional Notes

  • React is a more comprehensive library for building user interfaces, while vue2-happyfri is a specific Vue.js project.
  • React uses JSX, which combines JavaScript and HTML-like syntax, while Vue uses a template-based approach.
  • vue2-happyfri is built with Vue 2, which has a different syntax and structure compared to React.
  • React's component-based architecture allows for more reusable and modular code.
  • vue2-happyfri showcases Vue's simplicity and ease of use for smaller projects.
95,657

Deliver web apps with confidence 🚀

Pros of Angular

  • Comprehensive framework with built-in features for large-scale applications
  • Strong typing with TypeScript integration
  • Robust CLI tools for project scaffolding and development

Cons of Angular

  • Steeper learning curve compared to Vue.js
  • More verbose and complex for small projects
  • Larger bundle size, potentially impacting initial load times

Code Comparison

Angular component:

@Component({
  selector: 'app-root',
  template: `
    <h1>{{ title }}</h1>
    <button (click)="incrementCounter()">Count: {{ counter }}</button>
  `
})
export class AppComponent {
  title = 'Angular App';
  counter = 0;
  incrementCounter() {
    this.counter++;
  }
}

vue2-happyfri component:

<template>
  <div>
    <h1>{{ title }}</h1>
    <button @click="incrementCounter">Count: {{ counter }}</button>
  </div>
</template>

<script>
export default {
  data() {
    return {
      title: 'Vue App',
      counter: 0
    }
  },
  methods: {
    incrementCounter() {
      this.counter++
    }
  }
}
</script>

The Angular example showcases TypeScript usage and decorators, while the Vue example demonstrates a more concise component structure. Angular's approach is more opinionated and structured, whereas Vue offers flexibility and simplicity for smaller projects like vue2-happyfri.

78,194

Cybernetically enhanced web apps

Pros of Svelte

  • Smaller bundle sizes and better performance due to compile-time optimization
  • Simpler, more intuitive syntax with less boilerplate code
  • Built-in state management without the need for additional libraries

Cons of Svelte

  • Smaller ecosystem and community compared to Vue.js
  • Fewer third-party components and plugins available
  • Steeper learning curve for developers coming from traditional frameworks

Code Comparison

Svelte component:

<script>
  let count = 0;
  function increment() {
    count += 1;
  }
</script>

<button on:click={increment}>
  Clicks: {count}
</button>

Vue2-happyfri component:

<template>
  <button @click="increment">
    Clicks: {{ count }}
  </button>
</template>

<script>
export default {
  data() {
    return { count: 0 };
  },
  methods: {
    increment() {
      this.count += 1;
    }
  }
};
</script>

Summary

Svelte offers a more modern approach to web development with its compile-time optimizations and simpler syntax. It excels in performance and bundle size reduction. However, vue2-happyfri, being based on Vue.js, benefits from a larger ecosystem and more widespread adoption. The choice between the two depends on project requirements, team expertise, and desired performance characteristics.

36,546

⚛️ Fast 3kB React alternative with the same modern API. Components & Virtual DOM.

Pros of Preact

  • Smaller bundle size and faster performance due to its lightweight nature
  • Closer to vanilla JavaScript, making it easier for developers familiar with standard web technologies
  • Extensive compatibility with React ecosystem through preact-compat

Cons of Preact

  • Smaller community and ecosystem compared to Vue.js
  • Less comprehensive documentation and learning resources
  • May require additional configuration for certain React-specific features

Code Comparison

vue2-happyfri (Vue.js):

<template>
  <div>{{ message }}</div>
</template>

<script>
export default {
  data() {
    return {
      message: 'Hello, Vue!'
    }
  }
}
</script>

Preact:

import { h, Component } from 'preact';

class App extends Component {
  render() {
    return <div>{this.props.message}</div>;
  }
}

export default App;

The code comparison shows the difference in syntax and structure between Vue.js and Preact. Vue.js uses a template-based approach with separate script and template sections, while Preact follows a more React-like JSX syntax. Preact's code is more concise and closer to vanilla JavaScript, but Vue.js offers a clearer separation of concerns in its component structure.

16,083

:fire: An extremely fast, React-like JavaScript library for building modern user interfaces

Pros of Inferno

  • Higher performance and smaller bundle size than Vue2-happyfri
  • More flexible and lightweight, allowing for easier integration into existing projects
  • Offers a virtual DOM implementation, which can lead to faster rendering in complex applications

Cons of Inferno

  • Smaller community and ecosystem compared to Vue.js, which Vue2-happyfri is based on
  • Steeper learning curve for developers familiar with Vue.js or React
  • Less comprehensive documentation and fewer learning resources available

Code Comparison

Vue2-happyfri (Vue.js):

<template>
  <div>{{ message }}</div>
</template>

<script>
export default {
  data() {
    return {
      message: 'Hello, Vue!'
    }
  }
}
</script>

Inferno:

import { render } from 'inferno';

function MyComponent({ message }) {
  return <div>{message}</div>;
}

render(<MyComponent message="Hello, Inferno!" />, document.getElementById('app'));

While both frameworks allow for component-based development, Inferno's syntax is more similar to React, using JSX and functional components. Vue2-happyfri, being a Vue.js project, uses Vue's template syntax and component structure. Inferno's approach may be more familiar to React developers, while Vue's template syntax can be easier for beginners to grasp.

Convert Figma logo designs to code with AI

Visual Copilot

Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.

Try Visual Copilot

README

说明

非常简单的一个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')