gpt4 book ai didi

node.js - Node Vue 需要在组件中使用的库

转载 作者:太空宇宙 更新时间:2023-11-04 02:12:44 25 4
gpt4 key购买 nike

我刚刚开始使用 vue.js 以及需要使用 NPM 的库的组件。

到目前为止,我使用 gulp Vueify 将我的核心代码与 .vue 文件一起编译到 bundle.js 文件中。它可以与我的组件配合使用,但是当添加像 Axios 这样的组件时,它无法找到该库。

所以我做了NPM install axios --save-dev 这给了我这个库。

然后是我的 vueCore.js 文件

var $               = require("jquery")
var Vue = require('vue')
var axios = require('axios')
var contentList = require('./components/content-list.vue')
var heroHeader = require('./components/hero-header.vue')

new Vue({
el: '#page',
components: {
'dashboard-view': dashboard,
'content-list': contentList,
'hero-header': heroHeader
}
})

然后当我尝试在 dashboard 组件中使用 axios

mounted:  function() {

axios.get('/custom_api/api_home_get.php?', {
params: {
ID: i4
}
})
.then(function (response) {
this.last = response;
console.log(response);
})
.catch(function (error) {
console.log(error);
});
}

然后我得到了所有组件元素,并出现错误 Uncaught ReferenceError: axios is not Defined

最佳答案

您必须在每个要使用它的组件中 require('axios')。

但我更喜欢的方式是使用 axios 设置 Vue.prototype.$http。因此,在声明您的 Vue 应用程序之前,您需要声明

Vue.prototype.$http = axios

然后 axios 将在您的任何组件中可用,而无需导入它。

mounted: function() {
this.$http.get('/whatever').then(whatever)
...
}

来源:vue-resource retirement

关于node.js - Node Vue 需要在组件中使用的库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41470987/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com