-
-
Notifications
You must be signed in to change notification settings - Fork 52
/
Copy pathjson_example.vue
47 lines (45 loc) · 1.34 KB
/
json_example.vue
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
<template>
<div class="json-example">
<div class="select">
<a :class="showRaw ? 'active' : ''" @click="raw">Raw</a>
<a :class="showRaw ? '' : 'active'" @click="formatted">Formatted</a>
</div>
<pre v-if="showRaw"><code><slot></slot></code></pre>
<Prism v-else :code="pretty" language="json"></Prism>
</div>
</template>
<script>
import 'prismjs';
import 'prismjs/themes/prism-tomorrow.css';
import 'prismjs/components/prism-json';
import Prism from 'vue-prism-component';
export default {
name: 'JSONExample',
components: {
Prism,
},
data () {
return {
showRaw: true,
};
},
computed: {
pretty () {
if (!this.$slots.default || !this.$slots.default.length || !this.$slots.default[0].text) { return ''; }
try {
return JSON.stringify(JSON.parse(this.$slots.default[0].text), null, 2);
} catch (_) {
return this.$slots.default[0].text;
}
},
},
methods: {
raw () {
this.$data.showRaw = true;
},
formatted () {
this.$data.showRaw = false;
},
},
};
</script>