A
A
Arioch2019-07-17 15:16:06
Vue.js
Arioch, 2019-07-17 15:16:06

When exporting/importing an object in Vue(Nuxt), the contents of the object are supplemented with service data - how to do it right?

I decided to put the JSON content in a separate file:

<script>
//data/Content.vue
export default {
  1: { id: 1, title: "title-1", short: "short-1", body: "body-1", icon: "", slug: "" },
  2: { id: 2, title: "title-2", short: "short-2", body: "body-2", icon: "", slug: "" }
} 
</script>

import into page:
//pages/index.vue
...
import Content from "@/data/Content";
...
data() {
    return {
      content: Content
    };
  }
...

and then I output:
<template>
  <div>
    {{content}}

renders in the browser like this:
{ "1": { "id": 1, "title": "title-1", "short": "short-1", "body": "body-1", "icon": "", "slug": "" }, "2": { "id": 2, "title": "title-2", "short": "short-2", "body": "body-2", "icon": "", "slug": "" }, "beforeCreate": [ null ], "beforeDestroy": [ null ], "__file": "data/Content.vue" }

those. adds three more properties with service information to the end of the object.
It can, of course, not bad, immediately know in which hooks it is available and what and how, but when sorting through v-forthese things, they are also displayed, but I, of course, do not need this.
So far I've done this:
export default [{ id: 1, title: "title-1", short: "short-1", body: "body-1", icon: "", slug: "" }, { id: 2, title: "title-2", short: "short-2", body: "body-2", icon: "", slug: "" }]

so it's normal.
But I want to understand.
How to implement such an import in the most correct way?
Vuex and axios - do not suggest :) I want to do it through regular files.
And yet, is it possible to get away from the construction <script>...</script>in the content file at all? I tried - without it does not work. But is it somehow possible?
Thank you.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
M
Michael, 2019-07-17
@arioch77

It's funny, but why not just include the .json file?
into a separate vue file?

N
nvdfxx, 2019-07-17
@nvdfxx

<script>
//data/Content.vue
export default {
  1: { id: 1, title: "title-1", short: "short-1", body: "body-1", icon: "", slug: "" },
  2: { id: 2, title: "title-2", short: "short-2", body: "body-2", icon: "", slug: "" }
} 
</script>

Some kind of hell, do you often wrap imported components in a script tag?
save as .json and import json not vue module

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question