json-view.vue 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. <template>
  2. <div class="cube-json-view">
  3. <h3>{{title}}</h3>
  4. <pre v-if="changed">
  5. <code ref="code">{{data}}</code>
  6. </pre>
  7. </div>
  8. </template>
  9. <script type="text/ecmascript-6">
  10. import hljs from 'highlight.js/lib/highlight'
  11. import json from 'highlight.js/lib/languages/json'
  12. import 'highlight.js/styles/solarized-dark.css'
  13. hljs.registerLanguage('json', json)
  14. export default {
  15. props: {
  16. data: {
  17. type: Object,
  18. default() {
  19. return {}
  20. }
  21. },
  22. title: {
  23. type: String,
  24. default: ''
  25. }
  26. },
  27. data() {
  28. return {
  29. changed: true
  30. }
  31. },
  32. watch: {
  33. data: {
  34. handler() {
  35. this.changed = false
  36. this.$nextTick(() => {
  37. this.changed = true
  38. this.$nextTick(() => {
  39. this.$refs.code && hljs.highlightBlock(this.$refs.code)
  40. })
  41. })
  42. },
  43. deep: true
  44. }
  45. },
  46. mounted() {
  47. this.$refs.code && hljs.highlightBlock(this.$refs.code)
  48. }
  49. }
  50. </script>
  51. <style lang="stylus" rel="stylesheet/stylus">
  52. @import "~@/common/stylus/variable.styl"
  53. .cube-json-view
  54. h3
  55. margin-top: 15px
  56. font-size: $fontsize-large
  57. </style>