我试图使用 vuejs 作为 WP restapi 的前端脚手架。我需要所有 vue 组件都可以访问由 wordpress 生成的 api url。这是我所做的:
Vue.mixin({
data: function () {
return {
get apiURL () {
return document.querySelector('link[rel="https://api.w.org/"]').href;
}
}
}
});
问题是,我可以从模板标签内部访问变量,如下所示:
<template>
<div class="container">
<p>{{ apiURL }}</p>
</div>
</template>
但是我无法在组件的方法中访问它:
methods: {
loadPosts: function () {
this.status = 'Loading...';
axios.get( apiURL + 'wp/v2/posts').then((response) => {
this.status = '';
this.posts = response.data;
console.log(response.data);
}).catch((error) => {
console.log(error);
});
}
}
在这部分代码中,它给了我这个错误:
ReferenceError: apiURL is not defined
什么是正确的方法。我正在使用 VueJS 版本 2。