Home > PHP Framework > ThinkPHP > How to realize interaction between thinkphp and vue

How to realize interaction between thinkphp and vue

PHPz
Release: 2023-04-14 09:38:33
Original
1914 people have browsed it

With the continuous development of Web technology, the front-end and back-end separation model has become a trend in Web application development. In this model, the front end is responsible for presentation and interaction, while the back end is responsible for data processing and business logic. Therefore, the interaction between the front and back ends is particularly critical. This article will introduce how to implement interaction in ThinkPHP and Vue.

1. Prerequisites

Before we start discussing how to implement interaction in ThinkPHP and Vue, we need to ensure that the following prerequisites are met:

1. Already installed PHP running environment and database

2. ThinkPHP framework has been installed

3.Vue.js has been installed

If you have not completed these preparations, please Complete them first before continuing with this article.

2. API provided by ThinkPHP

In ThinkPHP, we can provide API through the controller to interact with Vue. An API can be a GET request or a POST request. Generally speaking, GET requests are used to obtain data, while POST requests are used to modify data. Let's look at an example.

1. Create a controller

First, we need to create a controller to provide the API. Assuming that we have created a controller named "Index" in ThinkPHP, we can add a method named "getArticles" to it to provide an API for getting a list of articles.

public function getArticles()
{
    $articles = Db::name('article')->select();
    return json($articles);
}
Copy after login

The above code uses the Db class to obtain the article list in the database, converts it to JSON format through the json function, and finally returns it to the browser as a response.

2. Access the API in Vue

Next, we need to call this API in Vue and get the returned article list. As we all know, Vue can use the axios library to send HTTP requests. Let's look at an example.

axios.get('/index/getArticles')
.then(function(response) {
    console.log(response.data);
})
.catch(function(error) {
    console.log(error);
});
Copy after login

The above code uses the axios.get method to access the API we created above, and prints the article list to the console when the response is successful. Note that we only need to specify the path to the API since we are using relative paths. If your server is configured with a virtual host, you need to configure the path as an absolute path.

3. Components provided by Vue

In addition to using the API, Vue also provides many components to make interacting with the backend more convenient. For example, Vue provides a component called Axios, which makes it easier for us to use the axios library.

1. Install Axios

To use Axios, we need to install it into our Vue project first. Run the following command in the console:

npm install axios --save
Copy after login

2. Use Axios

After the installation is complete, we can use it in the Vue component. Let's create a component called "ArticleList" in Vue to display a list of articles.

<template>
  <div>
    <ul>
      <li v-for="article in articles">{{ article.title }}</li>
    </ul>
  </div>
</template>

<script>
  import axios from 'axios';

  export default {
    data() {
      return {
        articles: []
      }
    },
    created() {
      axios.get('/index/getArticles')
      .then((response) => {
        this.articles = response.data;
      })
      .catch((error) => {
        console.log(error);
      });
    }
  }
</script>
Copy after login

The above code uses the axios.get method to get the article list from the API and save it in the component's properties. The component automatically executes the create function when it is created so that it displays the list of articles as soon as they are fetched.

4. Summary

Now, we have learned how to implement interaction in ThinkPHP and Vue. Whether you use APIs or components, communication between the front and back ends can be easily achieved. I hope this article is helpful to you, and I wish you more success in web application development!

The above is the detailed content of How to realize interaction between thinkphp and vue. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template