├── .browserslistrc
├── .editorconfig
├── .env.development
├── .env.production
├── .eslintrc.js
├── .gitignore
├── README.md
├── babel.config.js
├── package-lock.json
├── package.json
├── postcss.config.js
├── public
├── favicon.ico
└── index.html
├── src
├── App.vue
├── api
│ ├── mock
│ │ ├── data
│ │ │ └── posts.json
│ │ └── index.js
│ └── server
│ │ └── index.js
├── assets
│ └── logo.png
├── components
│ └── HelloWorld.vue
├── main.js
├── router.js
├── store.js
└── views
│ ├── About.vue
│ └── Home.vue
└── vue.config.js
/.browserslistrc:
--------------------------------------------------------------------------------
1 | > 1%
2 | last 2 versions
3 | not ie <= 8
4 |
--------------------------------------------------------------------------------
/.editorconfig:
--------------------------------------------------------------------------------
1 | [*.{js,jsx,ts,tsx,vue}]
2 | indent_style = space
3 | indent_size = 2
4 | trim_trailing_whitespace = true
5 | insert_final_newline = true
6 |
--------------------------------------------------------------------------------
/.env.development:
--------------------------------------------------------------------------------
1 | VUE_APP_API_CLIENT = 'mock'
--------------------------------------------------------------------------------
/.env.production:
--------------------------------------------------------------------------------
1 | VUE_APP_API_CLIENT = 'server'
--------------------------------------------------------------------------------
/.eslintrc.js:
--------------------------------------------------------------------------------
1 | module.exports = {
2 | root: true,
3 | env: {
4 | node: true
5 | },
6 | 'extends': [
7 | 'plugin:vue/essential',
8 | '@vue/standard'
9 | ],
10 | rules: {
11 | 'no-console': process.env.NODE_ENV === 'production' ? 'error' : 'off',
12 | 'no-debugger': process.env.NODE_ENV === 'production' ? 'error' : 'off'
13 | },
14 | parserOptions: {
15 | parser: 'babel-eslint'
16 | }
17 | }
18 |
--------------------------------------------------------------------------------
/.gitignore:
--------------------------------------------------------------------------------
1 | .DS_Store
2 | node_modules
3 | /dist
4 |
5 | # local env files
6 | .env.local
7 | .env.*.local
8 |
9 | # Log files
10 | npm-debug.log*
11 | yarn-debug.log*
12 | yarn-error.log*
13 |
14 | # Editor directories and files
15 | .idea
16 | .vscode
17 | *.suo
18 | *.ntvs*
19 | *.njsproj
20 | *.sln
21 | *.sw*
22 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # Source code for [How to Use Mock Data in Vue Apps](https://tahazsh.com/use-mock-data-in-vue) tutorial
2 |
--------------------------------------------------------------------------------
/babel.config.js:
--------------------------------------------------------------------------------
1 | module.exports = {
2 | presets: [
3 | '@vue/app'
4 | ]
5 | }
6 |
--------------------------------------------------------------------------------
/package.json:
--------------------------------------------------------------------------------
1 | {
2 | "name": "vue-cli-3-mock-data",
3 | "version": "0.1.0",
4 | "private": true,
5 | "scripts": {
6 | "serve": "vue-cli-service serve",
7 | "build": "vue-cli-service build",
8 | "lint": "vue-cli-service lint"
9 | },
10 | "dependencies": {
11 | "axios": "^0.18.0",
12 | "vue": "^2.5.21",
13 | "vue-router": "^3.0.1",
14 | "vuex": "^3.0.1"
15 | },
16 | "devDependencies": {
17 | "@vue/cli-plugin-babel": "^3.3.0",
18 | "@vue/cli-plugin-eslint": "^3.3.0",
19 | "@vue/cli-service": "^3.3.0",
20 | "@vue/eslint-config-standard": "^4.0.0",
21 | "babel-eslint": "^10.0.1",
22 | "eslint": "^5.8.0",
23 | "eslint-plugin-vue": "^5.0.0",
24 | "stylus": "^0.54.5",
25 | "stylus-loader": "^3.0.2",
26 | "vue-template-compiler": "^2.5.21"
27 | }
28 | }
29 |
--------------------------------------------------------------------------------
/postcss.config.js:
--------------------------------------------------------------------------------
1 | module.exports = {
2 | plugins: {
3 | autoprefixer: {}
4 | }
5 | }
6 |
--------------------------------------------------------------------------------
/public/favicon.ico:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/TahaSh/use-mock-data-in-vue/bc1ff8014568e03fb00708c0bb5572c34fc7c466/public/favicon.ico
--------------------------------------------------------------------------------
/public/index.html:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 | vue-cli-3-mock-data
9 |
10 |
11 |
14 |
15 |
16 |
17 |
18 |
--------------------------------------------------------------------------------
/src/App.vue:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
22 |
--------------------------------------------------------------------------------
/src/api/mock/data/posts.json:
--------------------------------------------------------------------------------
1 | [
2 | { "title": "Post Title 1" },
3 | { "title": "Post Title 2" },
4 | { "title": "Post Title 3" },
5 | { "title": "Post Title 4" },
6 | { "title": "Post Title 5" }
7 | ]
--------------------------------------------------------------------------------
/src/api/mock/index.js:
--------------------------------------------------------------------------------
1 | import posts from './data/posts'
2 |
3 | const fetch = (mockData, time = 0) => {
4 | return new Promise((resolve) => {
5 | setTimeout(() => {
6 | resolve(mockData)
7 | }, time)
8 | })
9 | }
10 |
11 | export default {
12 | fetchPosts () {
13 | return fetch(posts, 1000) // wait 1s before returning posts
14 | }
15 | }
16 |
--------------------------------------------------------------------------------
/src/api/server/index.js:
--------------------------------------------------------------------------------
1 | import axios from 'axios'
2 |
3 | export default {
4 | fetchPosts () {
5 | return axios
6 | .get('https://jsonplaceholder.typicode.com/posts')
7 | .then(response => response.data)
8 | }
9 | }
10 |
--------------------------------------------------------------------------------
/src/assets/logo.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/TahaSh/use-mock-data-in-vue/bc1ff8014568e03fb00708c0bb5572c34fc7c466/src/assets/logo.png
--------------------------------------------------------------------------------
/src/components/HelloWorld.vue:
--------------------------------------------------------------------------------
1 |
2 |
3 |
{{ msg }}
4 |
5 | For a guide and recipes on how to configure / customize this project,
6 | check out the
7 | vue-cli documentation.
8 |
9 |
Installed CLI Plugins
10 |
14 |
Essential Links
15 |
22 |
Ecosystem
23 |
30 |
31 |
32 |
33 |
41 |
42 |
43 |
58 |
--------------------------------------------------------------------------------
/src/main.js:
--------------------------------------------------------------------------------
1 | import Vue from 'vue'
2 | import App from './App.vue'
3 | import router from './router'
4 | import store from './store'
5 |
6 | Vue.config.productionTip = false
7 |
8 | new Vue({
9 | router,
10 | store,
11 | render: h => h(App)
12 | }).$mount('#app')
13 |
--------------------------------------------------------------------------------
/src/router.js:
--------------------------------------------------------------------------------
1 | import Vue from 'vue'
2 | import Router from 'vue-router'
3 | import Home from './views/Home.vue'
4 |
5 | Vue.use(Router)
6 |
7 | export default new Router({
8 | routes: [
9 | {
10 | path: '/',
11 | name: 'home',
12 | component: Home
13 | },
14 | {
15 | path: '/about',
16 | name: 'about',
17 | // route level code-splitting
18 | // this generates a separate chunk (about.[hash].js) for this route
19 | // which is lazy-loaded when the route is visited.
20 | component: () => import(/* webpackChunkName: "about" */ './views/About.vue')
21 | }
22 | ]
23 | })
24 |
--------------------------------------------------------------------------------
/src/store.js:
--------------------------------------------------------------------------------
1 | import Vue from 'vue'
2 | import Vuex from 'vuex'
3 | import client from 'api-client'
4 |
5 | Vue.use(Vuex)
6 |
7 | export default new Vuex.Store({
8 | state: {
9 | posts: []
10 | },
11 |
12 | mutations: {
13 | setPosts (state, posts) {
14 | state.posts = posts
15 | }
16 | },
17 |
18 | actions: {
19 | fetchPosts ({ commit }) {
20 | return client
21 | .fetchPosts()
22 | .then(posts => commit('setPosts', posts))
23 | }
24 | }
25 | })
26 |
--------------------------------------------------------------------------------
/src/views/About.vue:
--------------------------------------------------------------------------------
1 |
2 |
3 |
This is an about page
4 |
5 |
6 |
--------------------------------------------------------------------------------
/src/views/Home.vue:
--------------------------------------------------------------------------------
1 |
2 |
3 |
Loading…
4 |
8 | -
13 |
{{ post.title }}
14 |
15 |
16 |
17 |
18 |
19 |
42 |
43 |
53 |
--------------------------------------------------------------------------------
/vue.config.js:
--------------------------------------------------------------------------------
1 | const path = require('path')
2 |
3 | module.exports = {
4 | chainWebpack: config => {
5 | const apiClient = process.env.VUE_APP_API_CLIENT // mock or server
6 | config.resolve.alias
7 | .set('api-client', path.resolve(__dirname, `src/api/${apiClient}`))
8 | }
9 | }
10 |
--------------------------------------------------------------------------------