Javascript Vue.js:变量可以在中显示,但不能在已安装的钩子中使用
我是Vue.js新手,我尝试从/public文件夹加载config.json文件,以便在运行时更改某些设置 我现在可以加载文件,但在.vue文件中,我可以在零件上显示设置变量,但无法在零件上获取变量。以下是我的.vue文件: 有人知道如何解决这个问题吗 --添加main.js的代码--Javascript Vue.js:变量可以在中显示,但不能在已安装的钩子中使用,javascript,vue.js,vuejs2,vue-component,Javascript,Vue.js,Vuejs2,Vue Component,我是Vue.js新手,我尝试从/public文件夹加载config.json文件,以便在运行时更改某些设置 我现在可以加载文件,但在.vue文件中,我可以在零件上显示设置变量,但无法在零件上获取变量。以下是我的.vue文件: 有人知道如何解决这个问题吗 --添加main.js的代码-- mounted() { this.baseUrl = this.ApiBaseUrl; }, 安装的挂钩可能是访问此功能的正常功能: 如果使用箭头函数,则应将组件实例作为参数传递: 安装的挂钩可能
mounted() {
this.baseUrl = this.ApiBaseUrl;
},
安装的挂钩可能是访问此功能的正常功能:
如果使用箭头函数,则应将组件实例作为参数传递:
安装的挂钩可能是访问此功能的正常功能:
如果使用箭头函数,则应将组件实例作为参数传递:
ApiBaseUrl来自哪里?它不是道具、计算值、数据或函数。我已经更新了问题,所以我从main.js加载ApiBaseUrl。ApiBaseUrl来自哪里?它不是道具、计算值、数据或函数。我已经更新了问题,所以我从main.js加载ApiBaseUrl。
TypeError: Cannot read property 'ApiBaseUrl' of undefined
at VueComponent.mounted (Home.vue?76f2:207)
at invokeWithErrorHandling (vue.runtime.esm.js?2b0e:1854)
at callHook (vue.runtime.esm.js?2b0e:4219)
at Object.insert (vue.runtime.esm.js?2b0e:3139)
at invokeInsertHook (vue.runtime.esm.js?2b0e:6346)
at Vue.patch [as __patch__] (vue.runtime.esm.js?2b0e:6565)
at Vue._update (vue.runtime.esm.js?2b0e:3945)
at Vue.updateComponent (vue.runtime.esm.js?2b0e:4066)
at Watcher.get (vue.runtime.esm.js?2b0e:4479)
at new Watcher (vue.runtime.esm.js?2b0e:4468)
import Vue from "vue";
import App from "./App.vue";
Vue.config.productionTip = false;
fetch("config.json")
.then((res) => res.json())
.catch((error) => {
// ignore any errors
console.warn(error);
return {};
})
.then((config) => {
Vue.prototype.ApiBaseUrl = config.ApiBaseUrl;
new Vue({
render: (h) => h(App)
}).$mount("#app");
});
mounted() {
this.baseUrl = this.ApiBaseUrl;
},
mounted: (vm) => {//vm refers to this
vm.baseUrl = vm.ApiBaseUrl;
},