Skip to content

Commit f3ac9bb

Browse files

File tree

7 files changed

+70
-22
lines changed

7 files changed

+70
-22
lines changed

package.json

+1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
"build": "vue-cli-service build"
88
},
99
"dependencies": {
10+
"@vuex-orm/core": "^0.36.4",
1011
"vue": "^3.0.0",
1112
"vuex": "^4.0.0-0"
1213
},

src/App.vue

+22-5
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,32 @@
11
<template>
2-
<HelloWorld msg="Welcome to Your Vue.js + TypeScript App" />
2+
<h1>state entities</h1>
3+
<pre>{{ entities }}</pre>
34
</template>
45

56
<script lang="ts">
6-
import { defineComponent } from 'vue'
7-
import HelloWorld from './components/HelloWorld.vue'
7+
import { defineComponent, ref, onMounted } from 'vue'
8+
import { useStore } from 'vuex'
9+
import { User } from '@/models'
810
911
export default defineComponent({
1012
name: 'App',
11-
components: {
12-
HelloWorld,
13+
setup() {
14+
const { state } = useStore()
15+
const entities = ref(state.entities)
16+
17+
onMounted(async () => {
18+
await User.insert({
19+
data: {
20+
id: 1,
21+
name: 'John Doe',
22+
23+
},
24+
})
25+
})
26+
27+
return {
28+
entities,
29+
}
1330
},
1431
})
1532
</script>

src/components/HelloWorld.vue

-16
This file was deleted.

src/models.ts

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import { Model } from '@vuex-orm/core'
2+
3+
export class User extends Model {
4+
static entity = 'users'
5+
6+
static fields() {
7+
return {
8+
id: this.attr(null),
9+
name: this.attr(''),
10+
email: this.attr(''),
11+
}
12+
}
13+
}
14+
15+
export class Post extends Model {
16+
static entity = 'posts'
17+
18+
static fields() {
19+
return {
20+
id: this.attr(null),
21+
user_id: this.attr(null),
22+
title: this.attr(''),
23+
body: this.attr(''),
24+
published: this.attr(false),
25+
author: this.belongsTo(User, 'user_id'),
26+
}
27+
}
28+
}

src/store/index.ts

+6
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,14 @@
11
import { createStore } from 'vuex'
2+
import VuexORM from '@vuex-orm/core'
3+
import * as models from '@/models'
4+
5+
const database = new VuexORM.Database()
6+
for (let model of Object.values(models)) database.register(model)
27

38
export default createStore({
49
state: {},
510
mutations: {},
611
actions: {},
712
modules: {},
13+
plugins: [VuexORM.install(database)],
814
})

tsconfig.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"compilerOptions": {
3-
"target": "es5",
3+
"target": "es6",
44
"module": "esnext",
55
"strict": true,
66
"jsx": "preserve",

yarn.lock

+12
Original file line numberDiff line numberDiff line change
@@ -505,6 +505,13 @@
505505
resolved "https://registry.yarnpkg.com/@vue/web-component-wrapper/-/web-component-wrapper-1.3.0.tgz#b6b40a7625429d2bd7c2281ddba601ed05dc7f1a"
506506
integrity sha512-Iu8Tbg3f+emIIMmI2ycSI8QcEuAUgPTgHwesDU1eKMLE4YC/c/sFbGc70QgMq31ijRftV0R7vCm9co6rldCeOA==
507507

508+
"@vuex-orm/core@^0.36.4":
509+
version "0.36.4"
510+
resolved "https://registry.yarnpkg.com/@vuex-orm/core/-/core-0.36.4.tgz#9e2b1b8dfd74c2a508f1862ffa3e4a2c1e4cc60c"
511+
integrity sha512-Wkq1G+FtcEQ4LbhEZUZalhuErb2nY2nuBo2UeGVpbVKkMss5piJhw7bt5TpWecDNmIUc2BzaenDDqm08AAe7Hw==
512+
dependencies:
513+
normalizr "^3.6.0"
514+
508515
"@webassemblyjs/[email protected]":
509516
version "1.9.0"
510517
resolved "https://registry.yarnpkg.com/@webassemblyjs/ast/-/ast-1.9.0.tgz#bd850604b4042459a5a41cd7d338cbed695ed964"
@@ -4562,6 +4569,11 @@ normalize-url@^3.0.0:
45624569
resolved "https://registry.yarnpkg.com/normalize-url/-/normalize-url-3.3.0.tgz#b2e1c4dc4f7c6d57743df733a4f5978d18650559"
45634570
integrity sha512-U+JJi7duF1o+u2pynbp2zXDW2/PADgC30f0GsHZtRh+HOcXHnw137TrNlyxxRvWW5fjKd3bcLHPxofWuCjaeZg==
45644571

4572+
normalizr@^3.6.0:
4573+
version "3.6.1"
4574+
resolved "https://registry.yarnpkg.com/normalizr/-/normalizr-3.6.1.tgz#d367ab840e031ff382141b8d81ce279292ff69fe"
4575+
integrity sha512-8iEmqXmPtll8PwbEFrbPoDxVw7MKnNvt3PZzR2Xvq9nggEEOgBlNICPXYzyZ4w4AkHUzCU998mdatER3n2VaMA==
4576+
45654577
npm-run-path@^2.0.0:
45664578
version "2.0.2"
45674579
resolved "https://registry.yarnpkg.com/npm-run-path/-/npm-run-path-2.0.2.tgz#35a9232dfa35d7067b4cb2ddf2357b1871536c5f"

0 commit comments

Comments
 (0)