Skip to content

Instantly share code, notes, and snippets.

@davidroyer
Last active August 23, 2016 00:46
Show Gist options
  • Save davidroyer/e2599a95e2bbf63b896e7d603c3edc42 to your computer and use it in GitHub Desktop.
Save davidroyer/e2599a95e2bbf63b896e7d603c3edc42 to your computer and use it in GitHub Desktop.
Example of using Vue.js with a simple store and vue-resource
<template>
<div id="app">
<h1>App Vue File</h1>
<button type="button" name="button"
v-on:click="changeState">
Change State</button>
<ul class="post__list">
<post v-for="post in sharedState.posts"
:post="post">
</post>
</ul>
</div>
</template>
<script>
import {store} from './store.js'
import Post from './Post.vue'
export default {
data: function () {
return {
sharedState: store.state
}
},
components: { Post },
methods: {
changeState: function () {
store.state.posts[0].title = 'Changed title'
}
}
}
</script>
<style>
body {
font-family: Helvetica, sans-serif;
}
</style>
{
"posts": [
{
"id": 1,
"title": "doloribus ad provident suscipit at",
"content": "qui consequuntur ducimus possimus quisquam amet similique\nsuscipit porro ipsam amet\neos veritatis officiis exercitationem vel fugit aut necessitatibus totam\nomnis rerum consequatur expedita quidem cumque explicabo"
},
{
"id": 2,
"title": "eum et est occaecati",
"content": "ullam autem assumenda voluptatem adipisci\nsit amet autem assumenda provident rerum culpa\nquis hic commodi nesciunt rem tenetur doloremque ipsam iure\nquis sunt voluptatem rerum illo velit"
},
{
"id": 3,
"title": "nesciunt quas odio",
"content": "repudiandae veniam quaerat sunt sed\nalias aut fugiat sit autem sed est\nvoluptatem omnis possimus esse voluptatibus quis\nest aut tenetur dolor neque"
},
{
"id": 4,
"title": "eum et est occaecati",
"content": "ullam et saepe reiciendis voluptatem adipisci\nsit amet autem assumenda provident rerum culpa\nquis hic commodi nesciunt rem tenetur doloremque ipsam iure\nquis sunt voluptatem rerum illo velit"
},
{
"id": 5,
"title": "nesciunt quas odio",
"content": "repudiandae veniam quaerat sunt sed\nalias aut fugiat sit autem sed est\nvoluptatem omnis possimus esse voluptatibus quis\nest aut tenetur dolor neque"
},
{
"id": 6,
"title": "quas eum odio",
"content": " saepe reiciendis voluptatem dipisci\nsit amet au sit autem sed est\nvoluptatem omnis possimus esse voluptatibus quis\nest aut tenetur dolor neque"
}
]
}
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8"/>
</head>
<body>
<app></app>
<script src="main.js"></script>
</body>
</html>
import Vue from 'vue'
import App from './App.vue'
import {store} from './store.js'
new Vue({
el: 'body',
components: { App },
data: {
sharedState: store.state
},
init: function () {
store.fetchPosts()
}
})
<template>
<li class="post__item">
<h3>{{ post.title }}</h3>
<span>{{ post.excerpt }}</span>
</li>
</template>
<script>
export default {
props: ['post']
}
</script>
<style lang="css">
</style>
import Vue from 'vue'
import VueResource from 'vue-resource'
Vue.use(VueResource)
export const store = {
state: {
posts: ''
},
fetchPosts: function () {
const postUrl = './dummyDB.json'
Vue.http.get(postUrl).then((response) => {
this.state.posts = response.data.posts
})
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment