代码之家  ›  专栏  ›  技术社区  ›  MoreScratch

Vue.js组件不会渲染

  •  1
  • MoreScratch  · 技术社区  · 7 年前

    我有一个使用 axios 将一些数据带入我的页面。从未提出数据请求。这是我的密码:

    在App.vue中:

    <template>
      <div id="app">
        <Prices/>
      </div>
    </template>
    
    <script>
    import Prices from './components/Prices.vue'
    
    export default {
      name: 'app',
      components: {
        Prices
      }
    }
    </script>
    

    <template>
      <div>
        <p>{{ info }}</p>
      </div>
    </template>
    
    <script>
    export default {
      name: 'Prices',
      props: {
        info: String
      }
    }
    </script>
    

    在main.js中:

    import Vue from 'vue/dist/vue.js';
    import App from './App.vue'
    import './registerServiceWorker'
    import axios from 'axios'
    
    Vue.config.productionTip = false
    
    new Vue({
      render: h => h(App),
        data () {
        return {
          info: null
        }
      },
      mounted () {
        axios
          .get('https://api.coindesk.com/v1/bpi/currentprice.json')
          .then(response => (this.info = response))
      }
    }).$mount('#app')
    

    我是Vue.js的新手,这是我的第一个应用程序,我不确定为了让它正常工作我缺少了什么。任何指点都将不胜感激。

    2 回复  |  直到 7 年前
        1
  •  1
  •   Boussadjra Brahim    7 年前

    您应该指定 data 性质 response 给你 info

     .then(response => (this.info = response.data))
    

    并将该财产转让 通过 props 到子组件:

      <Prices :info='info'/>
    

    改变 app.vue 为此:

    <template>
      <div id="app">
        <Prices :info='info'/>
      </div>
    </template>
    
    <script>
    import Prices from './components/Prices.vue'
    
    export default {
      name: 'app',
        data () {
        return {
          info: null
        }
      },
      components: {
        Prices
      },
      mounted () {
        this.axios  //<--- use this.axios instead of `axios`
          .get('https://api.coindesk.com/v1/bpi/currentprice.json')
          .then(response => (this.info = response.data))
      }
    }
    </script>
    

    main.js :

    import Vue from 'vue/dist/vue.js';
    import App from './App.vue'
    import './registerServiceWorker'
    import axios from 'axios';
    import vueAxios from 'vue-axios';
    Vue.use(vueAxios,axios); //add Vue.use(axios)
    Vue.config.productionTip = false new Vue({
      render: h => h(App),
      data() {
        return {}
      }
    }).$mount('#app')
    

    笔记

    vue-axios

       npm install --save vue-axios
    

    axios 比如:

        import axios from 'axios';
        import vueAxios from 'vue-axios';
        Vue.use(vueAxios,axios); //add Vue.use(axios)
    
        2
  •  0
  •   Boussadjra Brahim    7 年前

    你必须提供 props

    main.js 获取任务 并在应用程序组件中执行此操作

    <template>
      <div id="app">
        <Prices :info="info"/>
      </div>
    </template>
    
    <script>
    import Prices from './components/Prices.vue'
    
    export default {
      name: 'app',
      data () {
        return {
          info: null
        }
      },
      components: {
        Prices
      },
     mounted () {
        axios
          .get('https://api.coindesk.com/v1/bpi/currentprice.json')
          .then(response => (this.info = response.data))
     }
    }
    </script>