简体   繁体   English

axios 未在 Vue js 2 中定义

[英]axios is not defined in Vue js 2

strong text I'm new in laravel and vue js.强文本我是 laravel 和 vue js 的新手。 I'm trying to learn Vue js.In a Laravel+Vue project, i tried to use axios to post an API response.我正在尝试学习 Vue js。在 Laravel+Vue 项目中,我尝试使用 axios 发布 API 响应。 axios is not defined in Vue js 2. How to solve this problem.When i add some data. axios 未在 Vue js 2 中定义。如何解决这个问题。当我添加一些数据时。 data didn't show and also didn't work my delete function.数据没有显示,我的删除 function 也没有工作。 and why I face this problem?为什么我会遇到这个问题? thanks for advance感谢提前

app.js应用程序.js

import Vue from 'vue';

import App from './vue/app';



import { library } from '@fortawesome/fontawesome-svg-core'
import { faPlusSquare, faTrash } from '@fortawesome/free-solid-svg-icons'
import { FontAwesomeIcon } from '@fortawesome/vue-fontawesome'

library.add(faPlusSquare, faTrash)

Vue.component('font-awesome-icon', FontAwesomeIcon)


const app = new Vue ({
    el: '#app',
    components: { App }
});

addItemForm addItemForm



<template>
  <div class="addItem">
    <input type="text" v-model="item.name" />
    <font-awesome-icon
      icon="plus-square"
      @click="addItem()"
      :class="[item.name ? 'active' : 'inactive', 'plus']"
    />
  </div>
</template>

<script>
export default {
  data: function () {
    return {
      item: {
        name: "",
      },
    };
  },
  methods: {
    addItem() {
      if (this.item.name == "") {
        return;
      }
      axios
        .post("api/item/store", {
          item: this.item,
        })
        .then((response) => {
          if (response.status == 201) {
            this.item.name = "";
            this.$emit("reloadlist");
          }
        })
        .catch((error) => {
          console.log(error);
        });
    },
  },
};
</script>
<style scoped>
.addItem {
  display: flex;
  justify-content: center;
  align-content: center;
}

input {
  background: rgb(236, 164, 138);
  border: 0px;
  outline: none;
  padding: 5px;
  margin-right: 10px;
  width: 100%;
}
.plus {
  font-size: 20px;
}

.active {
  color: rgb(34, 211, 57);
}

.inactive {
  color: rgb(63, 66, 63);
}
</style>

app.vue应用程序.vue



<template>

  <div class="todoListContainer">
    <div class="heading">
      <h2 id="title">Todo List</h2>
      <add-item-form v-on:reloadlist="getList()" />
    </div>
    <list-view :items="items"
    v-on:reloadlist="getList()" />
  </div>
</template>

<script>

import addItemForm from "./addItemForm.vue";
import listView from "./listView.vue";

export default {
  components: {
    addItemForm,
    listView,
  },
  data: function () {
    return {
      items: [],
    };
  },
  methods: {
    getList() {
      axios
        .post('api/items')
        .then((response) => {
          this.items = response.data;
        })
        .catch((error) => {
          console.log(error);
        });
    },
  },
  created() {
    this.getList();
  },
};
</script>

<style scoped>
.todoListContainer {
  width: 350px;
  margin: auto;
}

.heading {
  background: wheat;
  padding: 10px;
}
#title {
  text-align: center;
}
</style>

In order to use, axios you have to import the axios.为了使用axios ,您必须导入 axios。 Considering you have already installed axios already in your project as it is third party library.考虑到您已经在项目中安装了axios ,因为它是第三方库。

import axios from 'axios';

Add above line in the component, wherever you use the package.在组件中添加以上行,无论您使用 package。

First install both axios and vue-axios packages.首先安装 axios 和 vue-axios 包。

npm install axios vue-axios

Then in app.js file write this code:然后在 app.js 文件中写下这段代码:

import axios from 'axios'
import VueAxios from 'vue-axios'

Vue.use(VueAxios, axios)

// this is the default base url in laravel
axios.defaults.baseURL = 'http://127.0.0.1:8000';

// this line is written to avoid csrf error
window.axios.defaults.headers.common = {
    'X-Requested-With': 'XMLHttpRequest',
    'X-CSRF-TOKEN' : document.querySelector('meta[name="csrf-token"]').getAttribute('content')
};

Then when you want to use axios just write this.axios .然后,当您想使用 axios 时,只需编写this.axios

Firstly, install axios and vue-axios首先,安装 axios 和 vue-axios

npm install axios vue-axios

Seconly, import it inside your script其次,将其导入您的script

<script>

import addItemForm from "./addItemForm.vue";
import listView from "./listView.vue";

import axios from 'axios';  // <-- add this line

export default {
  components: {
    addItemForm,
    listView,
  },
  data: function () {
    return {
      items: [],
    };
  },
  methods: {
    getList() {
      axios
        .post('api/items')
        .then((response) => {
          this.items = response.data;
        })
        .catch((error) => {
          console.log(error);
        });
    },
  },
  created() {
    this.getList();
  },
};
</script>

hi you must install Axios first您好,您必须先安装 Axios

npm install axios vue-axios

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM