vue @click更改api请求网址

时间:2020-08-07 04:56:02

标签: rest vue.js axios

我对Vue还是很陌生,现在被困住了。
问题:我无法通过v-model进行选择来更改api url来更改html上的数据。
这是我的html:

     <div class="uk-width-1-2@s">
                        <select class="uk-select" id="form-stacked-select" v-model="selected" >
                            <option v-for="option in options" v-bind:value="option.value">
                                {{ option.text }}
                            </option>
                        </select>
                    </div>
  <div v-else v-for="(cases, index) in cases_list"  :key="index" >
                       <p>{{cases.deaths}} deaths.</p>
          
                    </div>
            

这是我的Js:

const API_URL = 'https://covid19-brazil-api.now.sh/api/report/v1'

new Vue({
    el: '#app',
    data() {
        return {
            cases_list: [],
            loading: true,
            errored: false,
            selected: 'df',
            state: '',
            number: '',
            options: [
                { text: 'AC', value: 'ac' },
                { text: 'AL', value: 'al' },
                { text: 'AP', value: 'ap' },
                { text: 'AM', value: 'am' },
                { text: 'BA', value: 'ba' },
                { text: 'CE', value: 'ce' },
                { text: 'DF', value: 'df' },
                { text: 'ES', value: 'es' },
                { text: 'GO', value: 'go' },
                { text: 'MA', value: 'ma' },
                { text: 'MT', value: 'mt' },
                { text: 'MS', value: 'ms' },
                { text: 'MG', value: 'mg' },
                { text: 'PA', value: 'pa' },
                { text: 'PB', value: 'pb' },
                { text: 'PR', value: 'pr' },
                { text: 'PE', value: 'pe' },
                { text: 'PI', value: 'pi' },
                { text: 'RJ', value: 'rj' },
                { text: 'RN', value: 'rn' },
                { text: 'RS', value: 'rs' },
                { text: 'RO', value: 'ro' },
                { text: 'RR', value: 'rr' },
                { text: 'SC', value: 'sc' },
                { text: 'SP', value: 'sp' },
                { text: 'SE', value: 'se' },
                { text: 'TO', value: 'to' }
            ],
            
        }
    },
    mounted() {
        this.getCases();

    },
methods: {
    getCases() {
        axios
            .get(API_URL)
            .then((response) => {
                this.cases_list = response.data.data;
                console.log(response.data.data);
            })
            .catch(error => {
                console.log(error)
                this.errored = true
            })
            .finally(() => this.loading = false)
    }
}

})

我的select选项链接到我拥有文本和值的options数组,并且每个值都应在被选中时在api url的末尾添加一个'uf / $ {option}'...
就像:const API_URL ='https://covid19-brazil-api.now.sh/api/report/v1'+'uf /%{option}';
我对如何使此选择有效感到非常困惑。我无法在vue文档中找到

1 个答案:

答案 0 :(得分:1)

您必须在Axios调用之前配置URL。

const API_URL = 'https://covid19-brazil-api.now.sh/api/report/v1/brazil'

new Vue({
  el: '#app',
  data() {
    return {
      cases_list: [],
      loading: true,
      errored: false,
      selected: 'df',
      state: '',
      number: '',
      options: [{
          text: 'AC',
          value: 'ac'
        },
        {
          text: 'AL',
          value: 'al'
        },
        {
          text: 'AP',
          value: 'ap'
        },
        {
          text: 'AM',
          value: 'am'
        },
        {
          text: 'BA',
          value: 'ba'
        },
        {
          text: 'CE',
          value: 'ce'
        },
        {
          text: 'DF',
          value: 'df'
        },
        {
          text: 'ES',
          value: 'es'
        },
        {
          text: 'GO',
          value: 'go'
        },
        {
          text: 'MA',
          value: 'ma'
        },
        {
          text: 'MT',
          value: 'mt'
        },
        {
          text: 'MS',
          value: 'ms'
        },
        {
          text: 'MG',
          value: 'mg'
        },
        {
          text: 'PA',
          value: 'pa'
        },
        {
          text: 'PB',
          value: 'pb'
        },
        {
          text: 'PR',
          value: 'pr'
        },
        {
          text: 'PE',
          value: 'pe'
        },
        {
          text: 'PI',
          value: 'pi'
        },
        {
          text: 'RJ',
          value: 'rj'
        },
        {
          text: 'RN',
          value: 'rn'
        },
        {
          text: 'RS',
          value: 'rs'
        },
        {
          text: 'RO',
          value: 'ro'
        },
        {
          text: 'RR',
          value: 'rr'
        },
        {
          text: 'SC',
          value: 'sc'
        },
        {
          text: 'SP',
          value: 'sp'
        },
        {
          text: 'SE',
          value: 'se'
        },
        {
          text: 'TO',
          value: 'to'
        }
      ],

    }
  },
  mounted() {
    this.getCases();

  },
  watch: {
    selected: function(val) {
      this.getCases()
    },
  },
  methods: {
    getCases() {
      console.log(API_URL + '/uf/' + this.selected);
      axios
        .get(API_URL + '/uf/' + this.selected)
        .then((response) => {
          this.cases_list = response.data;
          console.log(response.data);
        })
        .catch(error => {
          console.log(error)
          this.errored = true
        })
        .finally(() => this.loading = false)
    }
  }

})
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.17/vue.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/axios/0.19.2/axios.min.js" integrity="sha512-VZ6m0F78+yo3sbu48gElK4irv2dzPoep8oo9LEjxviigcnnnNvnTOJRSrIhuFk68FMLOpiNz+T77nNY89rnWDg==" crossorigin="anonymous"></script>
<div id='app' class="uk-width-1-2@s">
  <select class="uk-select" id="form-stacked-select" v-model="selected">
    <option v-for="option in options" v-bind:value="option.value">
      {{ option.text }}
    </option>
  </select>
</div>