在vuejs组件中获取axios base url的值

时间:2017-11-20 18:20:39

标签: javascript vuejs2 axios

我已经在vuejs全局配置中设置了axios配置,就像这样

const axiosConfig = {
baseURL: 'http://127.0.0.1:8000/api',
timeout: 30000, 
 };
Vue.prototype.$axios =  axios.create(axiosConfig);

现在我想检索一个组件内的basEuRL值

<script>
  data:()=>({
     apiurl:this.$axios.baseURL
   })
<script>

但上述方法无效。我期望财产apiurl

http://127.0.0.1:8000/api

1 个答案:

答案 0 :(得分:0)

不知道为什么需要在数据组件中调用baseURL,但我找到了一种常用的方法来使用axios baseURL,如Vue.js REST API Consumption with Axios

sub center {
   my ($n, $s) = @_;
   my $pad_len = $n - length($s);
   my $pad_len_l = int($pad_len/2);
   my $pad_len_r = $pad_len - $pad_len_l;
   return ( " " x $pad_len_l ) . $s . ( " " x $pad_len_r );
}

my $MAX_VAL_SIZE = 5;

for my $n (0...$N) {
   my @row;
   for my $k (0..$n) {
      push @row, C($n, $k);
   }

   my $row = join "  ", map center($MAX_VAL_SIZE, $_), @row;
   say center(($N+1)*($MAX_VAL_SIZE+2)-2, $row);
}

然后

// http-common.js
import axios from 'axios';

export const HTTP = axios.create({
  baseURL: `http://jsonplaceholder.typicode.com/`,
  headers: {
    Authorization: 'Bearer {token}'
  }
})