VUE JS 2 + WEBPACK Cannot read property 'get' of undefined VUE RESOURCE

心不动则不痛 提交于 2019-12-24 00:37:58

问题


I feel lost.

I've used vue-cli for my project. I've vuerouter installed. Everything is ok. I want to use view resource in a vue component but i can't find why it don't works.

This is my main.js (i'm using webpack)

import Vue from 'vue'
import VueRouter from 'vue-router'
var VueResource = require('vue-resource')

Vue.use(VueRouter)
Vue.use(VueResource)

const router = new VueRouter({
  routes: [{
    path: '/',
    component: require('./components/index.vue')
  },
  {
    path: '/products/stock',
    component: require('./components/stock.vue')
  },
  {
    path: '/products/seo',
    component: require('./components/seo.vue')
  },
  {
    path: '/settings',
    component: require('./components/settings.vue')
  }
  ]
})

new Vue({
  el: '#app',
  router,
  data: {
    message: 'Hello Vue!'
  },
  mounted: () => {
    console.log("APP MOUNTED")
  },
  render: h => h(require('./App.vue'))
})

when i go to /#/product/seo there is the code:

<template>
  <div id="app">
    Seo tabs du fastmanager {{ message }}
    <input type="text" name="" v-model="message">
  </div>
</template>

<script>

export default {
  data: () => {
    return {
      message: "Hello buddy"
    }
  },
  mounted: () => {
    console.log("SEO MOUNTED")
    this.$http.get('/').then((response) => {
    // success callback
  }, (response) => {
    // error callback
  });

  }
}
</script>

<style lang="css">
</style>

i've have this error in JS console.

seo.vue?468d:18Uncaught TypeError: Cannot read property 'get' of undefined(…)

Vue resource is working well in main.js.

So, i think it's because the view is loaded before the app. I don't know how to do. Sorry for ma bad english.


回答1:


Ok i've found IT.

in my seo.vue i put this:

<template>
  <div id="app">
    Seo tabs du fastmanager
    <textarea type="text" name="" cols="40" rows="10" v-model="message"></textarea>
    <button type="button" v-on:click="test" name="button">Test</button>
  </div>
</template>

<script>
var Vue = require('vue')
export default {
  data: () => {
    return {
      message: "Coucou"
    }
  },
  mounted: function()
  {
    console.log(this)

  },
  methods: {
    test: function(){
      Vue.http.get('https://jsonplaceholder.typicode.com/posts/1').then((response) => {
        console.log("SUCCESS",response);
        this.message = response
      }, (response) => {
        console.log("ERROR",response);
        // error callback
      });
    }
  }
}
</script>

<style lang="css">
</style>

An dit work fine.



来源:https://stackoverflow.com/questions/40999766/vue-js-2-webpack-cannot-read-property-get-of-undefined-vue-resource

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!