1

I am trying to add a user input for a query to add to my application, which utilizes a News API. Working with Vue and Axios. It works with default links, however, implementing a user input for 'query' it doesn't work.

So simply I I am trying to add a search a feature for the user to insert any news topic they would like. Would appreciate any guidance and solutions.

Kind Regards

var vm = new Vue({
el: '#app',
data:{
  status:''

},
created: function(){
this.loadNews();
},
methods:{
loadNews: function(){
  this.status = 'Loading...';
  var vm = this;
  axios.get('https://newsapi.org/v2/everything?q=' + query + "&apiKey=6d2c267eacd549e79d27f597d7917699"')
  .then(function(response){
vm.status = response.data.articles[0].author + response.data.articles[0].title + response.data.articles[0].url;

  })
  .catch(function(error){
    vm.status = 'An error occurred' + error;

  })
}
}


});
<!DOCTYPE html>
<html lang="en" dir="ltr">
  <head>
    <meta charset="utf-8">
    <title>App</title>
    <link rel="stylesheet" href="master.css">
    <script src="https://unpkg.com/vue/dist/vue.js"></script>
<script src="https://unpkg.com/axios/dist/axios.min.js"></script>  </head>
  <body>
    <div id="app">
      <input type="text" v-model="query">
      <p>{{ status }}</p>
  
    </div>
<script src="script.js"></script>
  </body>
</html>

1 Answer 1

1

It looks like you didn't bind your input correctly.

change your data to

data:{
  status:'',
  query: ''
}

and axios url to

axios.get('https://newsapi.org/v2/everything?q=' + this.query + "&apiKey=6d2c267eacd549e79d27f597d7917699"')
Sign up to request clarification or add additional context in comments.

2 Comments

How would I make it submit the request?
Create a new button with click event. <button @click="loadNews">Submit</button>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.