File tree Expand file tree Collapse file tree 3 files changed +55
-35
lines changed Expand file tree Collapse file tree 3 files changed +55
-35
lines changed Original file line number Diff line number Diff line change 1414</template >
1515
1616<script >
17+ import { ref } from ' vue'
18+
1719import Form from ' @/components/Form'
1820import List from ' @/components/List'
21+
1922export default {
2023 components: { Form, List },
21- data () {
24+ setup () {
25+ const items = ref ([
26+ {
27+ id: 1 ,
28+ body: ' hello vue 3' ,
29+ likes: 12 ,
30+ avatar: ` https://avatars.dicebear.com/api/male/1.svg` ,
31+ date: new Date (Date .now ()).toLocaleString ()
32+ },
33+ {
34+ id: 2 ,
35+ body: ' hello world' ,
36+ likes: 6 ,
37+ avatar: ` https://avatars.dicebear.com/api/male/2.svg` ,
38+ date: new Date (Date .now ()).toLocaleString ()
39+ }
40+ ])
41+ const handleSubmit = item => items .value .push (item)
42+
2243 return {
23- items: [
24- {
25- id: 1 ,
26- body: ' hello vue 3' ,
27- likes: 12 ,
28- avatar: ` https://avatars.dicebear.com/api/male/1.svg` ,
29- date: new Date (Date .now ()).toLocaleString ()
30- },
31- {
32- id: 2 ,
33- body: ' hello world' ,
34- likes: 6 ,
35- avatar: ` https://avatars.dicebear.com/api/male/2.svg` ,
36- date: new Date (Date .now ()).toLocaleString ()
37- }
38- ]
39- }
40- },
41- methods: {
42- handleSubmit (item ) {
43- this .items .push (item)
44+ items,
45+ handleSubmit
4446 }
4547 }
4648}
Original file line number Diff line number Diff line change 66</template >
77
88<script >
9+ import { ref } from ' vue'
10+
911export default {
10- data () {
11- return {
12- body: ' '
13- }
14- },
15- methods: {
16- onSubmit () {
17- this .$emit (' onSubmit' , {
12+ emits: [' onSubmit' ],
13+
14+ setup (_ , { emit }) {
15+ const body = ref (' ' )
16+
17+ const onSubmit = () => {
18+ emit (' onSubmit' , {
1819 id: Math .round (Math .random () * 30 ),
1920 avatar: ` https://avatars.dicebear.com/api/male/${ Date .now ()} .svg` ,
20- body: this . body ,
21+ body: body . value ,
2122 likes: 0 ,
2223 date: new Date (Date .now ()).toLocaleString ()
2324 })
24-
25- // reset
26- this .body = ' '
25+ body .value = ' '
2726 }
27+
28+ return { body, onSubmit }
2829 }
2930}
3031 </script >
Original file line number Diff line number Diff line change 11<template >
2+ <select v-model =" sortBy" >
3+ <option value =" date" >Sort by date</option >
4+ <option value =" likes" >Sort by like</option >
5+ </select >
26 <ul class =" tweets__wrapper" >
3- <Item v-for =" item in items " :key =" item.id" :item =" item" />
7+ <Item v-for =" item in sorteredItems " :key =" item.id" :item =" item" />
48 </ul >
59</template >
610
711<script >
12+ import { ref , computed } from ' vue'
813import Item from ' @/components/Item.vue'
914
1015export default {
@@ -14,6 +19,18 @@ export default {
1419 type: Array ,
1520 reqired: true
1621 }
22+ },
23+ setup ({ items }) {
24+ const sortBy = ref (' date' )
25+
26+ const sorteredItems = computed (() => {
27+ return items .sort ((a , b ) => {
28+ if (a[sortBy .value ] > b[sortBy .value ]) return - 1
29+ if (a[sortBy .value ] < b[sortBy .value ]) return 1
30+ })
31+ })
32+
33+ return { sortBy, sorteredItems }
1734 }
1835}
1936 </script >
You can’t perform that action at this time.
0 commit comments