VueJS vue-spinner

2019-07-20 13:59发布

Hello I'm trying to set up view-spinner--> https://www.npmjs.com/package/vue-spinner

But an error appears:

vue.esm.js:591 [Vue warn]: Property or method "loading" is not defined on the instance but referenced during render. Make sure that this property is reactive, either in the data option, or for class-based components, by initializing the property.

That's my main JS (app.js):

import Vue from 'vue';
import App from './App.vue'
import axios from 'axios'
import router from './router'
import store from './vuex/store';
import PulseLoader from 'vue-spinner/src/PulseLoader.vue'

require('../scss/style.scss');

Vue.config.productionTip = false;
Vue.prototype.$http = axios;

new Vue({
    el: '#vueApp',
    router,
    store,
    template: '<App/>',
    components: {
        App,
        PulseLoader
    },
});

My .vue

    <template>
    <div class="app">
        <pulse-loader :loading="loading" :color="color" :size="size"></pulse-loader>
        <nav class="navbar navbar-expand-lg navbar-light bg-light">
            <button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarNav"
                    aria-controls="navbarNav" aria-expanded="false" aria-label="Toggle navigation">
                <span class="navbar-toggler-icon"></span>
            </button>
            <div class="collapse navbar-collapse" id="navbarNav">
                <ul class="navbar-nav">
                    <router-link class="nav-item" tag="li" to="/" active-class="active">
                        <a class="nav-link">Home</a>
                    </router-link>
                    <router-link class="nav-item" tag="li" to="/wow" active-class="active">
                        <a class="nav-link">Wow</a>
                    </router-link>
                </ul>
            </div>
        </nav>

        <router-view></router-view>
    </div>
</template>


<script>
    export default {
        name: 'app',
        data: () => {
            return {
                color: '#000000',
                size: '1000px'
            }
        }
    }
</script>

<style lang="scss">

</style>

Thanks for your help.

2条回答
祖国的老花朵
2楼-- · 2019-07-20 14:22

It looks like you are setting up vue-spinner in the wrong place, i.e main.js instead of App.vue.

This works (see CodeSandbox)

main.js

import Vue from "vue";
import App from "./App";
// import PulseLoader from "vue-spinner/src/PulseLoader.vue";

Vue.config.productionTip = false;

/* eslint-disable no-new */
new Vue({
  el: "#vueApp",
  template: "<App/>",
  components: {
    App,
    // PulseLoader
  }
});

App.vue

<template>
  <div id="app">
    <pulse-loader :loading="loading" :color="color" :size="size"></pulse-loader>
    ...
  </div>
</template>

<script>
import PulseLoader from "vue-spinner/src/PulseLoader.vue";

export default {
  name: "App",
  data() {
    return {
      loading: true,
      color: "#2c3e50",
      size: "10px"
    };
  },
  components: {
    PulseLoader,
    ...
  }
};
</script>

<style>
 ...
查看更多
Rolldiameter
3楼-- · 2019-07-20 14:33

in your data object add loading property and import the module as follow :

  import PulseLoader from "vue-spinner/src/PulseLoader.vue";
 export default{
 data: () => {
        return {
            color: '#000000',
            size: '1000px',
            loading:true
        }
    },
   components:{PulseLoader}
   ...
  }

or you could use computed a property like :

  computed:{
  loading(){ return true;} 
   }

you could do whatever you want and return a boolean value with loading name

查看更多
登录 后发表回答