Home > OS >  Vue 3 - State is not available and is not showing in Vue devtools
Vue 3 - State is not available and is not showing in Vue devtools

Time:06-01

I was following a tutorial on learning Vuejs 3 and for some reason after defining my reactive object, none of the properties are available or are visible in the vue-devtools. The pokemon names I am trying to render doesn't event appear because the pokemons property is not available. Can someone help me figure out what is going on? here is my code below:

HomeView.vue:

<template>
  <div >
    <h3>Hello World!</h3>
      <p>{{number}}</p>
    <ul>
      <li v-for="pokemon in pokemons" :key="pokemon.name" >{{pokemon.name}}</li>
    </ul>
  </div>
</template>

<script>
import axios from "axios";
import { onMounted } from '@vue/runtime-core';
import { reactive } from "vue";
// @ is an alias to /src

export default {
  name: 'HomeView',
  setup(){

    let number = 1;

    const state = reactive({
      pokemons:[],
      urlIdLookup: {}
    })

    onMounted( async () =>{
      try {
          const pokemonReq = await axios.get("https://pokeapi.co/api/v2/pokemon?");
          console.log("data", pokemonReq.data);
          console.log("state", state)
          state.pokemons = pokemonReq.data.results;
      } catch(err){
        console.log(err);
      }
    });

  }
}
</script>

App.vue:

<template>
    <div >
      <router-link   to="/">Pokemon Picker</router-link>
    </div>
  <router-view/>
</template>

<style>
</style>

router > index.js


import { createRouter, createWebHistory } from 'vue-router'
import HomeView from '../views/HomeView.vue'

const routes = [
  {
    path: '/',
    name: 'home',
    component: HomeView
  },
  {
    path: '/about/:slug',
    name: 'about',
    // route level code-splitting
    // this generates a separate chunk (about.[hash].js) for this route
    // which is lazy-loaded when the route is visited.
    component: () => import(/* webpackChunkName: "about" */ '../views/AboutView.vue')
  }
]

const router = createRouter({
  history: createWebHistory(process.env.BASE_URL),
  routes
})

export default router

CodePudding user response:

You need to return your reactive data from setup function:

const { onMounted, reactive, toRefs, ref } = Vue;
const app = Vue.createApp({
    setup(){
    let number = ref(1);
    const state = reactive({
      pokemons:[],
      urlIdLookup: {}
    })
    onMounted( async () =>{
      try {
        const pokemonReq = await axios.get("https://pokeapi.co/api/v2/pokemon?");
        state.pokemons = pokemonReq.data.results;
      } catch(err){
        console.log(err);
      }
    });
    return { ...toRefs(state), number }
  }
})
app.mount('#demo')
<script src="https://unpkg.com/vue@3/dist/vue.global.prod.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/axios/0.27.2/axios.min.js" integrity="sha512-odNmoc1XJy5x1TMVMdC7EMs3IVdItLPlCeL5vSUPN2llYKMJ2eByTTAIiiuqLg GdNr9hF6z81p27DArRFKT7A==" crossorigin="anonymous" referrerpolicy="no-referrer"></script>
<div id="demo">
  <div >
    <h3>Hello World!</h3>
      <p>{{number}}</p>
    <ul>
      <li v-for="pokemon in pokemons" :key="pokemon.name" >{{pokemon.name}}</li>
    </ul>
  </div>
</div>

  • Related