Problems to render Vue.js main.js

Asked

Viewed 140 times

0

I’m new to Vue and I need to maintain a project.. the problem is this: I have in main.js the route scheme..

main.js

import Vue from 'vue'

const app = new Vue({
    el: '#app',
    data: {
        message: 'Hello Vue!'
    }
});

page.

<span>{{ message }}</span>

I happen to be trying to display the "message" that is on date in an external template.. that is in the folder address ".. /pages/page.Vue"; more when I put {{ message }} returns me the following error:

[Vue warn]: Property or method "message" is not defined on the instance but referenced During render. Make sure to declare reactive data properties in the data option.

Thank you!

  • It makes no sense for you to define properties in main.js, data transfer is done through the Vue components, in main you should render your core component and manage the library

  • Here’s a good example of methods to transfer data between components https://codesandbox.io/s/mm58vw8px8

  • Blz I’ll read here valeu man!

1 answer

1


From what I understand, you’re trying to access a property of data, in case the message who is in his main.js in a component page.vue.

If so, this will not work. You should have this variable inside the data() of the component page.vue. Check the example below:

page.

<template>
  <div>
    <span>{{ message }}</span>
  </div>
</template>

<script>
export default {
  name: 'page',
  data () {
    return {
      message: 'Hello Vue!'
    }
  }
}
</script>

Another way to do it is a way that I don’t particularly recommend, but you will be able to access the variable message of the Vue instance with the this.$root. Check the example below:

App.

<template>
  <div id="app">
    <img src="./assets/logo.png">
    <router-view/>
  </div>
</template>

<script>
export default {
  name: 'App',
  mounted () {
    console.log(this.$root.message)
  }
}
</script>
  • It worked here as first example brother! vlw.. if I want to reuse the same object in the entire application I need to use the concept of Storage.. like Vuex, right ?! Thank you!

  • Not at all. Yes, the ideal would be to use Vuex or Vue Stash if the application is smaller. It is not recommended to use the this.$root, using the data of the Vue instance, at least in my opinion.

  • 1

    Blz thanks man!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.